首页 > 解决方案 > OpenCV "cv2.omnidir.calibrate" throws an Assertion Error

问题描述

I am working on Ubuntu 16.04, using python 3.5.2 and OpenCV 3.4.0.

I am trying to calibrate images taken by wide angle GoPro camera using omnidirectional calibration technique in OpenCV. I have tried to write this C++ omnidirectional calibration tutorial in python but I am getting an Assertion error.

This is the code that I am currently running :

#importing opencv, glob and numpy libraries
import cv2
import numpy as np
import glob
import imutils as im
    
#reading all JPEG images in current directory
images = glob.glob('*.jpg')

#criteria for refining corners
criteria = (cv2.TERM_CRITERIA_MAX_ITER + cv2.TERM_CRITERIA_EPS, 30, 0.001)

#flags to control omnidir.calibrate and omnidir.undistortImage functions
calib_flag = cv2.omnidir.CALIB_USE_GUESS
    
#setting up grid for the 3D world points
nx = 9; ny = 6 
objp = np.zeros((nx*ny,3), np.float32)
objp[:,:2] = np.mgrid[0:nx,0:ny].T.reshape(-1,2)

#final list of world and image points
objpoints = []
imgpoints = []

for img_name in images :
    #reading images one by one and normalizing their image histograms 
    img = cv2.imread(img_name)
    img = im.resize(img, width = 1260)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    #finding chessboard corners in the image
    found, corners = cv2.findChessboardCorners(gray, (nx,ny), None)

    #if corners are found, append object points and refine the corners to append them to image points
    if found :
        objpoints.append(objp)
    
        #refining corners using subpixel gradients
        corners = cv2.cornerSubPix(gray, corners, (11,11), (-1,-1), criteria)
        corners = corners.reshape((54,2))    #because corners is (54,1,2) after cornerSubPix
        imgpoints.append(corners)

#trying to force the datatype to float64, comment out for float 32
imgpoints = np.array(imgpoints, dtype = np.float64)
objpoints = np.array(objpoints, dtype = np.float64)

print(imgpoints.shape,objpoints.shape)

#calibrating the camera using omnidirectional calibrate function
M = np.zeros((3,3))
dist_coeff = np.zeros((4,1))
xi = np.array([])
idx = np.array([])
ret, M, xi, dist_coeff, rVecs, tVecs, idx = cv2.omnidir.calibrate(objpoints, imgpoints, gray.shape[::-1], M, xi, dist_coeff, calib_flag, criteria)
#Assertion error in above line

Below is the error that I got when I ran the code in the terminal :

OpenCV Error: Assertion failed ((patternPoints.type() == (((6) & ((1 << 3) - 1)) + (((3)-1) << 3)) && imagePoints.type() == (((6) & ((1 << 3) - 1)) + (((2)-1) << 3))) || (patternPoints.type() == (((5) & ((1 << 3) - 1)) + (((3)-1) << 3)) && imagePoints.type() == (((5) & ((1 << 3) - 1)) + (((2)-1) << 3)))) in calibrate, file /home/pradyu/opencv_contrib-3.4.0/modules/ccalib/src/omnidir.cpp, line 1067

Traceback (most recent call last):

  File "omnicalib.py", line 54, in <module>

      ret, M, xi, dist_coeff, rVecs, tVecs, idx = cv2.omnidir.calibrate(objpoints, imgpoints, gray.shape[::-1], M, xi, dist_coeff, calib_flag, criteria)

cv2.error: /home/pradyu/opencv_contrib-3.4.0/modules/ccalib/src/omnidir.cpp:1067: error: (-215) (patternPoints.type() == (((6) & ((1 << 3) - 1)) + (((3)-1) << 3)) && imagePoints.type() == (((6) & ((1 << 3) - 1)) + (((2)-1) << 3))) || (patternPoints.type() == (((5) & ((1 << 3) - 1)) + (((3)-1) << 3)) && imagePoints.type() == (((5) & ((1 << 3) - 1)) + (((2)-1) << 3))) in function calibrate

I checked the assertion in line 1067 of opencv-contrib module source code and it is supposed to be a datatype assertion for the image and world points in the calibrate function : World points - CV_64FC3 (or 32) and Image points - CV_64FC2 (or 32) I've made sure that the respective arrays have the correct types and number of channels, but the error persists.

标签: pythonpython-3.xopencv

解决方案


推荐阅读