'error: (-215:Assertion failed) count >= 0 in function 'cornerSubPix' in Camera270° Calibration in python
I am going to calibrate camera 270° with following code, but It has following assertion error,
in corners2 = cv2.cornerSubPix(gray, corners, (3,3),(-1,-1), criteria), in error: (-215:Assertion failed) count >= 0 in function 'cornerSubPix' .
Exception has occurred: error OpenCV(4.5.3) /tmp/pip-wheel-pd499c9i/
enter code hereopencv-python_3a15e83eee864e65b7311a199a94e9f1/opencv/modules/imgproc/src/cornersubpix.cpp:58: error: (-215:Assertion failed) count >= 0 in function 'cornerSubPix'enter code hereFile "/home/pi/Desktop/calibratfischeye3.py", in corners2 = cv2.cornerSubPix(gray, corners, (3,3),(-1,-1), criteria)
I will be glad somebody help me.
import cv2
import numpy as np
import os
import glob
# ****Defining the dimensions of checkerboard
CHECKERBOARD = (6,9)
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
#**** Creating vector to store vectors of 3D points for each checkerboard image
objpoints = []
# ****Creating vector to store vectors of 2D points for each checkerboard image
imgpoints = []
#**** Defining the world coordinates for 3D points
objp = np.zeros((1, CHECKERBOARD[0] * CHECKERBOARD[1], 3), np.float32)
objp[0,:,:2] = np.mgrid[0:CHECKERBOARD[0], 0:CHECKERBOARD[1]].T.reshape(-1, 2)
prev_img_shape = None
#**** Extracting path of individual image stored in a given directory
images = glob.glob('/home/pi/Desktop/image1pic18.jpg')
img = cv2.imread(fname)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret, corners = cv2.findChessboardCorners(gray, CHECKERBOARD, cv2.CALIB_CB_ADAPTIVE_THRESH + cv2.CALIB_CB_FAST_CHECK + cv2.CALIB_CB_NORMALIZE_IMAGE)
if ret != True:
objpoints.append(objp)
#**** refining pixel coordinates for given 2d points.
corners2 = cv2.cornerSubPix(gray, corners, (3,3),(-1,-1), criteria)
imgpoints.append(corners2)
#**** Draw and display the corners
img = cv2.drawChessboardCorners(img, CHECKERBOARD, corners2, ret)
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
h,w = img.shape[:2]
#****"""
#****Performing camera calibration by
#****passing the value of known 3D points (objpoints)
#****and corresponding pixel coordinates of the
#****detected corners (imgpoints)
#****"""
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1], None, None)
Solution 1:[1]
Why are you checking if ret != True:? You want to check if ret == True: instead because you can only refine the corners if you found any before.
After changing this, your code worked for me.
If you didn't find any chessboard corners, the most likely cause is that you got the dimensions of the chessboard wrong. CHECKERBOARD should contain the inner corners of the chessboard, so number of boxes in that direction - 1.
Anyway, I found this question because I had the same error message but for a different use case. I solved it in the meantime. My problem was that the corners I wanted to refine were of a wrong type. Casting the corners to np.float32 solved it for me. float64 didn't work.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Olli |
