Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Chessboard and Circle calibration provide radically different results

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)

Chessboard and Circle calibration provide radically different results

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)

EDIT: Here are the outputs as well as my pretty print statements:

Chessboard:

camera matrix:
[[  1.88284099e+03   0.00000000e+00   9.17371014e+02]
 [  0.00000000e+00   1.88512764e+03   4.92135568e+02]
 [  0.00000000e+00   0.00000000e+00   1.00000000e+00]]
distortion coefficients:
[[  6.84320591e-02   2.23885561e-02  -4.68515125e-03   1.36915007e-05  8.41989278e+00]]

Calibrated Focal Length (mm): 35.8333816671
PPA X (px): 3669.48405497
PPA Y (px): 1968.54227198
k1: 0.0684320591357
k2: 0.0223885561237
k3: 8.41989278488
p1: -0.00468515124597
p2: 1.36915007217e-05

2.33% difference between nominal and calibrated focal length
RMS reprojection error (px): 0.438001043203

Circle grid:

camera matrix:
[[  1.73132731e+04   0.00000000e+00   7.77840540e+02]
 [  0.00000000e+00   3.58839571e+04   2.71340491e+02]
 [  0.00000000e+00   0.00000000e+00   1.00000000e+00]]
distortion coefficients:
[[  8.45040736e+01  -2.60062724e+04  -8.98376313e-01  -2.19077639e-01  1.46813984e+03]]

Calibrated Focal Length (mm): 505.905658805
PPA X (px): 3111.36215828
PPA Y (px): 1085.36196308
k1: 84.5040736301
k2: -26006.2723672
k3: 1468.13984356
p1: -0.898376312689
p2: -0.219077638716

93.08% difference between nominal and calibrated focal length
RMS reprojection error (px): 34.0621258094