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.
Our code can be found at OpenCV Examples.
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:
Required packages need to be imported.
import numpy as np
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.
# termination criteria
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.
cap = cv2.VideoCapture(0)
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.
# It's very important to transform the matrix to list.
You may use the following piece of code to load the calibration parameters from file “calibration.yaml”.
with open('calibration.yaml') as f: