title: Can't distort back image in opencv C++
body: <p>I am trying to distort back an image in opencv. At first the image captured with a pinhole camera is undistort thanks to  <code>cv::undistort(raw, undist, cameraMatrix, distCoeffs);</code> which is working. Now I am trying to distort back <code>undist</code> to it's original state with the patch that I found here: <a href="http://code.opencv.org/issues/1387" rel="nofollow">http://code.opencv.org/issues/1387</a> 
  but so far I have not manage to make it work. Here is the code:</p>
  
  <pre><code>void distort(const cv::Mat&amp; src, cv::Mat&amp; dst, const cv::Mat&amp; cameraMatrix, const cv::Mat&amp; distCoeffs)
  {
  
    cv::Mat pixel_locations_src = cv::Mat(src.size(), CV_32FC2);
  
    for (int i = 0; i &lt; src.size().height; i++) {
      for (int j = 0; j &lt; src.size().width; j++) {
        pixel_locations_src.at&lt;cv::Point2f&gt;(i,j) = cv::Point2f(j,i);
      }
    }
  
    cv::Mat fractional_locations_dst = cv::Mat(src.size(), CV_32FC2);
  
    cv::Mat pixel_locations_dst = cv::Mat(src.size(), CV_32FC2);
  
    cv::undistortPoints(pixel_locations_src, pixel_locations_dst, cameraMatrix, distCoeffs);
  
    const float fx = cameraMatrix.at&lt;double&gt;(0,0);
    const float fy = cameraMatrix.at&lt;double&gt;(1,1);
    const float cx = cameraMatrix.at&lt;double&gt;(0,2);
    const float cy = cameraMatrix.at&lt;double&gt;(1,2);
  
    // is there a faster way to do this?
    for (int i = 0; i &lt; fractional_locations_dst.size().height; i++) {
      for (int j = 0; j &lt; fractional_locations_dst.size().width; j++) {
        const float x = fractional_locations_dst.at&lt;cv::Point2f&gt;(i,j).x*fx + cx;
        const float y = fractional_locations_dst.at&lt;cv::Point2f&gt;(i,j).y*fy + cy;
        pixel_locations_dst.at&lt;cv::Point2f&gt;(i,j) = cv::Point2f(x,y);
      }
    }
  
    cv::remap(src, dst, pixel_locations_dst, cv::Mat(), CV_INTER_LINEAR);
  }
  </code></pre>
  
  <p>I tried to pass a RGB image to the function but since <code>undistortPoints</code> takes a 1*N, 2 channels matrix the code will fire an assertion at <code>undistortPoints</code> I don't understand why <code>distort()</code> takes a 1xN matrix as input.</p>
  
  <p>Any light on the topic would be great. Thanks</p>