简体   繁体   English

在python中使用opencv时断言失败

[英]Assertion failed when use opencv in python

I am doing Camera Calibration in opencv in python and I followed the tutorials on this page . 我在python的opencv中进行相机校准,并且按照本页上的教程进行操作。 My code is completely copied from the page with tiny adjustment on the parameters. 我的代码是完全从页面复制的,而对参数的调整很小。

Code: 码:

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

but when I run it, an error shows up as: 但是当我运行它时,错误显示为:

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. 这是我的形象。


I have searched on the Internet that many people are also confronted with this problem. 我在互联网上搜索到很多人也面临这个问题。 But most of the solution on blog is saying that this is caused by the type of the first and second parameters of calibrateCamera() which is objpoints and imgpoints . 但是博客上的大多数解决方案都说这是由calibrateCamera()的第一个和第二个参数的类型引起的,它们是objpointsimgpoints But those are all solution for opencv on c++. 但是这些都是在c ++上针对opencv的解决方案。

Could any one tell me how to solve it in python? 谁能告诉我如何在python中解决它?

The number of entries in objpoints and imgpoints must be the same. objpoints和imgpoints中的条目数必须相同。 This assertion means they aren't. 这个主张意味着他们不是。 It looks like you're creating a set of 6*7=42 objpoints, which is intended for a chessboard of 6x7 intersections, but your actual chessboard has 8*11=88 intersections. 看起来您正在创建一组6 * 7 = 42个objpoints,用于6x7交集的棋盘,但是您实际的棋盘有8 * 11 = 88交集。 So as you process images, your objpoints and imgpoints lists get different lengths. 因此,在处理图像时,objpoints和imgpoints列表的长度会有所不同。 You need to modify your creation/initialization of objp to have 8*11=88 points, whose coordinates correspond to the real physical coordinates on your chessboard. 您需要修改objp的创建/初始化,使其具有8 * 11 = 88点,其坐标对应于棋盘上的实际物理坐标。

To do this, you'll need to really understand the code you are using. 为此,您需要真正了解所使用的代码。 Putting in more debug statements will help you to trace through what is happening in your code. 放入更多调试语句将帮助您跟踪代码中发生的情况。

And note that the Python API to OpenCV is just a wrapper around the C++ API, so any solution for using OpenCV with C++ are (usually) relevant in Python too. 还要注意,OpenCV的Python API只是C ++ API的包装,因此,将OpenCV与C ++结合使用的任何解决方案(通常)在Python中也很重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM