Cv2.omnidir.calibrate() error

Hi! I’m working on my master thesis and want to calibrate a camera with 194 deg FOV, therefore I’m using the omnidirectional calibration method in OpenCV as I’ve understood that this is recommended for cameras with a FOV >180 deg. When trying to compile I get the following error thrown:

  Message=OpenCV(4.5.5) D:\a\opencv-python\opencv-python\opencv_contrib\modules\ccalib\src\omnidir.cpp:854: error: (-215:Assertion failed) !objectPoints.empty() && objectPoints.type() == CV_64FC3 in function 'cv::omnidir::internal::computeJacobian'

  Source=C:\Users\SLINDE\source\repos\Test.py
  StackTrace:
  File "C:\Users\SLINDE\source\repos\Test.py", line 66, in getOmnidirectionalCalibrationParams
    ret, K, xi, D, rvecs, tvecs, idx = cv2.omnidir.calibrate(objectpoints, imagepoints, image_size, K, xi, D, calibration_flag, subpixel_criteria)
  File "C:\Users\SLINDE\source\repos\Test.py", line 74, in <module> (Current frame)
    ret, K, xi, D, rvecs, tvecs, idx = getOmnidirectionalCalibrationParams(objectpoints, imagepoints, omnidir_calibration_flags, subpixel_criteria)

I’ve googled around and found similar errors but nothing have helped me solve this issue. I would highly appreciate if anyone could try to guide me. Below you can see my code, the error is thrown at line 66.

import glob
import os
import numpy as np
import matplotlib.pyplot as plt
import cv2
assert cv2.version[0] >= ‘3’

#Globals
image_path = ‘C:/Users/SLINDE/Documents/Checkboard/’
output_path = ‘C:/Users/SLINDE/Documents/Checkboard/output/’
image_extension = ‘jpg’
image_size = (4032, 3024)
checkerboard_size = (10,15)
subpixel_criteria = (cv2.TERM_CRITERIA_EPS+cv2.TERM_CRITERIA_MAX_ITER, 30, 0.1)
fisheye_calibration_flags = cv2.fisheye.CALIB_RECOMPUTE_EXTRINSIC+cv2.fisheye.CALIB_CHECK_COND+cv2.fisheye.CALIB_FIX_SKEW
omnidir_calibration_flags = cv2.omnidir.CALIB_USE_GUESS
draw_chessboard_marked_images = False

#OpenCV Checkboard setup
def getObjectAndImagePoints(imagePath, imageExtension, checkerboardSize, subpixelCriteria, drawChessboardMarkedImages):
objp = np.zeros((1, checkerboardSize[0]*checkerboardSize[1], 3), np.float32)
objp[0,:,:2] = np.mgrid[0:checkerboardSize[0], 0:checkerboardSize[1], ].T.reshape(-1, 2)
_img_shape = None
objpoints = [] #3d point in real world space
imgpoints = [] #2d points in image plane.

images = glob.glob(imagePath + '*.' + imageExtension)
for fname in images:
    img = cv2.imread(fname)
    if _img_shape == None:
        _img_shape = img.shape[:2]
    else:
        assert _img_shape == img.shape[:2], "All images must share the same size."
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    #Find the chess board corners
    ret, corners = cv2.findChessboardCorners(gray, checkerboardSize, cv2.CALIB_CB_ADAPTIVE_THRESH+cv2.CALIB_CB_FAST_CHECK+cv2.CALIB_CB_NORMALIZE_IMAGE)

    print(corners.shape)

    #If found, add object points, image points (after refining them)
    if ret == True:
        cv2.cornerSubPix(gray, corners, (5,5), (-1,-1), subpixelCriteria)
        #corners = corners.reshape((35,2)) # This is necessary only for omnidirectional calibration
        imgpoints.append(corners)
        objpoints.append(objp)

        if drawChessboardMarkedImages:
            gray2 = gray
            cv2.drawChessboardCorners(gray2, checkerboardSize, corners, ret)
            cv2.imwrite(os.path.splitext(fname)[0] + '_' + 'CHESSCORNERS.png', gray2)

return (objpoints, imgpoints)

def getOmnidirectionalCalibrationParams(objectpoints, imagepoints, calibration_flag, subpixel_criteria):
K = np.zeros((3,3))
D = np.zeros((1,4))
xi = np.array([])
idx = np.array([])

no_images = len(imagepoints)
no_points = len(imagepoints[0])
objectpoints = np.reshape(objectpoints, (no_images, 1, no_points, 3))
imagepoints = np.reshape(imagepoints, (no_images, 1, no_points, 2))
 
ret, K, xi, D, rvecs, tvecs, idx = cv2.omnidir.calibrate(objectpoints, imagepoints, image_size, K, xi, D, calibration_flag, subpixel_criteria)
 
return ret, K, xi, D, rvecs, tvecs, idx

#Find camera calibration parameters
objectpoints, imagepoints = getObjectAndImagePoints(image_path, image_extension, checkerboard_size, subpixel_criteria, draw_chessboard_marked_images)
print(imagepoints)
ret, K, xi, D, rvecs, tvecs, idx = getOmnidirectionalCalibrationParams(objectpoints, imagepoints, omnidir_calibration_flags, subpixel_criteria)

please, no images of code or errors (entirely useless here), replace with TEXT, thanks !

the error you show complains about objectPoints being either empty or in the wrong format. print out shape, type, dtype, and tell us !

related: Opencv-contrib-python Assertion error