 # Computer Vision – Camera Calibration using MATLAB In this tutorial, you will learn how to calibrate a camera using MATLAB Software (Multiparadigm Programming Language). Here, we apply some of the concepts from our introductory computer vision tutorial.

To estimate the necessary parameters for the calibration process, you will have to use the equipment described below, but there are already other materials that can be used in the calibration process, for example, instead of using the standard chess sheet, you can use identical patterns such as ChArUco, (this one most used when using the Python programming language), which is very similar to the conventional one.

### Parts Required

• Print an A4 paper with the calibration chessboard;
• Hardware – Webcam;
• Software – IDE MATLAB (in this case tested in version R2018b).

### Camera Calibration Steps

As already described in the previous tutorial, the calibration must be according to the following steps:

• Capture several images of the calibration chessboard from different angles of view;
• Find the 2D coordinates of the calibration chessboard;
• Calibrate the camera;
• Undistortion camera.

Therefore, the necessary test for each of these steps is presented below.

### Capture several images (calibration chessboard) from different views

The starting point in camera calibration consists of capturing several images of the calibration chessboard (Figure 2, this must be glued to a flat surface) note that this must have an even number of squares along one edge, the opposite edge must have an odd number of squares) using the camera we would like to calibrate. We should capture between 10 and 20 images. The calibrated needs at least three images, but a higher value is advisable for better results.

Note: You can use the simple Chessboard presented in Figure 2.

These images can be captured by fixing the calibration chessboard and changing the position and orientation of the camera or vice versa by fixing the camera and changing the position and orientation of the calibration chessboard. For the tutorial presented, it is irrelevant how the images are captured, one way or another.

Note: The standard focus and zoom must be maintained, not changing it when capturing images for calibration. As well as the distance from the camera to the calibration chessboard must be kept throughout the captures, it must be around 2 meters.

### Find the 2D coordinates (calibration chessboard)

Below we can see at the beginning of the code, below the comment “%Read a set of calibration images.” we can see how the reading of several images can be done (already captured previously) is being used further down. Then, under the comment “%Detect the calibration pattern.” we have the function “detectCheckerboardPoints” which detects the existing calibration standard. Bellow the comment “%Generate the world coordinates of the corners of the squares” is displayed. We can obtain the world coordinates of the squares, through the function “%generateCheckerboardPoints“. Still below the comment “%Caliber camera” we have the estimation of extrinsic, intrinsic, and calibration coefficients.

``````%Read a set of calibration images.
for a = 1:10
filename = ['user001-' num2str(a,'%02d') '.bmp'];

% do something with img
end

%Detect the calibration pattern.
[imagePoints, boardSize] = detectCheckerboardPoints(imageFileNames);

%Generate the world coordinates of the corners of the squares.
squareSizeInMM = 29;
worldPoints = generateCheckerboardPoints(boardSize,squareSizeInMM);

%Calibrate the camera.
imageSize = [size(I, 1),size(I, 2)];
params = estimateCameraParameters(imagePoints,worldPoints, ...
'ImageSize',imageSize);

``````

### Calibrate the camera

In order to understand the reproduction error, right after the comment “%Visualize the calibration accuracy.” we have the function “showReprojectionErrors” which generates a bar graph, where we can see the average error in each pixel per image. This is already pre-configured by the function itself, with it we understand in an easy way, if there is an image of our data set that contains a very high error, thus harming the estimation of calibration parameters. If there is an image with a very high error, it must be reactivated and run again the code presented above.

``````%Visualize the calibration accuracy.
showReprojectionErrors(params);``````

Right after the comment “%Plot detected and retroprojected points.” we can see after the comment “%Plot detected and retroprojected points.” we can see one of the possible ways to present the images with the detection of the reprojection points, as the comment itself says and is represented in Figure 3.

``````%Plot detected and reprojected points.
figure;
imshow(imageFileNames{1});
hold on;
plot(imagePoints(:,1,1), imagePoints(:,2,1),'go');
plot(params.ReprojectedPoints(:,1,1),params.ReprojectedPoints(:,2,1),'r+');
legend('Detected Points','ReprojectedPoints');
hold off;``````

### Undistortion camera

We can see in the code below how to remove the distortion, using the previously estimated calibration parameters, just below the comment “%Remove lens distortion and display results.” via the “undistortImage” function.

``````%Remove lens distortion and display results. 