Neuralet > Camera Calibration Using Homography Estimation

Camera Calibration Using Homography Estimation

camera calibration

A Sunday on La Grande Jatte, Georges Seurat, 1884-1886 (image source)

This article explains how to map pixel distances on 2D images to the corresponding real-world distances in 3D scenes using homography estimation and applies this approach to a practical problem as a use case.

Basic knowledge of linear algebra, e.g., matrix multiplication and system of linear equations, is recommended but not required to follow this article.

Measuring world coordinates; the challenges

Let us start off with an example to illustrate the problem that we are trying to solve here. Suppose that you want to measure the distance between people who cross a particular street using the videos captured by CCTV cameras installed there (we will refer to this example a few times throughout the rest of this article). Even if you manage to detect pedestrians in each video frame, calculating the real-world distance (according to the real-world coordinate system) between the people using the pixel coordinates in the video frame is not straightforward.

Some challenges arise when you want to calculate the world coordinates (or distances) using the pixel coordinates. For instance, in a 2D image, since the third dimension cannot be displayed, we cannot take the depth of the scene into account easily. This issue results in some inaccuracies when calculating world coordinates from pixel coordinates. In Figure 1, the two men passing by each other are keeping a distance of almost three meters. However, the distance seems less in the image view.

Calculating the world distance from images is not straightforward. We need to use camera calibration techniques to calculate the world distances accurately.
Figure 1. Two men passing by each other while keeping a distance of almost three meters. The distance between them seems much less in the image view.

In some applications, this error cannot be ignored. For example, if you want to find out if people are adhering to social distancing, you need to make sure that your calculations are exact and accurate. Otherwise, your application reports inaccurate social distancing violation cases, and therefore, will be ineffective.

We need a stable approach to map 2D pixel coordinates to 3D world coordinates of each point in order to calculate the world distances of the points of interest. This article explains camera calibration using homography estimation to mitigate the challenge of mapping pixel coordinates to world coordinates without losing much accuracy.

From 3D scene to 2D image and vice versa

Cameras use perspective projection to map the world coordinates in 3D scenes into pixel coordinates in 2D images. Figure 2 illustrates an example of how cameras project the object of interest into a 2D plane using perspective projection.

How perspective projection works
Figure 2. Perspective projection maps the world coordinates in 3D scenes to pixel coordinates in 2D images (image credits).

We are interested in doing the exact opposite of what cameras do; to find a mapping from pixel coordinates of a point in an image to world coordinates of that point in the actual 3D scene. 

In the social distancing application example that we mentioned earlier, if we find the pedestrians’ world coordinates, calculating the world distance between the people becomes trivial. To achieve the world coordinates, we need an approach to map the 2D coordinates on the image to 3D coordinates in the real-world scene, exactly in the opposite way of what cameras do.

Pinhole camera model

Let us quickly explain the mathematics behind projecting a point from the 3D scene onto the 2D image. We will assume that we are working with an ideal pinhole camera with an aperture described as a point (see Figure 3).

 

An ideal pinhole camera's aperture only passes a ray of light for each point in the scene.
Figure 3. An ideal pinhole camera has an aperture so small that it only passes a single light ray for each point in the environment (image credits).

The relationship between the world coordinates and the image coordinates can be expressed using the following formula:

(1)   \begin{equation*}  s\begin{pmatrix}u \\v \\1\end{pmatrix}= \underbrace{\begin{pmatrix}f_x & 0 & c_x \\ 0 & f_y & c_y \\ 0 & 0 & 1\end{pmatrix}}_\text{$I$}\underbrace{\begin{pmatrix}r_{11} & r_{12} & r_{13} & t_1 \\ r_{21} & r_{22} & r_{23} & t_2 \\ r_{31} & r_{32} & r_{33} & t_3\end{pmatrix}}_\text{$E$}\underbrace{\begin{pmatrix}X \\ Y \\ Z \\1 \end{pmatrix}}_\text{$K$} \end{equation*}

On the left-hand side, we see scale factor s and a vector representing the image coordinates. Note that we can always factorize this vector by s such that the third element becomes equal to 1. We calculate s, u, v based on the calculations on the right-hand side to map 3D world coordinates to 2D image coordinates.

