I am trying to do stereo fish eye camera calibration using OpenCV and Python Here is my code:
import glob import os import random import sys
import numpy as np import cv2
CHESSBOARD_SIZE = (9, 6)
CHESSBOARD_OPTIONS = (cv2.CALIB_CB_ADAPTIVE_THRESH | cv2.CALIB_CB_NORMALIZE_IMAGE | cv2.CALIB_CB_FAST_CHECK) calibration_flags = cv2.fisheye.CALIB_RECOMPUTE_EXTRINSIC + cv2.fisheye.CALIB_CHECK_COND + cv2.fisheye.CALIB_FIX_SKEW
OBJECT_POINT_ZERO = np.zeros((CHESSBOARD_SIZE[0] * CHESSBOARD_SIZE[1], 3), np.float32) OBJECT_POINT_ZERO[:, :2] = np.mgrid[0:CHESSBOARD_SIZE[0], 0:CHESSBOARD_SIZE[1]].T.reshape(-1, 2)
OPTIMIZE_ALPHA = 0.25
TERMINATION_CRITERIA = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001) MAX_IMAGES = 25
if len(sys.argv) != 4: print("Syntax: {0} LEFT_IMAGE_DIR RIGHT_IMAGE_DIR OUTPUT_FILENAME" .format(sys.argv[0])) sys.exit(1)
leftImageDir = sys.argv[1] rightImageDir = sys.argv[2] outputFile = sys.argv[3]
def readImagesAndFindChessboards(imageDirectory): cacheFile = "{0}/chessboards.npz".format(imageDirectory) try: cache = np.load(cacheFile) print("Loading image data from cache file at {0}".format(cacheFile)) return (list(cache["filenames"]), list(cache["objectPoints"]), list(cache["imagePoints"]), tuple(cache["imageSize"])) except IOError: print("Cache file at {0} not found".format(cacheFile))
print("Reading images at {0}".format(imageDirectory))
imagePaths = glob.glob("{0}/*.jpg".format(imageDirectory))
filenames = []
objectPoints = []
imagePoints = []
imageSize = None
for imagePath in sorted(imagePaths):
image = cv2.imread(imagePath)
grayImage = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
newSize = grayImage.shape[::-1]
if imageSize != None and newSize != imageSize:
raise ValueError(
"Calibration image at {0} is not the same size as the others"
.format(imagePath))
imageSize = newSize
ret, corners = cv2.findChessboardCorners(grayImage,
CHESSBOARD_SIZE, cv2.CALIB_CB_FAST_CHECK)
if ret:
filenames.append(os.path.basename(imagePath))
objectPoints.append(OBJECT_POINT_ZERO)
cv2.cornerSubPix(grayImage, corners, (11, 11), (-1, -1),
TERMINATION_CRITERIA)
imagePoints.append(corners)
cv2.drawChessboardCorners(image, CHESSBOARD_SIZE, corners, hasCorners)
cv2.imshow(imageDirectory, image)
# Needed to draw the window
cv2.waitKey(1)
cv2.destroyWindow(imageDirectory)
print("Found corners in {0} out of {1} images"
.format(len(imagePoints), len(imagePaths)))
np.savez_compressed(cacheFile,
filenames=filenames, objectPoints=objectPoints,
imagePoints=imagePoints, imageSize=imageSize)
return filenames, objectPoints, imagePoints, imageSize
(leftFilenames, leftObjectPoints, leftImagePoints, leftSize ) = readImagesAndFindChessboards(leftImageDir) (rightFilenames, rightObjectPoints, rightImagePoints, rightSize ) = readImagesAndFindChessboards(rightImageDir)
if leftSize != rightSize: print("Camera resolutions do not match") sys.exit(1) imageSize = leftSize
filenames = list(set(leftFilenames) & set(rightFilenames)) if (len(filenames) > MAX_IMAGES): print("Too many images to calibrate, using {0} randomly selected images" .format(MAX_IMAGES)) filenames = random.sample(filenames, MAX_IMAGES) filenames = sorted(filenames) print("Using these images:") print(filenames)
def getMatchingObjectAndImagePoints(requestedFilenames, allFilenames, objectPoints, imagePoints): requestedFilenameSet = set(requestedFilenames) requestedObjectPoints = [] requestedImagePoints = []
for index, filename in enumerate(allFilenames):
if filename in requestedFilenameSet:
requestedObjectPoints.append(objectPoints[index])
requestedImagePoints.append(imagePoints[index])
return requestedObjectPoints, requestedImagePoints
leftObjectPoints, leftImagePoints = getMatchingObjectAndImagePoints(filenames, leftFilenames, leftObjectPoints, leftImagePoints) rightObjectPoints, rightImagePoints = getMatchingObjectAndImagePoints(filenames, rightFilenames, rightObjectPoints, rightImagePoints)
TODO: Fix this validation
Keep getting "Use a.any() or a.all()" even though it's already used?!
if (leftObjectPoints != rightObjectPoints).all():
print("Object points do not match")
sys.exit(1)
objectPoints = leftObjectPoints
N_OK = len(leftImagePoints)
objecPoints = np.array([objectPoints]*len(leftImagePoints), dtype=np.float64) leftImagePoints=np.asarray(leftImagePoints, dtype=np.float64) rightImagePoints=np.asarray(rightImagePoints, dtype=np.float64)
objectPoints = np.reshape(objectPoints,(N_OK, 1, CHESSBOARD_SIZE[0]CHESSBOARD_SIZE[1],3)) leftImagePoints = np.reshape(leftImagePoints, (N_OK, 1, CHESSBOARD_SIZE[0]CHESSBOARD_SIZE[1], 2)) rightImagePoints = np.reshape(rightImagePoints,(N_OK, 1, CHESSBOARD_SIZE[0]*CHESSBOARD_SIZE[1], 2))
K_left = np.zeros((3, 3)) D_left = np.zeros((4, 1))
K_right = np.zeros((3, 3)) D_right = np.zeros((4, 1))
R = [np.zeros((1, 1, 3), dtype=np.float64) for i in range(MAX_IMAGES)] T = [np.zeros((1, 1, 3), dtype=np.float64) for i in range(MAX_IMAGES)]
print("Calibrating left fisheye camera...")
rms, _, _, _, _ = cv2.fisheye.calibrate(objectPoints,leftImagePoints,imageSize, K_left,D_left,R,T,calibration_flags, (cv2.TERM_CRITERIA_EPS+cv2.TERM_CRITERIA_MAX_ITER, 30, 1e-6))
print("calibrating right fisheye camera...")
rms, _, _, _, _ = cv2.fisheye.calibrate(objectPoints,rightImagePoints, imageSize,K_right,D_right,R,T,calibration_flags, (cv2.TERM_CRITERIA_EPS+cv2.TERM_CRITERIA_MAX_ITER, 30, 1e-6))
print("calibrating both fisheye cameras together...")
(rms, _, _, _, _) = \ cv2.fisheye.stereoCalibrate( objectPoints, leftImagePoints, rightImagePoints, K_left, D_left, K_right, D_right, imageSize, R, T, (cv2.fisheye.CALIB_RECOMPUTE_EXTRINSIC + cv2.fisheye.CALIB_CHECK_COND + cv2.fisheye.CALIB_FIX_SKEW) );
I am able to do calibration of individual camera but it is throwing errors when it comes to stereo fisheye camera calibration. If anyone could guide me with this would be great help.