# Import required libraries
import cv2
import glob
import numpy as np
import matplotlib.pyplot as plt
# Arrays to store object points and image points from all the images.
objpoints = [] # 3d point in real world space
imgpoints = [] # 2d points in image plane.
# Termination criteria
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
# We can specify these points in the units of our choice (for
# example, in centimeters or in inches)
# However, the simplest thing to do is to assume that each square represents one unit.
# Prepare object points
objp = np.zeros((6*8,3), np.float32)
objp[:,:2] = np.mgrid[0:8,0:6].T.reshape(-1,2)
# Make a list of calibration images
images = glob.glob('GO*.jpg')
These functions provided by OpenCV can help us to detect feature points that are interesting for us in order to calibrate the camera:
# Loop through the list of images and search for chessboard corners
for idx, filename in enumerate(images):
# Read the image and convert it to a grayscale
image = cv2.imread(filename)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Find the chessboard corners
found, corners = cv2.findChessboardCorners(gray, (8,6), None)
# If found, add object points, image points (after refining them)
if found == True:
objpoints.append(objp)
corners2 = cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria)
imgpoints.append(corners2)
# Draw and display the corners
cv2.drawChessboardCorners(image, (8,6), corners, found)
#write_name = 'corners_found'+str(idx)+'.jpg'
#cv2.imwrite(write_name, image)
cv2.imshow("Corners",image)
cv2.waitKey(0);
cv2.destroyAllWindows()