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


rtabmap
Author(s): Mathieu Labbe
autogenerated on Wed Jun 5 2019 22:43:40