On the right-hand side, we see two matrices, I, E, and a vector K. The vector contains the world coordinates (X, Y, Z) of the point that we are trying to map to the 2D space with the fourth element set equal to 1.

The relationship between the world coordinates and the image coordinates depends on both intrinsic and extrinsic characteristics of the camera. Therefore, two matrices I and E are introduced into the formula to capture these characteristics. Let us have a closer look at the parameters included in the intrinsic and extrinsic matrices:

Intrinsic matrix

The intrinsic matrix I describes the camera’s internal specifications, such as the focal length and the principal point. 

f_x, f_y: camera’s focal length (meters/pixels). Two parameters are introduced to describe cameras with rectangular pixels. If your camera uses square pixels, set f_x = f_y.

c_x, c_y: principal point or image center. The coordinates of the point at the intersection of the optical axis and the image plane.

Extrinsic matrix

The extrinsic matrix E defines the camera’s external properties, i.e.,  the camera’s position and rotation angle in the real-world scene.

r_{ij}: camera’s rotation matrix that describes the camera’s rotation angle in the installed environment.

t_i: the camera’s offset from the origin of the world coordinate system.

Once you measure the mentioned parameters, you can plug them in the formula and do a simple matrix multiplication to obtain image coordinates

Besides direct methods of measuring the camera’s intrinsic and extrinsic parameters, some alternative approaches can be used to estimate these parameters, such as Zhang’s method and Tsai’s algorithm. We encourage you to investigate these papers (1, 2) to learn more about these approaches.

The pinhole camera model maps the 3D world coordinates to 2D image coordinates by parameterizing the intrinsic and extrinsic characteristics of the camera. If we want to do the opposite and find the 3D world coordinates from the 2D image coordinates, we should calculate the inverse of matrices I and E and multiply the image coordinates vector by I^{-1} and E^{-1} to obtain values of the world coordinates. These calculations can be described using the following formula:

(2)   \begin{equation*}  \begin{pmatrix}X \\ Y \\ Z \\1 \end{pmatrix} = s\begin{pmatrix}r_{11} & r_{12} & r_{13} & t_1 \\ r_{21} & r_{22} & r_{23} & t_2 \\ r_{31} & r_{32} & r_{33} & t_3\end{pmatrix}^{\dagger}\begin{pmatrix}f_x & 0 & c_x \\ 0 & f_y & c_y \\ 0 & 0 & 1\end{pmatrix}^{-1}\begin{pmatrix}u \\v \\1\end{pmatrix} \end{equation*}

You can now plug in image coordinates into this formula and obtain the corresponding world coordinates by doing simple matrix multiplications.

Don’t need that much information? Here’s the easier way.

The previous approach maps every point in the image coordinates to the corresponding point in the real-world’s coordinate system. However, in some applications, we do not need that much information to reach our final goal.

Let us picture the example of calculating the distances between the people again. If we want to measure the distances between the pedestrians using images (or videos) taken with a camera, do we need to map every point in the image to its corresponding coordinates in the world scene?

We know that the pedestrians are walking on the same plane, which is the ground. If we represent each pedestrian with a point on the ground plane and find the world distance between these points, the problem would be solved because these points are all on the same plane. Thus, we only need to calculate the world coordinates of the points on the ground plane for each pedestrian in order to find the pairwise distances between them. 

In other words, we want to map the points that lie on one plane from the image coordinates to the world coordinates. Therefore, we will be using 2D vectors to represent both image coordinates and world coordinates that reside on the same plane. In Figure 4, for example, mapping the points that reside on the blue plane is sufficient to calculate the distance between the people who pass the corridor.

Mapping the points on one plane from image coordinates to world coordinates is sufficient to calculate the world distance between the people.
Figure 4. Finding the world coordinates of the blue points (the ground plane) is sufficient to calculate the distances between the people who pass this corridor (image credits).

In this example, the problem is reduced to determining the world coordinates of the points residing in a two-dimensional plane instead of mapping the whole space. In such cases, we can apply a more straightforward method, called homography estimation, to find the world coordinates of points that lie on a plane (rather than the whole space) using fewer parameters.

Homography estimation

