# Camera Calibration Theory

This page details the mathematics of the camera calibration.

## Camera Model

Calcam is based on fitting or otherwise creating a model which describes the relationship between 3D real-world coordinates and image coordinates. It supports two different models: one for “conventional” rectilinear lenses and one for fisheye lenses. In both cases, we wish to relate the coordinates of a point \((X,Y,Z)\) in the lab frame to its pixel coordinates \((x{_p},y{_p})\) in the camera image. First, we must consider the position and viewing direction of the camera in the lab frame, which is described by a 3D translation and rotation. The translation and rotation parameters are known as the *extrinsic* parameters in the model. Knowing these, we can apply a suitable translation and rotation to obtain the point of interest’s coordinates in the camera frame: a 3D real space coordinate system where the camera pupil is at the origin and the camera looks along the positive \(Z\) axis. We denote the coordinates of our point of interest in the camera frame as \((X^\prime,Y^\prime,Z^\prime)\).

In order to find the pixel coordinates of this point in the camera image, we start with a simple perspective projection, where the height of an object in the image is inversely proportional to its distance from the camera pupil:

The *normalised* coordinates \((x_n,y_n)\) are then transformed by a model which describes the image distortion due to the optical system. This model depends on the lens type, and models for the rectilinear and fisheye lenses are described in the following sections. Here we simply denote the resulting distorted normalised coordinates as \((x_d, y_d)\). Finally, the normalised, distorted coordinates are related to the actual pixel coordinates \(x_p, y_p\) in the image plane by multiplication with the *camera matrix*:

Here \(f_x\) and \(f_y\) are the effective focal length of the imaging system measured in units of detector pixels in the horizontal and vertical directions, and are expected to be equal for square pixels and non-anamorphic optics. \(c_x\) and \(c_y\) are the pixel coordinates of the centre of the perspective projection on the sensor, expected to be close to the detector centre. The parameters in the camera matrix, along with those describing the distortion model, constitute the *intrinsic* camera parameters, i.e. they are characteristic of the camera and optical system and are independent of how that system is placed in the lab.

## Rectilinear Lens Distortion Model

The image distortion model for rectilinear lenses takes in to account radial (barrel or pincushion) distortion, and tangential (wedge-prism like, usually due to de-centring of optical components) distortions. The equation relating the undistorted and distorted normalised image coordinates in this model is:

where \(r = \sqrt{x_n^2 + y_n^2}\), and \(k_n\) and \(p_n\) are radial and tangential distortion coefficients, respectively. The polynomial in \(r^2\) in the first term describes the radial distortion while the second term represents tangential distortion.

## Fisheye Lens Distirtion Model

The fisheye distortion model only includes radial fisheye distortion. Unlike the rectilinear lens model, the polynomial describing the radial distortion is a function of an anglular distance from the centre of perspective, rather than a linear distance in the image:

where \(r = \sqrt{x_n^2 + y_n^2}\) and \(\theta = \tan^{-1}(r)\).

## Underlying OpenCV Documentation

Calcam does not implement the above camera models within its own code; under the hood it uses the OpenCV camera calibration functions. It may therefore be helpful to also refer to the OpenCV camera calibration documentation, which can be found on the OpenCV webpages.