摄像机校准代码 - OpenCV错误:断言失败

时间:2018-01-24 08:41:52

标签: python opencv camera-calibration

我正在尝试执行此代码进行相机校准。此代码正在检测角落,但在此错误发生之后。

  

OpenCV错误:未知函数,文件中的断言失败(nimages> 0)   ...... \ modules \ calib3d \ src \ calibration.cpp,第3415行追溯   (最近一次调用最后一次):文件   " C:/ Users / uidn1759 / PycharmProjects / Camera Calibration / calib.py",line   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

我无法找到问题所在,如果有人可以解释,那对我有帮助。谢谢- 这是代码 -

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 个答案:

答案 0 :(得分:1)

在您的代码中,您有:

objpoints = []

但你永远不会追加任何东西。你错过了:

objpoints .append(objp )
行后

imgpoints.append(corners)

还有一件事,你应该在做校准之前检查它们是否有分数,如:

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")