title: bad Disparity map using opencv StereoBM
body: <p>I am trying to use StereoBM to get disparity map of two images. I tried some sample code and images. They are working fine. However, when I try my own images, I got very bad map, very noisy.</p>
  
  <p><a href="https://i.stack.imgur.com/H2SZd.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/H2SZd.jpg" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/6wuJr.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/6wuJr.jpg" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/eRfCw.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/eRfCw.jpg" alt="enter image description here"></a></p>
  
  <p>my StereoBM parameters</p>
  
  <pre><code>sbm.state-&gt;SADWindowSize = 25;
  sbm.state-&gt;numberOfDisparities = 128;
  sbm.state-&gt;preFilterSize = 5;
  sbm.state-&gt;preFilterCap = 61;
  sbm.state-&gt;minDisparity = -39;
  sbm.state-&gt;textureThreshold = 507;
  sbm.state-&gt;uniquenessRatio = 0;
  sbm.state-&gt;speckleWindowSize = 0;
  sbm.state-&gt;speckleRange = 8;
  sbm.state-&gt;disp12MaxDiff = 1;
  </code></pre>
  
  <p>My questions are</p>
  
  <ol>
  <li>Any problems about my images?</li>
  <li>Is possible to get good disparity map without calibration of camera? Do I need to rectify images before StereoBM</li>
  </ol>
  
  <p>Thanks.</p>
  
  <p>Here is my code for rectifying image</p>
  
  <pre><code>Mat img_1 = imread( "image1.jpg", CV_LOAD_IMAGE_GRAYSCALE );
  Mat img_2 = imread( "image2.jpg", CV_LOAD_IMAGE_GRAYSCALE );
  
  int minHessian = 430;
  SurfFeatureDetector detector( minHessian );
  std::vector&lt;KeyPoint&gt; keypoints_1, keypoints_2;
  detector.detect( img_1, keypoints_1 );
  detector.detect( img_2, keypoints_2 );
  
  //-- Step 2: Calculate descriptors (feature vectors)
  SurfDescriptorExtractor extractor;
  Mat descriptors_1, descriptors_2;
  extractor.compute( img_1, keypoints_1, descriptors_1 );
  extractor.compute( img_2, keypoints_2, descriptors_2 );
  
  //-- Step 3: Matching descriptor vectors with a brute force matcher
  BFMatcher matcher(NORM_L1, true);   //BFMatcher matcher(NORM_L2);
  
  std::vector&lt; DMatch &gt; matches;
  matcher.match( descriptors_1, descriptors_2, matches );
  
  double max_dist = 0; double min_dist = 100;
  //-- Quick calculation of max and min distances between keypoints
  for( int i = 0; i &lt; matches.size(); i++ )
  { double dist = matches[i].distance;
      if( dist &lt; min_dist ) min_dist = dist;
      if( dist &gt; max_dist ) max_dist = dist;
  }
  
  std::vector&lt; DMatch &gt; good_matches;
  vector&lt;Point2f&gt;imgpts1,imgpts2;
  for( int i = 0; i &lt; matches.size(); i++ )
  {
      if( matches[i].distance &lt;= max(4.5*min_dist, 0.02) ){
          good_matches.push_back( matches[i]);
          imgpts1.push_back(keypoints_1[matches[i].queryIdx].pt);
          imgpts2.push_back(keypoints_2[matches[i].trainIdx].pt);
      }
  
  }
  
  std::vector&lt;uchar&gt; status;
  cv::Mat F = cv::findFundamentalMat(imgpts1, imgpts2, cv::FM_8POINT, 3., 0.99, status);   //FM_RANSAC
  
  Mat H1,H2;
  cv::stereoRectifyUncalibrated(imgpts1, imgpts1, F, img_1.size(), H1, H2);
  
  cv::Mat rectified1(img_1.size(), img_1.type());
  cv::warpPerspective(img_1, rectified1, H1, img_1.size());
  
  cv::Mat rectified2(img_2.size(), img_2.type());
  cv::warpPerspective(img_2, rectified2, H2, img_2.size());
  
  StereoBM sbm;
  sbm.state-&gt;SADWindowSize = 25;
  sbm.state-&gt;numberOfDisparities = 128;
  sbm.state-&gt;preFilterSize = 5;
  sbm.state-&gt;preFilterCap = 61;
  sbm.state-&gt;minDisparity = -39;
  sbm.state-&gt;textureThreshold = 507;
  sbm.state-&gt;uniquenessRatio = 0;
  sbm.state-&gt;speckleWindowSize = 0;
  sbm.state-&gt;speckleRange = 8;
  sbm.state-&gt;disp12MaxDiff = 1;
  
  Mat disp,disp8;
  sbm(rectified1, rectified2, disp);
  </code></pre>
  
  <p>the rectified images and disparity map are here</p>
  
  <p><a href="https://i.stack.imgur.com/hPlWS.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/hPlWS.jpg" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/bqSh5.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/bqSh5.jpg" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/VsyR9.png" rel="noreferrer"><img src="https://i.stack.imgur.com/VsyR9.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/NaBpm.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/NaBpm.jpg" alt="enter image description here"></a></p>