There are two new methods in the latest-ofOpenCV.zip. One is the undistort method chris mentioned. The other is the remap method. For remap you first have to setup two displacement IplImage object.
In the past I have been using something like this:
void initUndistort( float radialDistX, float radialDistY,
float tangentDistX, float tangentDistY,
float focalX, float focalY,
float centerX, float centerY,
IplImage* undistortMapX, IplImage* undistortMapY )
{
float camIntrinsics[] = { focalX, 0, centerX, 0, focalY, centerY, 0, 0, 1 };
float distortionCoeffs[] = { radialDistX, radialDistY, tangentDistX, tangentDistY };
cvInitUndistortMap( camM, dist, undistortMapX, undistortMapY );
}
and then you can use undistortMapX and undistortMapY like this
myOfCvImage.remap( undistortMapX, undistortMapY );
remap(ā¦) is very efficient when you also need to do some other transformation. To add some translation to the distortion maps you could simply do something like:
cvAddS( undistortMapX, cvScalarAll( -translateX ), undistortMapX );
cvAddS( undistortMapY, cvScalarAll( -translateY ), undistortMapY );
For both undistort(ā¦) and remap(ā¦) you need to know focal length, radial distortion and tangential distortion. You can either manually play with the values or use a calibration chessboard patter to figure these out. I probably can dig out some code for the whole chessboard calibration thing if anybody is interested. For my multitouch projects I usually had slightly better result hand tuning the coefficients, though.
Itās not overly intuitive how the intrinsic camera parameters affect the distortion. The distortion coefficients and the focal length are interrelated. Basically the higher the focal length is the less effective the distortion numbers will become.
centerX, centerY should generally be in the center of the frame (eg 160, 120). For the focal length itās not absolutely necessary to match the camera lens but probably a good starting point. To get a hang for the distortion coefficients you probably want to set them all to 0 and then individually increase them slightly.
Happy Hacking,