在python中使用opencv时断言失败

抒情诗

我在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'

这是我的形象。


我在互联网上搜索到许多人也面临这个问题。但是博客上的大多数解决方案都说这是由第一个参数和第二个参数的类型引起的,该参数calibrateCamera()objpointsand imgpoints但是这些都是在c ++上针对opencv的解决方案。

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

忠祖瓦拉

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

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

还要注意,OpenCV的Python API只是C ++ API的包装,因此,将OpenCV与C ++结合使用的任何解决方案(通常)在Python中也很重要。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在UITableViewController中使用UISearchDisplayController时断言失败

Python 和 OpenCV:在 Exe 文件中使用 IP 摄像机断言失败

使用 Opencv 时 C++ 调试断言失败

OpenCV Python Split -> Laplacian -> HoughCircles = 断言失败。

OpenCV错误:使用COLOR_BGR2GRAY函数时断言失败

错误:(-215:断言失败)使用OpenCV处理轮廓时,npoints> 0

OpenCV calibrateCamera()断言失败

OpenCV断言以ROI失败

Opencv错误:断言失败

OpenCV:断言失败

在python断言中,断言失败时如何打印条件?

断言失败:在Flutter中使用Navigator.popUntil()时,!_debugLocked不为真

在python中断言失败时如何退出?

断言失败时的Python unittest调用函数

计算HOG功能时,opencv断言失败错误438

opencv -215:尝试打印人脸坐标时断言失败

断言失败后在python中使用xmlrunner和unittest继续测试

C ++和OpenCV断言失败

人脸识别opencv断言失败

在业力规则中使用数字生成器时,uint_not_usable_without_attribute静态断言失败

在-[AVPlayerPeriodicCaller initWithPlayer:interval:queue:block:]中使用断言失败更新进度滑块时,AVPlayer导致崩溃

为什么“断言失败:>!不在(go ...)块中使用”

Python底图“断言失败”

断言失败时继续执行Python的单元测试

调试仅在未调试时失败的python unittest断言的策略

使用isdigit比较unicode时出现调试断言失败错误

在python中使用OpenCV时视频不刷新

在断言异常时,如何知道何时在ScalaTest Matchers中使用“ a”,“ an”或“ the”?

在熊猫中使用from_records时断言错误