title: OpenCV undistort
body: <p>I'm using <code>OpenCV</code> (2.4.8) on Windows 7 and I'm trying to calibrate and undistort images taken from a GoPro camera.</p>
  
  <p>The calibration is done using the live option on the GoPro via WiFi.</p>
  
  <p>I'm using this code (taken from <a href="http://www.aishack.in/2010/07/calibrating-undistorting-with-opencv-in-c-oh-yeah/" rel="nofollow noreferrer">Here</a> and modified a bit by me):</p>
  
  <pre><code>int numBoards = 10;
  int numCornersHor = 9;
  int numCornersVer = 6;
  
  int numSquares = numCornersHor * numCornersVer;
  Size board_sz = Size(numCornersHor, numCornersVer);
  
  vector&lt;vector&lt;Point3f&gt;&gt; object_points;
  vector&lt;vector&lt;Point2f&gt;&gt; image_points;
  
  vector&lt;Point2f&gt; corners;
  int successes=0;
  
  Mat image;
  Mat gray_image;
  *videoCapture &gt;&gt; image;
  cvtColor(image, gray_image, CV_BGR2GRAY);
  vector&lt;Point3f&gt; obj;
  for(int j=0;j&lt;numSquares;j++)
  {
      obj.push_back(Point3f(j/numCornersHor, j%numCornersHor, 0.0f));
  }
  
  while(successes&lt;numBoards)
  {
      bool found = findChessboardCorners(image, board_sz, corners, CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_FILTER_QUADS);
  
      if(found)
      {
          cornerSubPix(gray_image, corners, Size(11, 11), Size(-1, -1), TermCriteria(CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 30, 0.1));
          drawChessboardCorners(gray_image, board_sz, corners, found);
          imshow("Image", image);
          imshow("Grayscale Image", gray_image);
      }
  
  
      *videoCapture &gt;&gt; image;
  
      int key = waitKey(1);
      if(key==27)
          return false;
  
      if(key==' ' &amp;&amp; found!=0)
      {
          image_points.push_back(corners);
          object_points.push_back(obj);
          printf("Snap stored!\n");
  
          successes++;
  
          if(successes&gt;=numBoards)
              break;
      }
  }
  //cameraMatrix = Mat(3, 3, CV_32FC1);
  FileStorage fs(CALIBRATION_XML_FILE, FileStorage::WRITE);
  vector&lt;Mat&gt; rvecs;
  vector&lt;Mat&gt; tvecs;
  //cameraMatrix.ptr&lt;float&gt;(0)[0] = 1;
  //cameraMatrix.ptr&lt;float&gt;(1)[1] = 1;
  cv::calibrateCamera(object_points, image_points, image.size(), cameraMatrix, distCoeffs, rvecs, tvecs);
  time_t rawtime; time(&amp;rawtime);
  
  fs &lt;&lt; "calibrationDate" &lt;&lt; asctime(localtime(&amp;rawtime));
  fs &lt;&lt; "cameraMatrix" &lt;&lt; cameraMatrix;
  fs &lt;&lt; "distCoeffs" &lt;&lt; distCoeffs;
  fs.release();
  Mat imageUndistorted;
  while(true)
  {
      *videoCapture &gt;&gt; image;
      undistort(image, imageUndistorted, cameraMatrix, distCoeffs);
  
      imshow("Original", image);
      imshow("Undistorted", imageUndistorted);
  
      int key = waitKey(1);
      if(key==27)
          return true;
  }
  </code></pre>
  
  <p>and after the calibration ends, the <code>Undistoreted</code> window shows the undistorted video feed very well.</p>
  
  <p>These matrices are saved to an XML file via OpenCV's FileStorage.</p>
  
  <p>I then, after closing the application and running it in another mode in order to distort images saved before (also taken with the same camera).</p>
  
  <p>First I'm reading the XML file (I printed the matrices after loading, they match the XML file so the loading is fine) using again the FileStorage class</p>
  
  <p>I'm then using this code to undistort the images:</p>
  
  <pre><code>Mat outMat;
  undistort(inputImage, outMat, cameraMatrix, distCoeffs);
  return outMat;
  </code></pre>
  
  <p>But the results are horrible.</p>
  
  <p>Input image:
  <img src="https://i.stack.imgur.com/njp28.jpg" alt="enter image description here"></p>
  
  <p>Undistorted image:
  <img src="https://i.stack.imgur.com/Shq3k.jpg" alt="enter image description here"></p>
  
  <p>Am I missing something that needs to be done before calling <code>undistort</code>?</p>
  
  <p>Thanks</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>If it matters, this is the XML file that was saved using the FileStorage:</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;opencv_storage&gt;
  &lt;calibrationDate&gt;"Fri May 09 17:12:58 2014&amp;#x0a;"&lt;/calibrationDate&gt;
  &lt;cameraMatrix type_id="opencv-matrix"&gt;
      &lt;rows&gt;3&lt;/rows&gt;
      &lt;cols&gt;3&lt;/cols&gt;
      &lt;dt&gt;d&lt;/dt&gt;
      &lt;data&gt;
          2.2187712262636470e+002 0. 2.2825108444773281e+002 0.
          2.1495762743910956e+002 1.0916298701649789e+002 0. 0. 1.&lt;/data&gt;&lt;/cameraMatrix&gt;
  &lt;distCoeffs type_id="opencv-matrix"&gt;
      &lt;rows&gt;1&lt;/rows&gt;
      &lt;cols&gt;5&lt;/cols&gt;
      &lt;dt&gt;d&lt;/dt&gt;
      &lt;data&gt;
          -3.0914887115528705e-001 1.1236389287266597e-001
           2.0789692021046750e-002 -2.6151676079193696e-003
          -1.7270919007796915e-002&lt;/data&gt;&lt;/distCoeffs&gt;
  &lt;/opencv_storage&gt;
  </code></pre>