Example Remove Lens Distortion

From BoofCV
Revision as of 09:54, 19 June 2014 by Peter (talk | contribs)
Jump to navigationJump to search

This example demonstrates how to load a camera's calibration from an XML file and remove lens distortion from its images. After lens distortion is removed, the new undistorted view often "pushes" pixels outside the viewing area. To compensate for this problem it is possible to adjust the view to maximize the view area based on different objectives. After adjusting the view be sure to use the new intrinsic parameters when performing geometric operations.

Example File: ExampleRemoveLensDistortion.java

Concepts:

  • Lens Distortion
  • Calibration
  • Intrinsic Parameters

Relevant Applets:

Related Examples:

Example Code

/**
 * All real camera lens have distortion.  This distortion causes large errors when attempting to recover the
 * scene's structure and camera's motion.  The following example demonstrates how the lens distortion can be
 * removed from an image after the camera has been calibrated.
 *
 * After lens distortion has been removed the new image will not be properly contained inside the original
 * image side.  Several methods are provided for scaling and translating the image to maximize the view area
 * using different metrics.  After this adjustment has been done the new image is equivalent to one being
 * generated by a virtual camera with a different set of intrinsic parameters.
 *
 * @author Peter Abeles
 */
public class ExampleRemoveLensDistortion {

	public static void main( String args[] ) {
		String calibDir = "../data/applet/calibration/mono/Sony_DSC-HX5V_Chess/";
		String imageDir = "../data/evaluation/structure/";

		// load calibration parameters from the previously calibrated camera
		IntrinsicParameters param = UtilIO.loadXML(calibDir + "intrinsic.xml");

		// load images and convert the image into a color BoofCV format
		BufferedImage orig = UtilImageIO.loadImage(imageDir + "dist_cyto_01.jpg");
		MultiSpectral<ImageFloat32> distortedImg =
				ConvertBufferedImage.convertFromMulti(orig, null,true, ImageFloat32.class);

		// compute the transform to remove lens distortion
		// The inverse transformation (adds distortion) is used when apply adjusting an image.
		// In other application the forward transformation (removes distortion) is required.
		PointTransform_F32 tran = LensDistortionOps.transformPixelToRadial_F32(param);

		// create new transforms to optimize view area
		// After distortion the adjusted intrinsic camera parameters should be used.
		// Since they are not being used in this example null is passed in.
		PointTransform_F32 fullView = LensDistortionOps.fullView(param, null);
		PointTransform_F32 allInside = LensDistortionOps.allInside(param, null);

		// Set up image distort
		InterpolatePixelS<ImageFloat32> interp = FactoryInterpolation.bilinearPixelS(ImageFloat32.class);
		ImageDistort<ImageFloat32> distort = FactoryDistort.distort(interp,null,ImageFloat32.class);

		// render and display the different types of views in a window
		displayResults(orig, distortedImg, tran, fullView, allInside, distort);
	}

	/**
	 * Displays results in a window for easy comparison..
	 */
	private static void displayResults(BufferedImage orig,
									   MultiSpectral<ImageFloat32> distortedImg,
									   PointTransform_F32 tran,
									   PointTransform_F32 fullView,
									   PointTransform_F32 allInside,
									   ImageDistort<ImageFloat32> distort) {
		// render the results
		MultiSpectral<ImageFloat32> undistortedImg = new MultiSpectral<ImageFloat32>(ImageFloat32.class,
				distortedImg.getWidth(),distortedImg.getHeight(),distortedImg.getNumBands());

		distort.setModel(new PointToPixelTransform_F32(tran));
		GImageMiscOps.fill(undistortedImg, 0);
		DistortImageOps.distortMS(distortedImg, undistortedImg, distort);
		BufferedImage out1 = ConvertBufferedImage.convertTo(undistortedImg, null,true);

		distort.setModel(new PointToPixelTransform_F32(fullView));
		GImageMiscOps.fill(undistortedImg,0);
		DistortImageOps.distortMS(distortedImg,undistortedImg,distort);
		BufferedImage out2 = ConvertBufferedImage.convertTo(undistortedImg,null,true);

		distort.setModel(new PointToPixelTransform_F32(allInside));
		GImageMiscOps.fill(undistortedImg,0);
		DistortImageOps.distortMS(distortedImg,undistortedImg,distort);
		BufferedImage out3 = ConvertBufferedImage.convertTo(undistortedImg,null,true);

		// display in a single window where the user can easily switch between images
		ListDisplayPanel panel = new ListDisplayPanel();
		panel.addItem(new ImagePanel(orig), "Original");
		panel.addItem(new ImagePanel(out1), "Undistorted");
		panel.addItem(new ImagePanel(out3), "Undistorted All Inside");
		panel.addItem(new ImagePanel(out2), "Undistorted Full View");

		ShowImages.showWindow(panel, "Removing Lens Distortion");
	}
}