# Camera Calibration Using a Circle Grid

# Preparation

By reading two of our previous blogs Camera Calibration Using a Chessboard and Camera Posture Estimation Using Circle Grid Pattern, it wouldn’t be hard to replace the chessboard by a circle grid to calculate camera calibration parameters.

# Coding

Our code can be found at OpenCV Examples.

## First of all

As mentioned in Camera Calibration Using a Chessboard, for intrinsic parameters estimation, namely, camera calibration, there is **NO** need to measure the circle unit size. And the circle gird is to be adopted is exactly the same one as used in Camera Posture Estimation Using Circle Grid Pattern:

## Secondly

Required packages need to be imported.

1 | import numpy as np |

## Thirdly

Some initialization work need to be done, including: 1) define the termination criteria when refine the corner sub-pixel later on; 2) blob detection parameters; 3) object points coordinators initialization.

1 | # termination criteria |

## Fourthly

After localizing 10 frames (**10** can be changed to any positive integer as you wish) of a grid of 2D circles, camera matrix and distortion coefficients can be calculated by invoking the function **calibrateCamera**. Here, we are testing on a real-time camera stream. Similar to Camera Posture Estimation Using Circle Grid Pattern, the trick is to do **blobDetector.detect()** and draw the detected blobs using **cv2.drawKeypoints()** before invoking **cv2.findCirclesGrid()**, so that the entire grid is easier to be found.

1 | cap = cv2.VideoCapture(0) |

## Finally

Write the calculated calibration parameters into a yaml file. Here, it is a bit tricky. Please bear in mind that you **MUST** call function tolist() to transform a numpy array to a list.

1 | # It's very important to transform the matrix to list. |

## Additionally

You may use the following piece of code to load the calibration parameters from file “calibration.yaml”.

1 | with open('calibration.yaml') as f: |