相机校准代码-OpenCV错误:断言失败 [英] Camera calibration code - OpenCV Error: Assertion failed
问题描述
我正在尝试执行此代码进行相机校准.这段代码正在检测拐角,但是在出现该错误之后.
I'm trying to execute this code for camera calibration. This code is detecting corners but after that error is coming.
OpenCV错误:未知函数,文件中的断言失败(nimages> 0)...... \ modules \ calib3d \ src \ calibration.cpp,第3415行追溯(最近通话最近):文件"C:/Users/uidn1759/PycharmProjects/Camera Calibration/calib.py",行25英寸ret,mtx,dist,rvecs,tvecs = cv2.calibrateCamera(objpoints,imgpoints,gray.shape [::-1],None,None)cv2.error:...... \ modules \ calib3d \ src \ calibration.cpp:3415:错误:(-215)nimages> 0
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)
推荐答案
在您的代码中,您具有:
In your code you have:
objpoints = []
,但是您从不添加任何内容.您不见了:
but you never append anything to it. You are missing:
objpoints .append(objp )
行后
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")
这篇关于相机校准代码-OpenCV错误:断言失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!