【问题标题】:Assertion failed when use opencv in python在 python 中使用 opencv 时断言失败
【发布时间】:2019-02-11 23:27:46
【问题描述】:

我正在 python 中的 opencv 中进行相机校准,我按照this page 上的教程进行操作。我的代码完全是从页面中复制过来的,只是对参数进行了微小的调整。

代码:

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'

Here is my image.


我在网上搜索过很多人也遇到过这个问题。但是博客上的大多数解决方案都说这是由calibrateCamera()的第一个和第二个参数的类型引起的,即objpointsimgpoints。但这些都是 c++ 上 opencv 的解决方案。

谁能告诉我如何用python解决它?

【问题讨论】:

    标签: python opencv camera-calibration distortion


    【解决方案1】:

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

    为此,您需要真正了解您正在使用的代码。加入更多调试语句将帮助您跟踪代码中发生的情况。

    请注意,OpenCV 的 Python API 只是 C++ API 的包装,因此任何将 OpenCV 与 C++ 结合使用的解决方案(通常)也与 Python 相关。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-20
      • 2014-02-10
      • 2022-07-01
      • 1970-01-01
      • 2021-09-28
      相关资源
      最近更新 更多