In this approach, we use a matrix, called the homography matrix, to map the points residing on a plane from world coordinates to the corresponding image coordinates. Note that all the points that we are mapping are coplanar; therefore, we can represent the world coordinates with 2D vectors. The homography matrix H is a 3*3 matrix that contains the parameters h_{11}, h_{12}, h_{13}, h_{21}, ..., h_{33}. If we manage to figure out the values of h_{11} through h_{33} somehow, we can use the following formula to map the world coordinates to image coordinates:

(3)   \begin{equation*}  \begin{pmatrix}x_2 \\ y_2 \\ z_2 \end{pmatrix} = \begin{pmatrix} h_{11} & h_{12} & h_{13} \\ h_{21} & h_{22} & h_{23} \\ h_{31} & h_{32} & h_{33}\end{pmatrix}\begin{pmatrix}x_1 \\ y_1 \\ z_1\end{pmatrix} \end{equation*}

In this formula, (x_2, y_2, z_2) is a normalized vector with z_2 = 1, and (x_2, y_2) is the coordinates of a point p within the image. Similarly, (x_1, y_1, z_1) is a normalized vector with z_1 = 1, and (x_1, y_1) describes the coordinates of p in the world coordinate system. By substituting the values of h_{11},..., h_{33}. in the homography matrix H, we can calculate the image coordinates for each given point expressed in world coordinates. 

If we want to do the mapping in the opposite direction, i.e., from the image coordinates to the world coordinates, we need to calculate the inverse of the homography matrix to get H^{-1} and multiply the image coordinates vector by H^{-1} to obtain the corresponding world coordinates. The following formula shows how we can find the world coordinates based on the image coordinates:

(4)   \begin{equation*}  \begin{pmatrix}x_1 \\ y_1 \\ z_1\end{pmatrix} = \begin{pmatrix} h_{11} & h_{12} & h_{13} \\ h_{21} & h_{22} & h_{23} \\ h_{31} & h_{32} & h_{33}\end{pmatrix} ^ {-1} \begin{pmatrix}x_2 \\ y_2 \\ z_2 \end{pmatrix} \end{equation*}

But how can we compute h_{11},..., h_{33}?

Suppose that we have the world coordinates of four points p_1, p_2, p_3, p_4 with their corresponding mapping p'_1, p'_2, p'_3, p'_4 in the image coordinate system, where p_i = (x_i, y_i) and p'_i = (x'_i, y'_i). Using these pairs of points, we can write a linear equation with eight equations and eight unknowns that describe h_{11},..., h_{32}, and normalize the last parameter by considering h_{33} = 1.

To solve this system of linear equations, we rearrange the equations to gather all the variables on one side and form a PH = 0 equation as follows:

(5)   \begin{equation*}  PH = \begin{pmatrix} -x_1 & -y_1 & -1 & 0 & 0 & 0 & x_1x'_1 & y_1x'_1 & x'_1\\ 0 & 0 & 0 & -x_1 & -y_1 & -1 & x_1y'_1 & y_1y'_1 & y'_1\\ -x_2 & -y_2 & -1 & 0 & 0 & 0 & x_2x'_2 & y_2x'_2 & x'_2\\ 0 & 0 & 0 & -x_2 & -y_2 & -1 & x_2y'_2 & y_2y'_2 & y'_2\\ -x_3 & -y_3 & -1 & 0 & 0 & 0 & x_3x'_3 & y_3x'_3 & x'_3\\ 0 & 0 & 0 & -x_3 & -y_3 & -1 & x_3y'_3 & y_3y'_3 & y'_3\\ -x_4 & -y_4 & -1 & 0 & 0 & 0 & x_4x'_4 & y_4x'_4 & x'_4\\ 0 & 0 & 0 & -x_4 & -y_4 & -1 & x_4y'_4 & y_4y'_4 & y'_4\\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 \end{pmatrix} \begin{pmatrix} h_{11} \\ h_{12} \\ h_{13} \\ h_{21} \\ h_{22} \\ h_{23} \\ h_{31} \\ h_{32} \\ h_{33}\end{pmatrix} = 0 \end{equation*}

We can now use linear algebra algorithms, such as SVD, to solve this system for H. Having calculated H, we can find the image coordinates from world coordinates using formula 3. Finding world coordinates from image coordinates is also possible by plugging in H_^{-1} in formula 4.

