Not logged in, Join Here! or Log In Below:  
 
News Articles Search    
 

 Home / General Programming / algorithms to correct for lens distortion Account Manager
 
Archive Notice: This thread is old and no longer active. It is here for reference purposes. This thread was created on an older version of the flipcode forums, before the site closed in 2005. Please keep that in mind as you view this thread, as many of the topics and opinions may be outdated.
 
hunguptodry

February 16, 2005, 08:25 AM

Is there a general way to correct for asymmetric lens distortion? In particular, is it possible to figure out an appropriate algorithm by taking a picture of grid paper and comparing distorted grid with original?

I can post distorted image here if it is any help.

thanks

 
Erik Faye-Lund

February 16, 2005, 02:19 PM

google for motion estimation. it's a rather common task and should be fairly well documented, as it's a part of stuff like mpeg4-encoders.

 
Chris

February 16, 2005, 02:29 PM

If you have an image of the distorted grid, I would imagine the solution is simple. Tile the picture in sufficiently small squares (maybe even correlate the square size to the local extent of distortion), approximate the distortion by only taking the relative movement of the four corner points into account.

You would only need a routine to map a polygonal (4 corners) area of image data onto a square. I would seem to me that this can be done with linear interpolation.

This may lead to seams in the final image, as approximations and linear interpolation on neighbouring squares will slightly differ from each other, but one could come up with an error estimation here and conditionally subdivide the square further.

I didn't actually implement this, but maybe it's worth a try ? Or maybe it gives unusable results for a reason not obvious to me.

 
Andrew Walker

February 16, 2005, 05:01 PM

Last time I had to this, it was for Robocup...

You're probably looking for Tsai Camera Calibration algorithm / method. I looked at all the maths and cringed, got lazy and then decided to use the camera undistortion algorithms from OpenCV, cvUnDistortInit and cvUnDistort.

OpenCV also comes with a directshow filter that generates an undistortion matrix from a chessboard which might be useful to you as an offline tool, although we found it to be quite buggy.

OpenCV on Sourceforge

 
hunguptodry

February 16, 2005, 06:43 PM

Chris:
This may lead to seams in the final image, as approximations and linear interpolation on neighbouring squares will slightly differ from each other, but one could come up with an error estimation here and conditionally subdivide the square further.

I'm getting somewhat similar results. I think some form of anti-aliasing is also required for this to work correctly. Perhaps my grid is not fine enough. Anyway I have abandoned this home-made approach. I'm hoping that this is something that has already been invented.

Andrew Walker:
You're probably looking for Tsai Camera Calibration algorithm / method. I looked at all the maths and cringed, got lazy and then decided to use the camera undistortion algorithms from OpenCV, cvUnDistortInit and cvUnDistort.

Thanks for the hint. Will investigate further.

 
rathapol

March 12, 2005, 12:39 PM

i also am looking for undistortion algorithms. i am trying opencv. cvUndistortInit() requries intrMatrix obtained from cvCalibrateCamera(). i have no clue how to use the cvCalibrateCamera() function -- which params are inputs or which are outputs. could use please list steps to get the undistortion matrix from a chessboard?

thanks

 
timm

March 12, 2005, 04:15 PM

Instead of taking pictures of a grid, why not take a color picture with
a red gradient in the horizontal direction and a blue gradient in the vertical direction? Then you will have a pixel-by-pixel distortion map instantly....

 
Morgan

March 12, 2005, 04:35 PM

I think the problem with a color grid is that then you'd have to exactly color calibrate the camera, which is about as hard as distortion correction.

-m

 
Andrew Walker

March 13, 2005, 03:59 PM

The simple answer is you don't need to. Calibrate the camera as an offline process using the directshow calibfilter sample supplied with OpenCV. Once you get something reasonable - and generally it takes a lot of stuffing around to get it even close, save the data out to file, and you can reload it later.

You'll need to copy and paste or write your own function for loading the matrix file generated by the calibfilter.

1. Load the matrix from file
2. Call cvUndistortImage once to generate the undistort mapping

  1.  
  2.     cvUnDistortInit(tmpImage.get(),        // doesn't matter
  3.                     undistortMap_,         // important - check docs on format
  4.                     params.cameraMatrix,   // loaded from file
  5.                     params.distortion,     // loaded from file
  6.                     false);                // interpolate
  7.  

3. Call cvUnDistort each time you need to undistort
  1.  
  2.     cvUnDistort( inputImage,
  3.                  outputImage,
  4.                  undistortMap,
  5.                  false);
  6.  

 
This thread contains 9 messages.
 
 
Hosting by Solid Eight Studios, maker of PhotoTangler Collage Maker.