StereoSGBM.cpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2010-2016, Mathieu Labbe - IntRoLab - Universite de Sherbrooke
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7  * Redistributions of source code must retain the above copyright
8  notice, this list of conditions and the following disclaimer.
9  * Redistributions in binary form must reproduce the above copyright
10  notice, this list of conditions and the following disclaimer in the
11  documentation and/or other materials provided with the distribution.
12  * Neither the name of the Universite de Sherbrooke nor the
13  names of its contributors may be used to endorse or promote products
14  derived from this software without specific prior written permission.
15 
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 
30 #include <opencv2/calib3d/calib3d.hpp>
31 #include <opencv2/imgproc/imgproc.hpp>
32 #include <opencv2/imgproc/types_c.h>
33 
34 namespace rtabmap {
35 
37  StereoDense(parameters),
38  blockSize_(Parameters::defaultStereoSGBMBlockSize()),
39  minDisparity_(Parameters::defaultStereoSGBMMinDisparity()),
40  numDisparities_(Parameters::defaultStereoSGBMNumDisparities()),
41  preFilterCap_(Parameters::defaultStereoSGBMPreFilterCap()),
42  uniquenessRatio_(Parameters::defaultStereoSGBMUniquenessRatio()),
43  speckleWindowSize_(Parameters::defaultStereoSGBMSpeckleWindowSize()),
44  speckleRange_(Parameters::defaultStereoSGBMSpeckleRange()),
45  P1_(Parameters::defaultStereoSGBMP1()),
46  P2_(Parameters::defaultStereoSGBMP2()),
47  disp12MaxDiff_(Parameters::defaultStereoSGBMDisp12MaxDiff()),
48  mode_(Parameters::defaultStereoSGBMMode())
49 {
50  this->parseParameters(parameters);
51 }
52 
54 {
55  Parameters::parse(parameters, Parameters::kStereoSGBMBlockSize(), blockSize_);
56  Parameters::parse(parameters, Parameters::kStereoSGBMMinDisparity(), minDisparity_);
57  Parameters::parse(parameters, Parameters::kStereoSGBMNumDisparities(), numDisparities_);
58  Parameters::parse(parameters, Parameters::kStereoSGBMPreFilterCap(), preFilterCap_);
59  Parameters::parse(parameters, Parameters::kStereoSGBMUniquenessRatio(), uniquenessRatio_);
60  Parameters::parse(parameters, Parameters::kStereoSGBMSpeckleWindowSize(), speckleWindowSize_);
61  Parameters::parse(parameters, Parameters::kStereoSGBMSpeckleRange(), speckleRange_);
62  Parameters::parse(parameters, Parameters::kStereoSGBMP1(), P1_);
63  Parameters::parse(parameters, Parameters::kStereoSGBMP2(), P2_);
64  Parameters::parse(parameters, Parameters::kStereoSGBMDisp12MaxDiff(), disp12MaxDiff_);
65  Parameters::parse(parameters, Parameters::kStereoSGBMMode(), mode_);
66 }
67 
69  const cv::Mat & leftImage,
70  const cv::Mat & rightImage) const
71 {
72  UASSERT(!leftImage.empty() && !rightImage.empty());
73  UASSERT(leftImage.cols == rightImage.cols && leftImage.rows == rightImage.rows);
74  UASSERT((leftImage.type() == CV_8UC1 || leftImage.type() == CV_8UC3) && rightImage.type() == CV_8UC1);
75 
76  cv::Mat leftMono;
77  if(leftImage.channels() == 3)
78  {
79  cv::cvtColor(leftImage, leftMono, CV_BGR2GRAY);
80  }
81  else
82  {
83  leftMono = leftImage;
84  }
85 
86  cv::Mat disparity;
87 #if CV_MAJOR_VERSION < 3
88  cv::StereoSGBM stereo(
91  blockSize_,
92  P1_,
93  P2_,
99  mode_==1);
100  stereo(leftMono, rightImage, disparity);
101 #else
102  cv::Ptr<cv::StereoSGBM> stereo = cv::StereoSGBM::create(
105  blockSize_,
106  P1_,
107  P2_,
113  mode_);
114  stereo->compute(leftMono, rightImage, disparity);
115 #endif
116 
117  if(minDisparity_>0)
118  {
119  cv::Mat dst;
120  cv::threshold(disparity, dst, minDisparity_*16, 0, cv::THRESH_TOZERO);
121  disparity = dst;
122  }
123 
124  return disparity;
125 }
126 
127 } /* namespace rtabmap */
rtabmap::StereoSGBM::blockSize_
int blockSize_
Definition: StereoSGBM.h:48
rtabmap::StereoSGBM::parseParameters
virtual void parseParameters(const ParametersMap &parameters)
Definition: StereoSGBM.cpp:53
rtabmap::StereoSGBM::uniquenessRatio_
int uniquenessRatio_
Definition: StereoSGBM.h:52
rtabmap::StereoSGBM::P1_
int P1_
Definition: StereoSGBM.h:55
StereoSGBM.h
rtabmap::ParametersMap
std::map< std::string, std::string > ParametersMap
Definition: Parameters.h:43
rtabmap::Parameters::parse
static bool parse(const ParametersMap &parameters, const std::string &key, bool &value)
Definition: Parameters.cpp:500
rtabmap::StereoSGBM::preFilterCap_
int preFilterCap_
Definition: StereoSGBM.h:51
rtabmap::StereoSGBM::speckleRange_
int speckleRange_
Definition: StereoSGBM.h:54
rtabmap::StereoDense
Definition: StereoDense.h:38
rtabmap::StereoSGBM::speckleWindowSize_
int speckleWindowSize_
Definition: StereoSGBM.h:53
rtabmap::StereoSGBM::mode_
int mode_
Definition: StereoSGBM.h:58
rtabmap::StereoSGBM::minDisparity_
int minDisparity_
Definition: StereoSGBM.h:49
UASSERT
#define UASSERT(condition)
rtabmap::Parameters
Definition: Parameters.h:170
rtabmap::StereoSGBM::computeDisparity
virtual cv::Mat computeDisparity(const cv::Mat &leftImage, const cv::Mat &rightImage) const
Definition: StereoSGBM.cpp:68
rtabmap::StereoSGBM::disp12MaxDiff_
int disp12MaxDiff_
Definition: StereoSGBM.h:57
ULogger.h
ULogger class and convenient macros.
rtabmap::StereoSGBM::numDisparities_
int numDisparities_
Definition: StereoSGBM.h:50
rtabmap::StereoSGBM::P2_
int P2_
Definition: StereoSGBM.h:56
dst
char * dst
Definition: lz4.h:354
rtabmap
Definition: CameraARCore.cpp:35
rtabmap::StereoSGBM::StereoSGBM
StereoSGBM(const ParametersMap &parameters=ParametersMap())
Definition: StereoSGBM.cpp:36


rtabmap
Author(s): Mathieu Labbe
autogenerated on Thu Jul 25 2024 02:50:22