All of the mathematics we explained above are implemented in a function in the OpenCV library. You can use this function to estimate the homography matrix in a single line of code. Let us explain how this method works.

Homography estimation in a single line of code

To restate the problem, you have installed and fixed your camera somewhere, and you want to obtain the world coordinates (or distances) using a single image captured by your camera.

To do so, first, mark the four corners of an imaginary square (a 1m x 1m square is recommended for ease of calculations) on the ground and choose one of these points as the origin of the world coordinate system. Then, measure the X, Y coordinates of these four points according to the origin you just set. We call these numbers the source points.

Then, without changing the camera position or the heading angle, calculate the coordinates of the four points from the previous step, but this time in image coordinates. Let us call these coordinates the destination points.

We are trying to estimate the homography matrix H by finding a mapping between the source points and the destination points. Luckily, the OpenCV library has a function that does the calculations for us. The findHomography function takes in two sets of numbers, the source points and the destination points, and estimates the homography matrix based on the corresponding coordinates of these two sets of numbers. We can use this function to calculate H in a single line of code as follows:

h, status = cv2.findHomography(pts_src, pts_dst)

You can refer to the OpenCV documentation to learn more about this function. Figure 5 illustrates how this function works in practice. The four blue dots are the reference points to calculate H. The green dots computed using the homography matrix show the ground plane. The key point is that each two consecutive green dots has the same distance in the world coordinates, and the closer the points are to the camera, the more distant they are.

Camera calibration using homography estimation sample output.
Figure 5. Camera calibration using homography estimation sample output. The four blue dots are the reference points to calculate H, and the green dots show the ground plane.

Camera calibration using homography estimation; a use case

To see how this camera calibration method works in practice, we implemented this method in our open-source Smart Social Distancing application. This application calculates the world distances between people in an input video to measure how well social distancing is being practiced. You can learn more about this application here.

The Smart Social Distancing application implements three methods to calculate the world distance between the people; 1- the calibration-less method comparing bounding box center points, 2- the calibration-less method comparing bounding box corners, and 3- the camera calibration method using homography estimation. The calibration-less methods are explained in this article in detail. 

The user can select the method they want to use by specifying the method name in the application configuration file. If the camera calibration method is selected, the user should also specify the path to a .txt file that contains the inverse of the homography matrix (H^{-1}) in the config file. In future updates, we will implement an interface for the user to mark the corners of a square that is 1 meter long on each side. The user can leave the rest of the calculations to the app.

Our pedestrian detection model outputs a rectangular bounding box around each detected pedestrian. For each bounding box, the bottom side of the rectangle lies on the ground plane. Thus, to measure the distance between the pedestrians, we take the middle point of each rectangle’s bottom side to estimate the intersection of the person with the ground and find its world coordinates (by multiplying H^{-1} with the pixel coordinates vector). Finally, we calculate and report the pairwise Euclidean distance as the distance between the detected pedestrians.

Note that since the reference square that we marked on the ground was a 1m x 1m square, the calculated distances preserve the real-world scale and are in meters. Therefore, no further calculation, such as rescaling or normalization, is needed.

Camera calibration using homography estimation is used in the Smart Social Distancing application by Neuralet.
Figure 6. Real-world distances between the people calculated using the homography estimation camera calibration method.

Conclusion

Cameras project 3D scenes onto 2D images. The pinhole camera model shows the relationship between the world coordinates and image coordinates using the intrinsic and extrinsic matrices that capture different characteristics of the camera.

If we want to find the relationship between the image coordinates and world coordinates of points that reside on the same plane (coplanar points), we can use homography estimation. The homography matrix can be estimated using two corresponding sets of points, one in world coordinates and the other in image coordinates. The OpenCV library provides a useful function that takes in the two sets of numbers as input and returns the homography matrix as output.

We have implemented the homography estimation algorithm for camera calibration in our Smart Social Distancing application. In future updates, we will add an interface for camera calibration in our app.

Further readings

Geometry of Image Formation

Camera parameters

Homography estimation

Share This

Leave a Reply

Your email address will not be published. Required fields are marked *

Get In Touch

Have a question? Send us a message and we will respond as soon as possible.