参考
https://docs.opencv.org/3.2.0/d3/d14/tutorial_ximgproc_disparity_filtering.html
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#include "test_precomp.hpp"
#include "opencv2/ximgproc/disparity_filter.hpp"
#include <string>
//CV_TEST_MAIN("")
using namespace cv;
using namespace std;
using namespace ximgproc; int main()
{
string left_im = "C:\\Users\\Administrator\\Desktop\\green_tree\\left.bmp";
string right_im = "C:\\Users\\Administrator\\Desktop\\green_tree\\right.bmp";
Mat left = imread(left_im, IMREAD_COLOR);
if (left.empty())
{
cout << "Cannot read image file: " << left_im;
return -;
}
Mat right = imread(right_im, IMREAD_COLOR);
if (right.empty())
{
cout << "Cannot read image file: " << right_im;
return -;
} int max_disp = ;
max_disp /= ;
if (max_disp % != )
max_disp += - (max_disp % );
Mat left_for_matcher, right_for_matcher;
resize(left, left_for_matcher, Size(), 1.0, 1.0);
resize(right, right_for_matcher, Size(), 1.0, 1.0); int wsize = ;
Ptr<StereoBM> left_matcher = StereoBM::create(max_disp, wsize); Ptr<DisparityWLSFilter> wls_filter;
wls_filter = createDisparityWLSFilter(left_matcher); Ptr<StereoMatcher> right_matcher = createRightMatcher(left_matcher);
cvtColor(left_for_matcher, left_for_matcher, COLOR_BGR2GRAY);
cvtColor(right_for_matcher, right_for_matcher, COLOR_BGR2GRAY);
double matching_time = (double)getTickCount();
Mat left_disp, right_disp, filtered_disp;
left_matcher->compute(left_for_matcher, right_for_matcher, left_disp);
right_matcher->compute(right_for_matcher, left_for_matcher, right_disp);
matching_time = ((double)getTickCount() - matching_time) / getTickFrequency(); double lambda = 8000.0;
double sigma = 0.5;
wls_filter->setLambda(lambda);
wls_filter->setSigmaColor(sigma);
double filtering_time = (double)getTickCount();
wls_filter->filter(left_disp, left, filtered_disp, right_disp);
filtering_time = ((double)getTickCount() - filtering_time) / getTickFrequency(); Mat raw_disp_vis;
double vis_mult = 10.0;
getDisparityVis(left_disp, raw_disp_vis, vis_mult);
namedWindow("raw disparity", WINDOW_AUTOSIZE);
imshow("raw disparity", raw_disp_vis);
Mat filtered_disp_vis;
getDisparityVis(filtered_disp, filtered_disp_vis, vis_mult);
namedWindow("filtered disparity", WINDOW_AUTOSIZE);
imshow("filtered disparity", filtered_disp_vis);
waitKey(); return ;
}