# matlab programming-making calibration board (within 10 lines of code)

## Preface

When the sensor is calibrated, a calibration board is needed; it may be single-sensor calibration, such as camera calibration to obtain internal parameters. It may be calibration between multiple sensors. Recently, we are doing perceptual fusion, which requires lidar and camera calibration. This article records the use of matlab to make the calibration board.

table of Contents

One, programming basics

Two, the basic version-to achieve the effect

Three, advanced version-eliminate the gray part

Fourth, save the calibration board

5. Advanced version—calibration board that meets specifications

Six, even number* odd number checkerboard—calibration board

## One, programming basics

Core function: checkerboard()

There are three ways to use them, as follows:

• I = checkerboard;
• I = checkerboard(n);
• I = checkerboard(n,r,c);

Meaning explanation: n refers to the pixel size of each grid; r refers to the number of rows; c refers to the number of columns.

## Two, the basic version-to achieve the effect

2.1) I = checkerboard;

The code is as follows: Create an 8*8 matrix by default, with 10 pixels;

``figure;I = checkerboard;imshow(I)``

Among them, figure refers to the creation of a panel, which will be used to display the calibration chart later; imshow(I) is the function of display.

I is essentially a matrix; the size is 80*80; it can be viewed with the size function, namely: size(I)

2.1) I = checkerboard(n);

The code is as follows: Create an 8*8 matrix by default, the pixel is n, which can be set freely, but it needs to be a positive integer, such as 20;

``figure;I = checkerboard(20);imshow(I)``

3) I = checkerboard(n,r,c);

The code is as follows: Create a black grid, white grid, 2 rows and 3 columns checkerboard grid, that is, a 4*6 matrix with 100 pixels;

``figure;I = checkerboard(100, 2, 3);imshow(I)``

## Three, advanced version-eliminate the gray part

Create a black grid and white grid with 2 rows and 3 columns, that is, a 4*6 matrix with 100 pixels; there is no gray effect:

code show as below:

``figure;I = checkerboard(100,2,3);K = (I > 0.5);imshow(K);``

## Fourth, save the calibration board

The main function is: imwrite()

code show as below:

``figure;I = checkerboard(100,2,3);K = (I > 0.5);imshow(K);imwrite(K,'cher_2_3.jpg');``

Save the location where the calibration board is located. On the displayed screen, click Save As to view the path where the calibration board is saved.

Note : Use the imwrite() function to save the generated calibration board picture; do not directly use the picture saved as; if you ask why? It is recommended to try it, and you will find that the saved image will change the original format, that is, the pixels of the calibration board have been changed.

## 5. Advanced version—calibration board that meets specifications

First observe the difference between these two calibration boards:

Even * Odd; In a checkerboard, one side contains an even number of squares, and the other side contains an odd number of squares.

Even number * Even number; in a checkerboard, one side contains an even number of squares, and the other side also contains an even number of squares.

Usually referring to the official guide, only even * even numbered checkerboards can be generated; this is the case for most tutorials on the Internet.

Guide to the official calibration board: https://ww2.mathworks.cn/help/images/ref/checkerboard.html

However, in the calibration work, there are requirements for the calibration board, such as the need: a checkerboard calibration board, one side contains an even number of squares, and the other side contains an odd number of squares . The square pattern can produce unexpected results for the external elements of the camera. For example: the orientation of the calibration board may not be detected correctly.

This problem also puzzled me for 2 hours before it was solved. Share it.

## Six, even number* odd number checkerboard—calibration board

The bright half meets the requirement of odd and even number side;

``figure;I = checkerboard(100,5,9);imshow(I);imwrite(I,'cher.jpg');``

Think about it first, the core function to create a checkerboard is checkerboard( ); the created object is essentially a matrix; then it is easy to solve it, first observe the matrix law, and then extract or divide it.

Check the size of I: (original size)

>> size(I)

ans = 1000 1800

Then, just cut out half of the rectangle.

Split out

``figure;I = checkerboard(100,5,9);imshow(I(:,1:900));imwrite(I(:,1:900),'cher.jpg');``

### Generic code

Generate a checkerboard with 10 rows and 9 columns-calibration board

``boardsize = 100; % 每个格子大小，单位：像素r = 10; % 行数c = 9; % 列数% 下面进行制止棋盘格figure;I = checkerboard(boardsize, r/2, c);imshow(I(:,1:boardsize*c));imwrite(I(:,1:boardsize*c),'cher.jpg');``

The calibration board complies with matlab standards:

https://ww2.mathworks.cn/help/lidar/ug/lidar-camera-calibration-guidelines-and-procedure.html

A3 paper size, generate a checkerboard with 8 rows and 5 columns-calibration board

``boardsize = 620; % 每个格子大小，单位：像素r = 8; % 行数c = 5; % 列数% 下面进行制止棋盘格figure;I = checkerboard(boardsize, r/2, c);imshow(I(:,1:boardsize*c));imwrite(I(:,1:boardsize*c),'cher_8_5_ok.jpg');``

The calibration board complies with matlab standards:

https://ww2.mathworks.cn/help/lidar/ug/lidar-and-camera-calibration.html

If you feel good, please like it; welcome to communicate~