title: correcting fisheye distortion programmatically
body: <blockquote>
    <p><strong><em>BOUNTY STATUS UPDATE:</em></strong></p>
  </blockquote>
  
  <p><strong><a href="https://stackoverflow.com/questions/2477774/correcting-fisheye-distortion-programmatically/2502276#2502276">I discovered how to map a linear lens</a></strong>, from <code>destination</code> coordinates to <code>source</code> coordinates.<br></p>
  
  <p><strong>How do you calculate the radial distance from the centre to go from fisheye to rectilinear?</strong></p>
  
  <ul>
  <li><p><strong>1).</strong> I actually struggle to reverse it, and to map source coordinates to destination coordinates.  What is the inverse, in code in the style of the converting functions I posted?
  <img src="https://i.stack.imgur.com/SIhfE.jpg" width="500" height="194" /><br></p></li>
  <li><p><strong>2).</strong> I also see that my undistortion is imperfect on some lenses - presumably those that are not strictly linear.  What is the equivalent to-and-from source-and-destination coordinates for those lenses?  Again, more code than just mathematical formulae please... 
  <img src="https://i.stack.imgur.com/ickIZ.jpg" width="500" height="194" /></p></li>
  </ul>
  
  <hr>
  
  <blockquote>
    <p><strong><em>Question as originally stated:</em></strong></p>
  </blockquote>
  
  <p>I have some points that describe positions in a picture taken with a fisheye lens.</p>
  
  <p>I want to convert these points to rectilinear coordinates.  I want to undistort the image.</p>
  
  <p>I've found <a href="http://wiki.panotools.org/Fisheye_Projection" rel="noreferrer">this description</a> of how to generate a fisheye effect, but not how to reverse it.</p>
  
  <p>There's also a <a href="http://photo.net/learn/fisheye/" rel="noreferrer">blog post</a> that describes how to use tools to do it; these pictures are from that:</p>
  
  <p><strong>(1)</strong> : <code>SOURCE</code> <a href="http://photo.net/learn/fisheye/fe01.jpg" rel="noreferrer">Original photo link</a> <br>
  <img src="https://i.stack.imgur.com/qLNQV.jpg" width="500" height="334" /><br>
  Input : <em>Original image with fish-eye distortion to fix.</em>
  <br></p>
  
  <p><strong>(2)</strong> : <code>DESTINATION</code> <a href="http://photo.net/learn/fisheye/fe04.jpg" rel="noreferrer">Original photo link</a> <br>
  <img src="https://i.stack.imgur.com/ogb9b.jpg" width="500" height="237" /><br>
  Output : <em>Corrected image (technically also with perspective correction, but that's a separate step).</em></p>
  
  <p>How do you calculate the radial distance from the centre to go from fisheye to rectilinear?</p>
  
  <p>My function stub looks like this:</p>
  
  <pre><code>Point correct_fisheye(const Point&amp; p,const Size&amp; img) {
      // to polar
      const Point centre = {img.width/2,img.height/2};
      const Point rel = {p.x-centre.x,p.y-centre.y};
      const double theta = atan2(rel.y,rel.x);
      double R = sqrt((rel.x*rel.x)+(rel.y*rel.y));
      // fisheye undistortion in here please
      //... change R ...
      // back to rectangular
      const Point ret = Point(centre.x+R*cos(theta),centre.y+R*sin(theta));
      fprintf(stderr,"(%d,%d) in (%d,%d) = %f,%f = (%d,%d)\n",p.x,p.y,img.width,img.height,theta,R,ret.x,ret.y);
      return ret;
  }
  </code></pre>
  
  <p>Alternatively, I could somehow convert the image from fisheye to rectilinear before finding the points, but I'm completely befuddled by the <a href="http://opencv.willowgarage.com/documentation/camera_calibration_and_3d_reconstruction.html#initundistortmap" rel="noreferrer">OpenCV documentation</a>.  Is there a straightforward way to do it in OpenCV, and does it perform well enough to do it to a live video feed?</p>