title: bad distance results using stereo camera
body: <p>I'm trying to measure distance in real time from stereo pair to a person detected in the scene. First i calibrated both cameras separately with a 9x6 checkerboard (square size of 59 mm) and i obtained a rms error between 0.15 and 0.19 for both cameras. Using the obtained parameters i calibrated the stereo pair and the rms error was 0.36. Later, I rectified, undistort and remap the stereo pair giving me this result:
  <a href="https://i.stack.imgur.com/cWHCZ.jpg" rel="nofollow noreferrer">rectified and undistorted stereo</a></p>
  
  <p>Done that, I computed stereo correspondence using stereoSGBM. That's how i did:</p>
  
  <pre><code>Mat imgDisp= Mat(frame1.cols, frame1.rows,CV_16S);
  
  cvtColor(frame1, frame1, CV_BGR2GRAY);
  cvtColor(frame2, frame2, CV_BGR2GRAY);
  
  //parameters for stereoSGBM
    stereo.SADWindowSize = 3;
    stereo.numberOfDisparities = 144;
    stereo.preFilterCap = 63;
    stereo.minDisparity = -39;
    stereo.uniquenessRatio = 10;
    stereo.speckleWindowSize = 100;
    stereo.speckleRange = 32;
    stereo.disp12MaxDiff = 1;
    stereo.fullDP = false;
    stereo.P1 = 216;
    stereo.P2 = 864;
  
  double minVal; double maxVal;
  minMaxLoc(imgDisp, &amp;minVal, &amp;maxVal);
  
  return imgDisp; 
  </code></pre>
  
  <p>I attached the result from stereoSGBM here: <a href="https://i.stack.imgur.com/2PDso.jpg" rel="nofollow noreferrer">disparity map</a>.</p>
  
  <p>For detect a person in the scene I used hog + svm (the default people dectector) and tracked that person with optical flow (cvCalcOpticalFlowPyrLK()). Using the disparity map obtained in the stereo correspondence process i obtained the disparity for each corner tracked from one person as follow: </p>
  
  <pre><code>int x= cornersA[k].x;
  int y= cornersA[k].y;
  
  short pixVal= mapaDisp.at&lt;short&gt;(y,x); 
  float dispFeatures= pixVal/ 16.0f;
  </code></pre>
  
  <p>with the disparity for each corner tracked for one person in the scene I computed the maxim disparity and computed the depth in that pixel using the formula ((focal*baseline)/disp):</p>
  
  <pre><code>float Disp =maxDisp_v[p];
  
  cout&lt;&lt; "max disp"&lt;&lt; Disp&lt;&lt;endl;
  float d = ((double)(879.85* 64.32)/(double)(Disp))/10; //distance in cms.
  </code></pre>
  
  <p>** for focal length I calculated the average between fx and fy obtained in the cameras matrix [3x3] parameters: </p>
  
  <blockquote>
    <p>CM1: [9.0472706037497187e+02  0.  3.7829164759284492e+02<br>
            0.  8.4576999835299739e+02 1.8649783393160138e+02 
            0. 0. 1.]</p>
    
    <p>CM2: [9.1390904648169953e+02 0. 3.5700689147467887e+02 0.
        8.5514555697053311e+02 2.1723345133656409e+02 0. 0. 1.]</p>
  </blockquote>
  
  <p>so fx camera1: 904.7; fy camera1: 845.7; fx camera2: 913.9; fy camera2: 855.1</p>
  
  <p>** The result of T[0,0] matrix matched with the baseline that I measure manuallly so I assumed that's correct baseline.</p>
  
  <p>**due to the square size of checkerboard is in mm i assumed that baseline must be in the same unit, that's why I'm put 64.32 mm in baseline.</p>
  
  <p>The result of distance is aprox. 55 cms but the real distance is 300 cms. I have checked many times but the measured distance is still incorrect: <a href="https://i.stack.imgur.com/Kw0c4.jpg" rel="nofollow noreferrer">distanceResult</a></p>
  
  <p>Help me please!, I have no idea what i'm doing wrong.</p>
  
  <p>*** I'm using opencv 2.4.9 in osx system.</p>