3 #include "fastBilateral.hpp"
4 #include "StereoMatcher.h"
5 #include <opencv2/opencv.hpp>
9 using namespace cv::ximgproc;
12 Rect computeROI2(Size2i src_sz, Ptr<StereoMatcher> matcher_instance)
14 int min_disparity = matcher_instance->getMinDisparity();
15 int num_disparities = matcher_instance->getNumDisparities();
16 int block_size = matcher_instance->getBlockSize();
18 int bs2 = block_size/2;
19 int minD = min_disparity, maxD = min_disparity + num_disparities - 1;
21 int xmin = maxD + bs2;
22 int xmax = src_sz.width + minD - bs2;
24 int ymax = src_sz.height - bs2;
26 Rect r(xmin, ymin, xmax - xmin, ymax - ymin);
32 void StereoMatcherNew::setParameters(
int minDisparity,
int numberOfDisparities,
int SADWindowSize,
33 int disp12MaxDiff,
int preFilterCap,
int uniquenessRatio,
34 int speckleWindowSize,
int speckleRange,
double sigmaColorBLF,
35 double sigmaSpaceBLF,
double wls_lambda,
double wls_sigma,
36 SM_BLF_FILTER BLFfiltering, SM_WLS_FILTER WLSfiltering,
37 SM_MATCHING_ALG stereo_matching)
39 this->stereo_parameters.minDisparity = minDisparity;
40 this->stereo_parameters.numberOfDisparities = numberOfDisparities;
41 this->stereo_parameters.SADWindowSize = SADWindowSize;
42 this->stereo_parameters.disp12MaxDiff = disp12MaxDiff;
43 this->stereo_parameters.preFilterCap = preFilterCap;
44 this->stereo_parameters.uniquenessRatio = uniquenessRatio;
45 this->stereo_parameters.speckleWindowSize = speckleWindowSize;
46 this->stereo_parameters.speckleRange = speckleRange;
47 this->stereo_parameters.sigmaColorBLF = sigmaColorBLF;
48 this->stereo_parameters.sigmaSpaceBLF = sigmaSpaceBLF;
49 this->stereo_parameters.wls_lambda = wls_lambda;
50 this->stereo_parameters.wls_sigma = wls_sigma;
51 this->stereo_parameters.BLFfiltering = BLFfiltering;
52 this->stereo_parameters.WLSfiltering = WLSfiltering;
53 this->stereo_parameters.stereo_matching = stereo_matching;
57 void StereoMatcherNew::compute()
62 this->stereo->updateMappings();
63 this->stereo->rectifyImages();
67 switch(this->stereo_parameters.stereo_matching)
69 case SM_MATCHING_ALG::SGBM_OPENCV:
72 case SM_MATCHING_ALG::SGBM_CUDA:
73 this->matchSGBMCUDA();
75 case SM_MATCHING_ALG::LIBELAS:
82 void StereoMatcherNew::filterBLF(
string kind =
"base")
85 cv::Mat input = this->getDisparity(kind);
86 cv::Mat input16 = this->getDisparity16(kind);
88 switch(this->stereo_parameters.BLFfiltering)
90 case SM_BLF_FILTER::BLF_ORIGINAL:
93 this->disparity16_blf,
94 this->stereo_parameters.sigmaColorBLF,
95 this->stereo_parameters.sigmaSpaceBLF);
97 getDisparityVis(this->disparity16_blf, this->disparity_blf, 3);
101 case SM_BLF_FILTER::BLF_CUDA:
103 cv::Mat grayL = this->stereo->getLRectified();
104 cv::cvtColor(grayL, grayL, CV_BGR2GRAY);
106 imageGpu.upload(grayL);
107 gpuDisp.upload(input16);
109 pCudaBilFilter->apply(gpuDisp, imageGpu, filtGpu);
111 filtGpu.download(this->disparity16_blf);
113 getDisparityVis(this->disparity16_blf, this->disparity_blf, 3);
117 case SM_BLF_FILTER::BLF_DISABLED:
120 this->disparity_blf = input.clone();
121 this->disparity16_blf = input16.clone();
127 void StereoMatcherNew::filterWLS(
string kind =
"base")
129 cv::Mat input = this->getDisparity(kind);
130 cv::Mat input16 = this->getDisparity16(kind);
133 if(this->stereo_parameters.WLSfiltering != SM_WLS_FILTER::WLS_DISABLED)
136 Ptr<StereoSGBM> sgbm =cv::StereoSGBM::create(this->stereo_parameters.minDisparity,
137 this->stereo_parameters.numberOfDisparities,
138 this->stereo_parameters.SADWindowSize,
139 8*3*this->stereo_parameters.SADWindowSize*this->stereo_parameters.SADWindowSize,
140 32*3*this->stereo_parameters.SADWindowSize*this->stereo_parameters.SADWindowSize,
141 this->stereo_parameters.disp12MaxDiff,
142 this->stereo_parameters.preFilterCap,
143 this->stereo_parameters.uniquenessRatio,
144 this->stereo_parameters.speckleWindowSize,
145 this->stereo_parameters.speckleRange,
146 this->useBestDisp?StereoSGBM::MODE_HH:StereoSGBM::MODE_SGBM);
148 cv::Mat left_rect = this->stereo->getLRectified();
150 switch(this->stereo_parameters.WLSfiltering)
152 case SM_WLS_FILTER::WLS_ENABLED:
154 wls_filter->setLambda(this->stereo_parameters.wls_lambda);
155 wls_filter->setSigmaColor(this->stereo_parameters.wls_sigma);
156 wls_filter->setDepthDiscontinuityRadius((
int)ceil(0.5*this->stereo_parameters.SADWindowSize));
157 ROI = computeROI2(left_rect.size(),sgbm);
159 wls_filter->filter(input,left_rect,this->disparity_wls,Mat(),ROI);
160 wls_filter->filter(input16,left_rect,this->disparity16_wls,Mat(),ROI);
164 case SM_WLS_FILTER::WLS_LRCHECK:
166 if(this->stereo_matching == SM_MATCHING_ALG::SGBM_OPENCV)
169 wls_filter->setLambda(this->stereo_parameters.wls_lambda);
170 wls_filter->setSigmaColor(this->stereo_parameters.wls_sigma);
174 Ptr<StereoMatcher> right_matcher = cv::ximgproc::createRightMatcher(sgbm);
176 right_matcher->compute(this->stereo->getRRectified(),this->stereo->getLRectified(), right_disp);
178 wls_filter->filter(input,left_rect,this->disparity_wls,right_disp);
179 wls_filter->filter(input16,left_rect,this->disparity16_wls,right_disp);
183 this->disparity_wls = input.clone();
184 this->disparity16_wls = input16.clone();
195 this->disparity_wls = input.clone();
196 this->disparity16_wls = input16.clone();
201 cv::Mat StereoMatcherNew::getDisparity(
string kind=
"base")
207 return this->disparity;
208 else if(kind ==
"blf")
209 return this->disparity_blf;
210 else if(kind ==
"wls")
211 return this->disparity_wls;
214 std::cout <<
"[StereoMatcherNew] !! Disparity kind " << kind <<
" not found, returning BASE disparity." << std::endl;
215 return this->disparity;
221 cv::Mat StereoMatcherNew::getDisparity16(
string kind=
"base")
227 return this->disparity16;
228 else if(kind ==
"blf")
229 return this->disparity16_blf;
230 else if(kind ==
"wls")
231 return this->disparity16_wls;
234 std::cout <<
"[StereoMatcherNew] !! Disparity16 kind " << kind <<
" not found, returning BASE disparity16." << std::endl;
235 return this->disparity16;
240 void StereoMatcherNew::setAlgorithm(
string name)
246 this->stereo_parameters.stereo_matching = SM_MATCHING_ALG::SGBM_OPENCV;
247 else if(name ==
"sgbm_cuda")
248 this->stereo_parameters.stereo_matching = SM_MATCHING_ALG::SGBM_CUDA;
249 else if(name ==
"libelas")
250 this->stereo_parameters.stereo_matching = SM_MATCHING_ALG::LIBELAS;
253 std::cout <<
"[StereoMatcherNew] !! Stereo Matching algorithm " << name <<
" not found, defaulting to SGBM." << std::endl;
254 this->stereo_parameters.stereo_matching = SM_MATCHING_ALG::SGBM_OPENCV;
259 StereoMatcherNew::StereoMatcherNew(yarp::os::ResourceFinder &rf,
StereoCamera * stereo)
264 this->stereo = stereo;
265 this->wls_filter = cv::ximgproc::createDisparityWLSFilterGeneric(
false);
267 this->initCUDAbilateralFilter();
271 void StereoMatcherNew::updateCUDAParams()
277 this->cuda_params.preFilterCap = this->stereo_parameters.preFilterCap;
278 this->cuda_params.BlockSize = this->stereo_parameters.SADWindowSize;
279 this->cuda_params.P1 = 8 * this->cuda_params.BlockSize * this->cuda_params.BlockSize;
280 this->cuda_params.P2 = 32 * this->cuda_params.BlockSize * this->cuda_params.BlockSize;
281 this->cuda_params.uniquenessRatio = this->stereo_parameters.uniquenessRatio;
282 this->cuda_params.disp12MaxDiff = this->stereo_parameters.disp12MaxDiff;
283 this->cuda_params.numberOfDisparities = this->stereo_parameters.numberOfDisparities;
285 cuda_init(&this->cuda_params);
287 pCudaBilFilter->setSigmaRange(this->stereo_parameters.sigmaColorBLF);
288 pCudaBilFilter->setNumDisparities(this->stereo_parameters.numberOfDisparities);
292 void StereoMatcherNew::initCUDAbilateralFilter()
299 this->pCudaBilFilter = cuda::createDisparityBilateralFilter(this->stereo_parameters.numberOfDisparities, radius, iters);
300 this->pCudaBilFilter->setSigmaRange(this->stereo_parameters.sigmaColorBLF);
304 void StereoMatcherNew::initELAS(yarp::os::ResourceFinder &rf)
309 string elas_string = rf.check(
"elas_setting",Value(
"ROBOTICS")).asString();
311 double disp_scaling_factor = rf.check(
"disp_scaling_factor",Value(1.0)).asFloat64();
313 this->elaswrap =
new elasWrapper(disp_scaling_factor, elas_string);
315 if (rf.check(
"elas_subsampling"))
316 elaswrap->set_subsampling(
true);
318 if (rf.check(
"elas_add_corners"))
319 elaswrap->set_add_corners(
true);
321 elaswrap->set_ipol_gap_width(40);
322 if (rf.check(
"elas_ipol_gap_width"))
323 elaswrap->set_ipol_gap_width(rf.find(
"elas_ipol_gap_width").asInt32());
325 if (rf.check(
"elas_support_threshold"))
326 elaswrap->set_support_threshold(rf.find(
"elas_support_threshold").asFloat64());
328 if(rf.check(
"elas_gamma"))
329 elaswrap->set_gamma(rf.find(
"elas_gamma").asFloat64());
331 if (rf.check(
"elas_sradius"))
332 elaswrap->set_sradius(rf.find(
"elas_sradius").asFloat64());
334 if (rf.check(
"elas_match_texture"))
335 elaswrap->set_match_texture(rf.find(
"elas_match_texture").asInt32());
337 if (rf.check(
"elas_filter_median"))
338 elaswrap->set_filter_median(rf.find(
"elas_filter_median").asBool());
340 if (rf.check(
"elas_filter_adaptive_mean"))
341 elaswrap->set_filter_adaptive_mean(rf.find(
"elas_filter_adaptive_mean").asBool());
375 void StereoMatcherNew::matchLIBELAS()
381 bool success = elaswrap->compute_disparity(this->stereo->getLRectified(), this->stereo->getRRectified(), this->disparity, this->stereo_parameters.numberOfDisparities);
384 map = this->disparity * (255.0 / this->stereo_parameters.numberOfDisparities);
386 this->disparity.convertTo(this->disparity16, CV_16SC1, 16.0);
388 getDisparityVis(this->disparity16, this->disparity, 3);
392 void StereoMatcherNew::matchSGBM()
399 Ptr<StereoSGBM> sgbm =cv::StereoSGBM::create(this->stereo_parameters.minDisparity,
400 this->stereo_parameters.numberOfDisparities,
401 this->stereo_parameters.SADWindowSize,
402 8*3*this->stereo_parameters.SADWindowSize*this->stereo_parameters.SADWindowSize,
403 32*3*this->stereo_parameters.SADWindowSize*this->stereo_parameters.SADWindowSize,
404 this->stereo_parameters.disp12MaxDiff,
405 this->stereo_parameters.preFilterCap,
406 this->stereo_parameters.uniquenessRatio,
407 this->stereo_parameters.speckleWindowSize,
408 this->stereo_parameters.speckleRange,
409 this->useBestDisp?StereoSGBM::MODE_HH:StereoSGBM::MODE_SGBM);
412 sgbm->compute(this->stereo->getLRectified(), this->stereo->getRRectified(), disp);
414 this->disparity16 = disp;
416 getDisparityVis(disp, this->disparity, 3);
420 void StereoMatcherNew::matchSGBMCUDA()
426 cv::Mat grayL = this->stereo->getLRectified();
427 cv::Mat grayR = this->stereo->getRRectified();
429 cv::cvtColor(grayL, grayL, CV_BGR2GRAY);
430 cv::cvtColor(grayR, grayR, CV_BGR2GRAY);
432 outputDm = zy_remap(grayL, grayR);
435 outputDm.convertTo(map,CV_32FC1,255/(this->stereo_parameters.numberOfDisparities*16.));
437 getDisparityVis(outputDm, this->disparity, 3);
439 this->disparity16 = outputDm;
443 StereoMatcherNew::~StereoMatcherNew()
The base class defining stereo camera.
void bilateralFilter(cv::InputArray src, cv::OutputArray dst, double sigmaColor, double sigmaSpace)
Implementation.