在python中使用opencv时,断言失败

在python中使用opencv时,断言失败,python,opencv,camera-calibration,distortion,Python,Opencv,Camera Calibration,Distortion,我正在用python的opencv进行相机校准,并遵循了上的教程。我的代码完全是从页面上复制的,只是对参数做了微小的调整 代码: import numpy as np import cv2 import glob # termination criteria criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001) # prepare object points, like (0,0,0), (1,0

我正在用python的opencv进行相机校准,并遵循了上的教程。我的代码完全是从页面上复制的,只是对参数做了微小的调整

代码:

import numpy as np
import cv2
import glob

# termination criteria
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)

# prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)
objp = np.zeros((6*7,3), np.float32)
objp[:,:2] = np.mgrid[0:7,0:6].T.reshape(-1,2)

# Arrays to store object points and image points from all the images.
objpoints = [] # 3d point in real world space
imgpoints = [] # 2d points in image plane.

images = glob.glob('../easyimgs/*.jpg')
print('...loading')
for fname in images:
    print(f'processing img:{fname}')
    img = cv2.imread(fname)
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    print('grayed')
    # Find the chess board corners
    ret, corners = cv2.findChessboardCorners(gray, (8, 11),None)

    # If found, add object points, image points (after refining them)
    if ret == True:
        print('chessboard detected')
        objpoints.append(objp)

        corners2 = cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria)
        imgpoints.append(corners2)

        # Draw and display the corners
        img = cv2.drawChessboardCorners(img, (8,11), corners2,ret)
        cv2.namedWindow('img',0)
        cv2.resizeWindow('img', 500, 500)
        cv2.imshow('img',img)
        cv2.waitKey(500)
        cv2.destroyAllWindows()


img2 = cv2.imread("../easyimgs/5.jpg")
print(f"type objpoints:{objpoints[0].shape}")
print(f"type imgpoints:{imgpoints[0].shape}")

ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1],None,None)
h,  w = img2.shape[:2]
newcameramtx, roi=cv2.getOptimalNewCameraMatrix(mtx,dist,(w,h),1,(w,h))
dst = cv2.undistort(img2, mtx, dist, None, newcameramtx)

# crop the image
x,y,w,h = roi
dst = dst[y:y+h, x:x+w]
cv2.namedWindow('result', 0)
cv2.resizeWindow('result', 400, 400)
cv2.imshow('result',dst)

cv2.destroyAllWindows()
但当我运行它时,一个错误显示为:

Traceback (most recent call last):
  File "*/undistortion.py", line 51, in <module>
    ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1],None,None)
cv2.error: OpenCV(3.4.2) C:\projects\opencv-python\opencv\modules\calib3d\src\calibration.cpp:3143: error: (-215:Assertion failed) ni == ni1 in function 'cv::collectCalibrationData'
回溯(最近一次呼叫最后一次):
文件“*/undistortion.py”,第51行,在
ret、mtx、dist、rvecs、tvecs=cv2.校准曲线(OBJ点、IMG点、灰色.形状[:-1],无,无)
cv2.error:OpenCV(3.4.2)C:\projects\OpenCV python\OpenCV\modules\calib3d\src\calibration.cpp:3143:error:(-215:断言失败)函数“cv::CollectionCalibrationData”中的ni==ni1


我在网上搜索到很多人也面临着这个问题。但博客上的大多数解决方案都说这是由
calibleCamera()
的第一个和第二个参数类型造成的,即
objpoints
imgpoints
。但是这些都是C++上OpenCV的解决方案。p>
有谁能告诉我如何在python中解决这个问题吗?

objpoints和imgpoints中的条目数必须相同。这个断言意味着它们不是。看起来您正在创建一组6*7=42个对象点,用于6x7个交点的棋盘,但实际的棋盘有8*11=88个交点。因此,在处理图像时,objpoints和imgpoints列表的长度不同。您需要修改objp的创建/初始化,使其具有8*11=88个点,其坐标对应于棋盘上的实际物理坐标

要做到这一点,您需要真正理解您正在使用的代码。输入更多的调试语句将帮助您跟踪代码中发生的事情

注意到OpenCV的Python API只是围绕C++ API的包装器,所以使用C++的OpenCV的任何解决方案也通常在Python中相关。