StereoBM.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 
36 StereoBM::StereoBM(int blockSize, int numDisparities) :
37  blockSize_(blockSize),
38  minDisparity_(Parameters::defaultStereoBMMinDisparity()),
39  numDisparities_(numDisparities),
40  preFilterSize_(Parameters::defaultStereoBMPreFilterSize()),
41  preFilterCap_(Parameters::defaultStereoBMPreFilterCap()),
42  uniquenessRatio_(Parameters::defaultStereoBMUniquenessRatio()),
43  textureThreshold_(Parameters::defaultStereoBMTextureThreshold()),
44  speckleWindowSize_(Parameters::defaultStereoBMSpeckleWindowSize()),
45  speckleRange_(Parameters::defaultStereoBMSpeckleRange()),
46  disp12MaxDiff_(Parameters::defaultStereoBMDisp12MaxDiff())
47 {
48 }
49 StereoBM::StereoBM(const ParametersMap & parameters) :
50  StereoDense(parameters),
51  blockSize_(Parameters::defaultStereoBMBlockSize()),
52  minDisparity_(Parameters::defaultStereoBMMinDisparity()),
53  numDisparities_(Parameters::defaultStereoBMNumDisparities()),
54  preFilterSize_(Parameters::defaultStereoBMPreFilterSize()),
55  preFilterCap_(Parameters::defaultStereoBMPreFilterCap()),
56  uniquenessRatio_(Parameters::defaultStereoBMUniquenessRatio()),
57  textureThreshold_(Parameters::defaultStereoBMTextureThreshold()),
58  speckleWindowSize_(Parameters::defaultStereoBMSpeckleWindowSize()),
59  speckleRange_(Parameters::defaultStereoBMSpeckleRange()),
60  disp12MaxDiff_(Parameters::defaultStereoBMDisp12MaxDiff())
61 {
62  this->parseParameters(parameters);
63 }
64 
65 void StereoBM::parseParameters(const ParametersMap & parameters)
66 {
67  Parameters::parse(parameters, Parameters::kStereoBMBlockSize(), blockSize_);
68  Parameters::parse(parameters, Parameters::kStereoBMMinDisparity(), minDisparity_);
69  Parameters::parse(parameters, Parameters::kStereoBMNumDisparities(), numDisparities_);
70  Parameters::parse(parameters, Parameters::kStereoBMPreFilterSize(), preFilterSize_);
71  Parameters::parse(parameters, Parameters::kStereoBMPreFilterCap(), preFilterCap_);
72  Parameters::parse(parameters, Parameters::kStereoBMUniquenessRatio(), uniquenessRatio_);
73  Parameters::parse(parameters, Parameters::kStereoBMTextureThreshold(), textureThreshold_);
74  Parameters::parse(parameters, Parameters::kStereoBMSpeckleWindowSize(), speckleWindowSize_);
75  Parameters::parse(parameters, Parameters::kStereoBMSpeckleRange(), speckleRange_);
76  Parameters::parse(parameters, Parameters::kStereoBMDisp12MaxDiff(), disp12MaxDiff_);
77 }
78 
80  const cv::Mat & leftImage,
81  const cv::Mat & rightImage) const
82 {
83  UASSERT(!leftImage.empty() && !rightImage.empty());
84  UASSERT(leftImage.cols == rightImage.cols && leftImage.rows == rightImage.rows);
85  UASSERT((leftImage.type() == CV_8UC1 || leftImage.type() == CV_8UC3) && rightImage.type() == CV_8UC1);
86 
87  cv::Mat leftMono;
88  if(leftImage.channels() == 3)
89  {
90  cv::cvtColor(leftImage, leftMono, CV_BGR2GRAY);
91  }
92  else
93  {
94  leftMono = leftImage;
95  }
96 
97  cv::Mat disparity;
98 #if CV_MAJOR_VERSION < 3
99  cv::StereoBM stereo(cv::StereoBM::BASIC_PRESET);
100  stereo.state->SADWindowSize = blockSize_;
101  stereo.state->minDisparity = minDisparity_;
102  stereo.state->numberOfDisparities = numDisparities_;
103  stereo.state->preFilterSize = preFilterSize_;
104  stereo.state->preFilterCap = preFilterCap_;
105  stereo.state->uniquenessRatio = uniquenessRatio_;
106  stereo.state->textureThreshold = textureThreshold_;
107  stereo.state->speckleWindowSize = speckleWindowSize_;
108  stereo.state->speckleRange = speckleRange_;
109  stereo(leftMono, rightImage, disparity, CV_16SC1);
110 #else
111  cv::Ptr<cv::StereoBM> stereo = cv::StereoBM::create();
112  stereo->setBlockSize(blockSize_);
113  stereo->setMinDisparity(minDisparity_);
114  stereo->setNumDisparities(numDisparities_);
115  stereo->setPreFilterSize(preFilterSize_);
116  stereo->setPreFilterCap(preFilterCap_);
117  stereo->setUniquenessRatio(uniquenessRatio_);
118  stereo->setTextureThreshold(textureThreshold_);
119  stereo->setSpeckleWindowSize(speckleWindowSize_);
120  stereo->setSpeckleRange(speckleRange_);
121  stereo->setDisp12MaxDiff(disp12MaxDiff_);
122  stereo->compute(leftMono, rightImage, disparity);
123 #endif
124 
125  if(minDisparity_>0)
126  {
127  cv::Mat dst;
128  cv::threshold(disparity, dst, minDisparity_*16, 0, cv::THRESH_TOZERO);
129  disparity = dst;
130  }
131 
132  return disparity;
133 }
134 
135 } /* namespace rtabmap */
rtabmap::StereoBM::computeDisparity
virtual cv::Mat computeDisparity(const cv::Mat &leftImage, const cv::Mat &rightImage) const
Definition: StereoBM.cpp:79
rtabmap::StereoBM::numDisparities_
int numDisparities_
Definition: StereoBM.h:51
rtabmap::StereoBM::speckleWindowSize_
int speckleWindowSize_
Definition: StereoBM.h:56
rtabmap::ParametersMap
std::map< std::string, std::string > ParametersMap
Definition: Parameters.h:43
rtabmap::StereoBM::parseParameters
virtual void parseParameters(const ParametersMap &parameters)
Definition: StereoBM.cpp:65
rtabmap::StereoBM::StereoBM
StereoBM(int blockSize, int numDisparities)
Definition: StereoBM.cpp:36
rtabmap::Parameters::parse
static bool parse(const ParametersMap &parameters, const std::string &key, bool &value)
Definition: Parameters.cpp:500
rtabmap::StereoBM::blockSize_
int blockSize_
Definition: StereoBM.h:49
StereoBM.h
rtabmap::StereoDense
Definition: StereoDense.h:38
rtabmap::StereoBM::textureThreshold_
int textureThreshold_
Definition: StereoBM.h:55
UASSERT
#define UASSERT(condition)
rtabmap::StereoBM::uniquenessRatio_
int uniquenessRatio_
Definition: StereoBM.h:54
rtabmap::Parameters
Definition: Parameters.h:170
rtabmap::StereoBM::minDisparity_
int minDisparity_
Definition: StereoBM.h:50
ULogger.h
ULogger class and convenient macros.
rtabmap::StereoBM::speckleRange_
int speckleRange_
Definition: StereoBM.h:57
rtabmap::StereoBM::disp12MaxDiff_
int disp12MaxDiff_
Definition: StereoBM.h:58
rtabmap::StereoBM::preFilterCap_
int preFilterCap_
Definition: StereoBM.h:53
dst
char * dst
Definition: lz4.h:354
rtabmap
Definition: CameraARCore.cpp:35
rtabmap::StereoBM::preFilterSize_
int preFilterSize_
Definition: StereoBM.h:52


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