processor.h
Go to the documentation of this file.
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2008, Willow Garage, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of the Willow Garage nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34 #ifndef STEREO_IMAGE_PROC_PROCESSOR_H
35 #define STEREO_IMAGE_PROC_PROCESSOR_H
36 
37 #include <image_proc/processor.h>
39 #include <stereo_msgs/DisparityImage.h>
40 #include <sensor_msgs/PointCloud.h>
41 #include <sensor_msgs/PointCloud2.h>
42 
43 namespace stereo_image_proc {
44 
46 {
49  stereo_msgs::DisparityImage disparity;
50  sensor_msgs::PointCloud points;
51  sensor_msgs::PointCloud2 points2;
52 };
53 
55 {
56 public:
57 
59 #if CV_MAJOR_VERSION == 3
60  {
61  block_matcher_ = cv::StereoBM::create();
62  sg_block_matcher_ = cv::StereoSGBM::create(1, 1, 10);
63 #else
64  : block_matcher_(cv::StereoBM::BASIC_PRESET),
65  sg_block_matcher_()
66  {
67 #endif
68  }
69 
71  {
72  BM, SGBM
73  };
74 
75  enum {
76  LEFT_MONO = 1 << 0,
77  LEFT_RECT = 1 << 1,
78  LEFT_COLOR = 1 << 2,
79  LEFT_RECT_COLOR = 1 << 3,
80  RIGHT_MONO = 1 << 4,
81  RIGHT_RECT = 1 << 5,
82  RIGHT_COLOR = 1 << 6,
83  RIGHT_RECT_COLOR = 1 << 7,
84  DISPARITY = 1 << 8,
85  POINT_CLOUD = 1 << 9,
86  POINT_CLOUD2 = 1 << 10,
87 
88  LEFT_ALL = LEFT_MONO | LEFT_RECT | LEFT_COLOR | LEFT_RECT_COLOR,
89  RIGHT_ALL = RIGHT_MONO | RIGHT_RECT | RIGHT_COLOR | RIGHT_RECT_COLOR,
90  STEREO_ALL = DISPARITY | POINT_CLOUD | POINT_CLOUD2,
91  ALL = LEFT_ALL | RIGHT_ALL | STEREO_ALL
92  };
93 
94  inline
95  StereoType getStereoType() const {return current_stereo_algorithm_;}
96  inline
97  void setStereoType(StereoType type) {current_stereo_algorithm_ = type;}
98 
99  int getInterpolation() const;
100  void setInterpolation(int interp);
101 
102  // Disparity pre-filtering parameters
103 
104  int getPreFilterSize() const;
105  void setPreFilterSize(int size);
106 
107  int getPreFilterCap() const;
108  void setPreFilterCap(int cap);
109 
110  // Disparity correlation parameters
111 
112  int getCorrelationWindowSize() const;
113  void setCorrelationWindowSize(int size);
114 
115  int getMinDisparity() const;
116  void setMinDisparity(int min_d);
117 
118  int getDisparityRange() const;
119  void setDisparityRange(int range); // Number of pixels to search
120 
121  // Disparity post-filtering parameters
122 
123  int getTextureThreshold() const;
124  void setTextureThreshold(int threshold);
125 
126  float getUniquenessRatio() const;
127  void setUniquenessRatio(float ratio);
128 
129  int getSpeckleSize() const;
130  void setSpeckleSize(int size);
131 
132  int getSpeckleRange() const;
133  void setSpeckleRange(int range);
134 
135  // SGBM only
136  int getSgbmMode() const;
137  void setSgbmMode(int fullDP);
138 
139  int getP1() const;
140  void setP1(int P1);
141 
142  int getP2() const;
143  void setP2(int P2);
144 
145  int getDisp12MaxDiff() const;
146  void setDisp12MaxDiff(int disp12MaxDiff);
147 
148  // Do all the work!
149  bool process(const sensor_msgs::ImageConstPtr& left_raw,
150  const sensor_msgs::ImageConstPtr& right_raw,
152  StereoImageSet& output, int flags) const;
153 
154  void processDisparity(const cv::Mat& left_rect, const cv::Mat& right_rect,
156  stereo_msgs::DisparityImage& disparity) const;
157 
158  void processPoints(const stereo_msgs::DisparityImage& disparity,
159  const cv::Mat& color, const std::string& encoding,
161  sensor_msgs::PointCloud& points) const;
162  void processPoints2(const stereo_msgs::DisparityImage& disparity,
163  const cv::Mat& color, const std::string& encoding,
165  sensor_msgs::PointCloud2& points) const;
166 
167 private:
169 
170  mutable cv::Mat_<int16_t> disparity16_; // scratch buffer for 16-bit signed disparity image
171 #if CV_MAJOR_VERSION == 3
172  mutable cv::Ptr<cv::StereoBM> block_matcher_; // contains scratch buffers for block matching
173  mutable cv::Ptr<cv::StereoSGBM> sg_block_matcher_;
174 #else
175  mutable cv::StereoBM block_matcher_; // contains scratch buffers for block matching
176  mutable cv::StereoSGBM sg_block_matcher_;
177 #endif
179  // scratch buffer for dense point cloud
180  mutable cv::Mat_<cv::Vec3f> dense_points_;
181 };
182 
183 
185 {
186  return mono_processor_.interpolation_;
187 }
188 
189 inline void StereoProcessor::setInterpolation(int interp)
190 {
191  mono_processor_.interpolation_ = interp;
192 }
193 
194 // For once, a macro is used just to avoid errors
195 #define STEREO_IMAGE_PROC_OPENCV2(GET, SET, TYPE, PARAM) \
196 inline TYPE StereoProcessor::GET() const \
197 { \
198  if (current_stereo_algorithm_ == BM) \
199  return block_matcher_.state->PARAM; \
200  return sg_block_matcher_.PARAM; \
201 } \
202  \
203 inline void StereoProcessor::SET(TYPE param) \
204 { \
205  block_matcher_.state->PARAM = param; \
206  sg_block_matcher_.PARAM = param; \
207 }
208 
209 #define STEREO_IMAGE_PROC_OPENCV3(GET, SET, TYPE, GET_OPENCV, SET_OPENCV) \
210 inline TYPE StereoProcessor::GET() const \
211 { \
212  if (current_stereo_algorithm_ == BM) \
213  return block_matcher_->GET_OPENCV(); \
214  return sg_block_matcher_->GET_OPENCV(); \
215 } \
216 \
217 inline void StereoProcessor::SET(TYPE param) \
218 { \
219  block_matcher_->SET_OPENCV(param); \
220  sg_block_matcher_->SET_OPENCV(param); \
221 }
222 
223 #if CV_MAJOR_VERSION == 3
224 STEREO_IMAGE_PROC_OPENCV3(getPreFilterCap, setPreFilterCap, int, getPreFilterCap, setPreFilterCap)
225 STEREO_IMAGE_PROC_OPENCV3(getCorrelationWindowSize, setCorrelationWindowSize, int, getBlockSize, setBlockSize)
226 STEREO_IMAGE_PROC_OPENCV3(getMinDisparity, setMinDisparity, int, getMinDisparity, setMinDisparity)
227 STEREO_IMAGE_PROC_OPENCV3(getDisparityRange, setDisparityRange, int, getNumDisparities, setNumDisparities)
228 STEREO_IMAGE_PROC_OPENCV3(getUniquenessRatio, setUniquenessRatio, float, getUniquenessRatio, setUniquenessRatio)
229 STEREO_IMAGE_PROC_OPENCV3(getSpeckleSize, setSpeckleSize, int, getSpeckleWindowSize, setSpeckleWindowSize)
230 STEREO_IMAGE_PROC_OPENCV3(getSpeckleRange, setSpeckleRange, int, getSpeckleRange, setSpeckleRange)
231 #else
232 STEREO_IMAGE_PROC_OPENCV2(getPreFilterCap, setPreFilterCap, int, preFilterCap)
233 STEREO_IMAGE_PROC_OPENCV2(getCorrelationWindowSize, setCorrelationWindowSize, int, SADWindowSize)
234 STEREO_IMAGE_PROC_OPENCV2(getMinDisparity, setMinDisparity, int, minDisparity)
235 STEREO_IMAGE_PROC_OPENCV2(getDisparityRange, setDisparityRange, int, numberOfDisparities)
236 STEREO_IMAGE_PROC_OPENCV2(getUniquenessRatio, setUniquenessRatio, float, uniquenessRatio)
237 STEREO_IMAGE_PROC_OPENCV2(getSpeckleSize, setSpeckleSize, int, speckleWindowSize)
238 STEREO_IMAGE_PROC_OPENCV2(getSpeckleRange, setSpeckleRange, int, speckleRange)
239 #endif
240 
241 #define STEREO_IMAGE_PROC_BM_ONLY_OPENCV2(GET, SET, TYPE, PARAM) \
242 inline TYPE StereoProcessor::GET() const \
243 { \
244  return block_matcher_.state->PARAM; \
245 } \
246 \
247 inline void StereoProcessor::SET(TYPE param) \
248 { \
249  block_matcher_.state->PARAM = param; \
250 }
251 
252 #define STEREO_IMAGE_PROC_SGBM_ONLY_OPENCV2(GET, SET, TYPE, PARAM) \
253 inline TYPE StereoProcessor::GET() const \
254 { \
255  return sg_block_matcher_.PARAM; \
256 } \
257 \
258 inline void StereoProcessor::SET(TYPE param) \
259 { \
260  sg_block_matcher_.PARAM = param; \
261 }
262 
263 #define STEREO_IMAGE_PROC_ONLY_OPENCV3(MEMBER, GET, SET, TYPE, GET_OPENCV, SET_OPENCV) \
264 inline TYPE StereoProcessor::GET() const \
265 { \
266  return MEMBER->GET_OPENCV(); \
267 } \
268 \
269 inline void StereoProcessor::SET(TYPE param) \
270 { \
271  MEMBER->SET_OPENCV(param); \
272 }
273 
274 // BM only
275 #if CV_MAJOR_VERSION == 3
276 STEREO_IMAGE_PROC_ONLY_OPENCV3(block_matcher_, getPreFilterSize, setPreFilterSize, int, getPreFilterSize, setPreFilterSize)
277 STEREO_IMAGE_PROC_ONLY_OPENCV3(block_matcher_, getTextureThreshold, setTextureThreshold, int, getTextureThreshold, setTextureThreshold)
278 #else
279 STEREO_IMAGE_PROC_BM_ONLY_OPENCV2(getPreFilterSize, setPreFilterSize, int, preFilterSize)
280 STEREO_IMAGE_PROC_BM_ONLY_OPENCV2(getTextureThreshold, setTextureThreshold, int, textureThreshold)
281 #endif
282 
283 // SGBM specific
284 #if CV_MAJOR_VERSION == 3
285 // getSgbmMode can return MODE_SGBM = 0, MODE_HH = 1. FullDP == 1 was MODE_HH so we're good
286 STEREO_IMAGE_PROC_ONLY_OPENCV3(sg_block_matcher_, getSgbmMode, setSgbmMode, int, getMode, setMode)
287 STEREO_IMAGE_PROC_ONLY_OPENCV3(sg_block_matcher_, getP1, setP1, int, getP1, setP1)
288 STEREO_IMAGE_PROC_ONLY_OPENCV3(sg_block_matcher_, getP2, setP2, int, getP2, setP2)
289 STEREO_IMAGE_PROC_ONLY_OPENCV3(sg_block_matcher_, getDisp12MaxDiff, setDisp12MaxDiff, int, getDisp12MaxDiff, setDisp12MaxDiff)
290 #else
291 STEREO_IMAGE_PROC_SGBM_ONLY_OPENCV2(getSgbmMode, setSgbmMode, int, fullDP)
292 STEREO_IMAGE_PROC_SGBM_ONLY_OPENCV2(getP1, setP1, int, P1)
293 STEREO_IMAGE_PROC_SGBM_ONLY_OPENCV2(getP2, setP2, int, P2)
294 STEREO_IMAGE_PROC_SGBM_ONLY_OPENCV2(getDisp12MaxDiff, setDisp12MaxDiff, int, disp12MaxDiff)
295 #endif
296 
297 } //namespace stereo_image_proc
298 
299 #endif
#define STEREO_IMAGE_PROC_ONLY_OPENCV3(MEMBER, GET, SET, TYPE, GET_OPENCV, SET_OPENCV)
Definition: processor.h:263
void setStereoType(StereoType type)
Definition: processor.h:97
#define STEREO_IMAGE_PROC_SGBM_ONLY_OPENCV2(GET, SET, TYPE, PARAM)
Definition: processor.h:252
image_proc::Processor mono_processor_
Definition: processor.h:168
cv::Mat_< int16_t > disparity16_
Definition: processor.h:170
#define STEREO_IMAGE_PROC_BM_ONLY_OPENCV2(GET, SET, TYPE, PARAM)
Definition: processor.h:241
image_proc::ImageSet left
Definition: processor.h:47
cv::Mat_< cv::Vec3f > dense_points_
Definition: processor.h:180
#define STEREO_IMAGE_PROC_OPENCV2(GET, SET, TYPE, PARAM)
Definition: processor.h:195
sensor_msgs::PointCloud points
Definition: processor.h:50
StereoType getStereoType() const
Definition: processor.h:95
#define STEREO_IMAGE_PROC_OPENCV3(GET, SET, TYPE, GET_OPENCV, SET_OPENCV)
Definition: processor.h:209
stereo_msgs::DisparityImage disparity
Definition: processor.h:49
image_proc::ImageSet right
Definition: processor.h:48
sensor_msgs::PointCloud2 points2
Definition: processor.h:51
void setInterpolation(int interp)
Definition: processor.h:189


stereo_image_proc
Author(s): Patrick Mihelich, Kurt Konolige, Jeremy Leibs
autogenerated on Fri May 3 2019 02:59:59