# Calibrating a camera: Theory

## Calibration

Why calibrate your camera? After calibration, you can use your camera to do real world measurements. It establishes a relation between pixels and real world dimensions. Also, you can remove distortions caused by a camera’s physical flaws. The goal of calibration is to estimate the following:

1. Focal length of lens along X axis
2. Focal length of lens along Y axis
3. Lens displacement along X axis
4. Lens displacement along Y axis
5. 3 numbers that describe radial distortion
6. 2 numbers that describe tangential distortion
7. The position of the camera in the real world (x, y and z)
8. The orientation of the camera in the real world (around the x, y and z axis)

That’s a lot of things that calibration establishes. Variables 1-4 constitute intrinsic parameters. Variables 5 and 6 are the distortion parameters. And variables 7 and 8 are the extrinsic parameters.

## Calibrating with chessboards

One standard procedure to calculate all the unknowns above, or simply calibrating a camera, is to use a chessboard pattern. It looks like this:

Remember that this is just an algorithm. They designed it using a chessboard. They could have done it using something else too.. a box or even a rubber duck. But a chessboard certain properties that makes it good for calibrating purposes:

• It is a plane. You don’t need to deal with “depth”.
• There exist points on this plane that you can identify uniquely.
• You can extract these points very easily.
• These points physically lie in straight lines, no matter how they appear on camera.

## Posing the chessboard

You take several pictures of the chessboard. Each picture in a different orientation. These orientations should vary a lot. Or you might get a bad estimate of the various parameters. A possible chessboard orientation could be like this:

From all those images, you solve several equations. The result is the best estimate for all the unknowns we were looking for. This solving produces what is known as a homography. A homography is a matrix that connects the image-coordinates (pixels) and the real world coordinates (meters, centimeters, etc).

I won’t go into its math right now, but just know that multiplying the homography with the position of a point in actual plane (chessboard) produces its image coordinates. And multiplying the image coordinates by the matrix inverse produces actual plane coordinates.

## After calibration

Once you’re done with solving all the equations and you have a result, you can fix distortions in the image and make actual measurements. Calibration is also needed for stereo vision.

## Summary

You got an overview of how one particular method of camera calibration works. OpenCV comes with several functions that help you implement this. But there are minor points that need to be taken care of. We’ll implement it next.

1. Divines
Posted February 7, 2011 at 10:09 pm | Permalink

Hi,

How can i calibrate objects in an image using matlab?
Is it possible to use camera calibration toolbox for such cases?

• Posted February 8, 2011 at 1:40 pm | Permalink

I haven’t worked with Matlab, but I’ve seen people calibrate cameras quite easily on Matlab, probably with the toolbox you mention.

2. Dave
Posted April 2, 2011 at 10:25 pm | Permalink

Hi Utkarsh,
I would like to estimate the distance of a captured object relative to the camera and already did a camera calibration with a chessboard, so I have the intrinsic and the distortion parameters. Now understood that I need the extrinsic parameters in order to estimate the distance. I found the function cvFindExtrinsicCameraParams2 of opencv (http://opencv.willowgarage.com/documentation/camera_calibration_and_3d_reconstruction.html#findextrinsiccameraparams2) but the problem is that I don’t understand the docu: I guess that the ImagePoints are the detected corners of the chessboard and the ObjectPoints are the real distances of the corners (in my case 20 mm). Can you give an example how to find the camera’s extrinsic parameters?

Thank you very much
Dave

3. ali
Posted June 28, 2011 at 2:33 am | Permalink

thx

4. Robert L
Posted July 23, 2011 at 7:52 pm | Permalink

Utkarsh,

You should publish a book on Computer Vision. The material that I have thus far seen or purchased is mediocre in comparison to your clear and understandable descriptions w/photos. Keep up the excellent work!

PS. Please write an article on morphology. Perhaps on rounded shape detection.

Robert

• Posted July 31, 2011 at 5:27 pm | Permalink

Ha! Thanks for thinking I should write a book And the morphology article, I probably will!