title: InitUndistortRectifyMap and Remap
body: <p>I am currently writing an openCV program for a pair of stereo cameras.
  Camera calibration as well as Stereo Calibration are done.</p>
  
  <p>The next step is to find a feature's position in space from the 2 images I get. That's why I have to Stereo Rectify the images and make my calculations afterwards. </p>
  
  <p>The problem I am facing with <code>initUndistortRectifyMap</code> is the following:</p>
  
  <p>-If I pass R1 or R2 calculated by <code>stereoRectify()</code> to <code>initUndistortRectifyMap()</code> I get black images after remapping.</p>
  
  <p>-If I pass r (an empty matrix) to <code>initUndistortRectifyMap()</code> I get unrectified images after remapping. The images I get are a little distorted though. </p>
  
  <p>I need to pass R1, and R2, to <code>initUndistortRectifyMap()</code> for rectifying the 2 cameras, otherwise when passing an empty matrix the stereo heads won't be rotated into the same plane.  </p>
  
  <p>Following is my code:</p>
  
  <pre><code>stereoRectify(intrinsic[0], distCoeffs[0], intrinsic[1], distCoeffs[1], imageSize, 
      R, T_Stereo, R1, R2, newP1, newP2, Q, CV_CALIB_ZERO_DISPARITY, -1, imageSize);
  
  if (x_Cam.GetSerial()=="4002678487")
  {
      initUndistortRectifyMap(intrinsic[0], distCoeffs[0], R1, newP1, imageSize,        
          CV_16SC2 , mapx1,  mapy1);
      remap(x_Image, imageRectified[0],mapx1, mapy1, INTER_LINEAR);
  
      return imageRectified[0];
  }   
  
  if (x_Cam.GetSerial()=="4002702131")
  {
      //flip(in, in, -1);
      initUndistortRectifyMap(intrinsic[1], distCoeffs[1], R2, newP2, imageSize, 
          CV_16SC2 , mapx2,  mapy2);
      remap(x_Image, imageRectified[1],mapx2, mapy2, INTER_LINEAR, BORDER_CONSTANT, 0);
  
      return imageRectified[1];
  }
  </code></pre>
  
  <p>I checked all the matrix values going in to <code>stereoRectify()</code> and they are correct. The rotation matrices R1, and R2 seem to be correct as well.  I am just getting black images as output.</p>
  
  <p>I tried passing rubbish values into <code>InitUndistortRectifyMap()</code> for R1 and R2 (R1*R2 for example) to see the effect simply, and I did get weird results but not black images. </p>