The code being referred is present at camera_calibration.
- Calculalte the internal vertices along row and column for your chessboard image.
################ FIND CHESSBOARD CORNERS - OBJECT POINTS AND IMAGE POINTS #############################
chessboardSize = (8,5)
Example is shown below:
There are 8 internal vertices along row (marked blue) and 5 internal vertices along column (marked blue).
- Calculate the actual length of square on paper. All units in further measurements will be the same (mm).
size_of_chessboard_squares_mm = 13
- objp are all the internal vertices along chessboard distanced at the same distance on paper as in real world, representing the co-ordinates(x,y,z) in real world.
# prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)
objp = np.zeros((chessboardSize[0] * chessboardSize[1], 3), np.float32)
objp[:,:2] = np.mgrid[0:chessboardSize[0],0:chessboardSize[1]].T.reshape(-1,2)
size_of_chessboard_squares_mm = 13
objp = objp * size_of_chessboard_squares_mm
- Loop through all input images.
- Convert rgb image to gray.
- Calculate coarse corners from chessboard corners (input gray image and internal vertices tuple) using findChessboardCorners.
- If corners are found (ret==True), add the 3d points obpj to obpj list.
- Calculate the finer corners from initial gray image and coarse corners, using cornerSubPix.
- Add the finer corners to corners list.
- Draw the finer corners on the image and save the re-drawn image and save them.
for image in images:
img = cv2.imread(parent_dir+image)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
frameSize = gray.shape
ret, corners = cv2.findChessboardCorners(gray, chessboardSize,None,)
print(f"{image}: {ret}: Framesize: {frameSize}")
# If found, add object points, image points (after refining them)
if ret == True:
objpoints.append(objp)
corners2 = cv2.cornerSubPix(gray, corners, (11,11), (-1,-1), criteria)
imgpoints.append(corners)
# Draw and display the corners
cv2.drawChessboardCorners(img, chessboardSize, corners2, ret)
if os.path.exists("drawn_chessboard/") is False:
os.makedirs("drawn_chessboard/")
cv2.imwrite(f'drawn_chessboard/{image}', img)
cv2.waitKey(1000)
-
Calculate all the calibration matrix using calibrateCamera.
- Input
- List of list of arrays of 3D points in real-world coordinates. Each array corresponds to the chessboard corners in one image. obpj list.
- List of list of arrays of finer detected corners 2D points in image coordinates. Each array corresponds to the detected corners in one image.
- Tuple of internal vertices along row and column for your chessboard image.
- Output
- ret: The root mean square (RMS) re-projection error. It gives a good estimation of the precision of the calibration. Lower values are better.
- cameraMatrix : A 3x3 matrix containing the intrinsic parameters of the camera.
- dist : Distortion Coefficients Format: [k1, k2, p1, p2, k3], where:
- k1, k2, k3 are the radial distortion coefficients.
- p1, p2 are the tangential distortion coefficients.
- If the vector contains more elements, it includes higher-order radial and tangential distortion coefficients.
- rvecs : Rotation Vectors- A list of rotation vectors (one for each calibration image) that describe the rotation of the camera relative to the calibration pattern. It can be converted to rotation matrix.
- tvecs : Translation Vectors- A list of translation vectors (one for each calibration image) that describe the translation of the camera relative to the calibration pattern.
- Input
-
Save cameraMatrix and dist as pkl file for further use in Camera_Calibration_output.