Search code examples
c++opencvruntime-errordisparity-mapping

opencv standard disparity map not working


I can't seem to get any sort of depth image using the standard opencv function (stereoBM).

I tried:

Mat disp, disp8;

StereoBM *sbm = StereoBM::create(16, 2);
sbm->setDisp12MaxDiff(1);
sbm->setSpeckleRange(8);
sbm->setSpeckleWindowSize(0);
sbm->setUniquenessRatio(0);
sbm->setTextureThreshold(507);
sbm->setMinDisparity(-39);
sbm->setPreFilterCap(61);
sbm->setPreFilterSize(5);
sbm->compute(imgLeft, imgRight, disp);
normalize(disp, disp8, 0, 255, CV_MINMAX, CV_8U);

cv::imshow("disp", disp8);

which compiles but chucks a load of errors. Not sure if I am using the abstract class right?

Thanks


Solution

  • Here my function working. I hope it helps you.

    cv::Mat Arritmic::DepthMap(cv::Mat &imageL, cv::Mat &imageR)
    {
    
        /// DISPARITY MAP AND DEPTH MAP
        cv::Mat left_for_matcher, right_for_matcher;
        cv::Mat left_disp,right_disp;
        cv::Mat filtered_disp;
        cv::Mat conf_map =  cv::Mat(imageL.rows, imageL.cols, CV_8U);
        conf_map =  cv::Scalar(255);
        cv::Rect ROI;
        int max_disp = 16; // n*16
        int wsize = 15;
    
    
    
    
       // Perform matching and create the filter instance
       /* I am using StereoBM for faster processing. If speed is not critical, 
       though, StereoSGBM would provide better quality.
       The filter instance is created by providing the StereoMatcher instance
       that we intend to use. Another matcher instance is returned by the
       createRightMatcher function. These two matcher instances are then used
       to compute disparity maps both for the left and right views, that are
       required by the filter. */
    
        cv::Ptr<cv::ximgproc::DisparityWLSFilter> wls_filter;
    
        cv::Ptr<cv::StereoBM >left_matcher = cv::StereoBM::create(max_disp,wsize);
        wls_filter = cv::ximgproc::createDisparityWLSFilter(left_matcher);
        cv::Ptr<cv::StereoMatcher> right_matcher = cv::ximgproc::createRightMatcher(left_matcher);
        cv::cvtColor(imageL,  left_for_matcher,  cv::COLOR_BGR2GRAY);
        cv::cvtColor(imageR, right_for_matcher, cv::COLOR_BGR2GRAY);
    
    
        left_matcher-> compute(left_for_matcher, right_for_matcher,left_disp);
        right_matcher->compute(right_for_matcher,left_for_matcher, right_disp);
    
        // Perform filtering
        /* Disparity maps computed by the respective matcher instances, as
        well as the source left view are passed to the filter. Note that we
        are using the original non-downscaled view to guide the filtering 
        process. 
        The disparity map is automatically upscaled in an edge-aware fashion
        to match the original view resolution. The result is stored in
        filtered_disp. */
    
    
        double lambda = 6000.0;   // hardcode
        double sigma = 2.0;       // hardcode
    
        //! [filtering]
        wls_filter->setLambda(lambda);
        wls_filter->setSigmaColor(sigma);
    
        wls_filter->filter(left_disp, imageL, filtered_disp, right_disp);
    
        //! [filtering]
        conf_map = wls_filter->getConfidenceMap();
    
        // Get the ROI that was used in the last filter call:
        ROI = wls_filter->getROI();
    
          cv::Mat raw_disp_vis;
        cv::ximgproc::getDisparityVis(left_disp,raw_disp_vis, 21.0);
        cv::Mat filtered_disp_vis;
        cv::ximgproc::getDisparityVis(filtered_disp,filtered_disp_vis, 15.0);
    
    
        return raw_disp_vis;  // rerturning de depth map image.
    }