1
votes

I'm trying to execute this code for camera calibration. This code is detecting corners but after that error is coming.

OpenCV Error: Assertion failed (nimages > 0) in unknown function, file ......\modules\calib3d\src\calibration.cpp, line 3415 Traceback (most recent call last): File "C:/Users/uidn1759/PycharmProjects/Camera Calibration/calib.py", line 25, in ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1],None,None) cv2.error: ......\modules\calib3d\src\calibration.cpp:3415: error: (-215) nimages > 0

I'm not able to find where is the problem, if someone can explain, that would be helpful for me. Thanks- Here is the code -

import numpy as np
import cv2
import glob
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
objp = np.zeros((6*7,3), np.float32)
objp[:,:2] = np.mgrid[0:7,0:6].T.reshape(-1,2)
objpoints = []
imgpoints = []
images = glob.glob('/usr/local/share/OpenCV/samples/cpp/chess*.jpg')
img = cv2.imread("2.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

ret = False
ret, corners = cv2.findChessboardCorners(gray, (7,6))
if ret == True:
    cv2.cornerSubPix(gray, corners, (11,11), (-1,-1), criteria)
    imgpoints.append(corners)
    # Draw and display the corners
    cv2.drawChessboardCorners(img, (7,6), corners, ret)
    cv2.imshow('img',img)
    cv2.imwrite('Corners_detected.jpg', img, None)
    cv2.waitKey(500)
cv2.destroyAllWindows()

ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, 
gray.shape[::-1],None,None)
1
you have to check if you have the points needed to do the calibration. The error tells you that you are not passing "images" to it, this means that imgpoints is empty. imgpoints can be empty if ret is false in the previous block. objpoints is always empty, because you never append to it (it should be appended every time you do it for imgpoints)api55
Thank you for the response @api55. But the code is returning ret as true and the image in which corners are detected is being displayed. But it is failing and showing error at ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1],None,None)Sameer J
I gave two possible errors, check for the second one :) the one with objpoints. objpoints = [] is the only time it is being objpoints is called before calibrateCamera. You are missing something like objpoints .append(objp ) after the line imgpoints.append(corners).api55
Now got it. As you told object points were not appended. Thanks a lot for your help. :) Results are perfect.Sameer J
I will add it as an answer, you may want to accept it so that other people can find a solution to a similar problemapi55

1 Answers

2
votes

In your code you have:

objpoints = []

but you never append anything to it. You are missing:

objpoints .append(objp )

after the line

imgpoints.append(corners)

One extra thing, you should check if they have points before doing calibration like:

if len(objpoints) == len(imgpoints) and len(objpoints) > 0:
    ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1],None,None)
else:
    print("not enough points to calibrate")