I have a 35mm nominal focal length camera that I'm trying to calibrate using a PyQt front end I'm building onto OpenCV's python library. It finds and displays matched points. Everything works except for the returns on cv2.calibrateCamera()
when using a 4 x 11 asymmetric circle grid and cv2.findCirclesGrid()
.
Using a 6 x 9 chessboard pattern, I obtain a focal length (converted to mm) of 35.8 mm. I'd accept this for now with the 35mm lens I'm using. When I use the same camera/lens and a circle grid pattern I obtain a focal length of 505.9 mm. This is clearly wrong. The k and p coefficients are also enormous. What am I missing? See code below.
Edited down to show only relevant bits. Some variables are defined elsewhere.
# termination criteria
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
# prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)
params = []
def objParams (grid):
if grid == "Checkerboard":
param1 = np.zeros((6 * 9, 3), np.float32)
param2 = np.mgrid[0:9,0:6].T.reshape(-1,2)
elif grid == "Circle Grid":
param1 = np.zeros((4 * 11, 3), np.float32)
param2 = np.mgrid[0:4,0:11].T.reshape(-1,2)
params.append(param1)
params.append(param2)
objParams(pattern)
objp = params[0]
objp[:, :2] = params[1]
# 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.
images = glob.glob(folder + '/*.jpg')
i = 0
for fname in images:
img = cv2.imread(fname)
smimg = cv2.resize(img, (0, 0), fx=scaleTo, fy=scaleTo)
gray = cv2.cvtColor(smimg, cv2.COLOR_BGR2GRAY)
# step counter
i += 1
# Find the chess board corners
if pattern == 'Checkerboard':
print("Now finding corners on image " + str(i) + ".")
ret, corners = cv2.findChessboardCorners(gray, (9, 6), None)
# If found, add object points, image points
if ret == True:
print("Corners found on image " + str(i) + ".")
objpoints.append(objp)
cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria)
imgpoints.append(corners)
print("Drawing corners on image " + str(i) + ".")
# Draw and display the corners
cv2.drawChessboardCorners(smimg, (9, 6), corners, ret)
if saveMarked == True:
cv2.imwrite(os.path.join(outfolder, os.path.basename(fname)), smimg)
cv2.imshow(os.path.basename(fname), smimg)
cv2.waitKey(500)
cv2.destroyAllWindows()
elif pattern == 'Circle Grid':
print("Now finding circles on image " + str(i) + ".")
ret, circles = cv2.findCirclesGrid(gray, (4,11), flags = cv2.CALIB_CB_ASYMMETRIC_GRID)
# If found, add object points, image points
if ret == True:
print("Circles found on image " + str(i) + ".")
objpoints.append(objp)
imgpoints.append(circles)
# Draw and display the circles
cv2.drawChessboardCorners(smimg, (4, 11), circles, ret)
if saveMarked == True:
cv2.imwrite(os.path.join(outfolder, os.path.basename(fname)), smimg)
cv2.imshow(os.path.basename(fname), smimg)
cv2.waitKey(500)
cv2.destroyAllWindows()
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1], paramMtx, None, None)