首页 > 解决方案 > OpenCV 立体相机校准错误:断言失败

问题描述

我知道这个问题被问了几次,但答案并没有解决我的问题。

我想校准一对相机以用作立体声输入。但是当我运行代码时,我收到错误消息:

OpenCV(3.4.1) Error: Assertion failed (nimages > 0 && nimages == (int)imagePoints1.total() && (!imgPtMat2 || nimages == (int)imagePoints2.total())) in collectCalibrationData, file /tmp/opencv-20180529-49540-yj8rbk/opencv-3.4.1/modules/calib3d/src/calibration.cpp, line 3133 Traceback (most recent call last): File "/Users/MyName/Pycharm/Project/calibration.py", line 342, in <module> TERMINATION_CRITERIA ) cv2.error: OpenCV(3.4.1) /tmp/opencv-20180529-49540-yj8rbk/opencv-3.4.1/modules/calib3d/src/calibration.cpp:3133: error: (-215) nimages > 0 && nimages == (int)imagePoints1.total() && (!imgPtMat2 || nimages == (int)imagePoints2.total()) in function collectCalibrationData 我的代码是:

def distortion_matrix(path, objpoints, imgpoints):

  for item in os.listdir(path):
    if item.endswith(".jpg"):
        cap = cv2.VideoCapture(path+item, cv2.CAP_IMAGES)

        ret, img = cap.read()  # Capture frame-by-frame

        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

        keypoints = blobDetector.detect(gray)  # Detect blobs.

                    im_with_keypoints = cv2.drawKeypoints(img, keypoints, np.array([]), (0, 255, 0),
                                              cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
        im_with_keypoints_gray = cv2.cvtColor(im_with_keypoints, cv2.COLOR_BGR2GRAY)
        ret, corners = cv2.findCirclesGrid(im_with_keypoints, (4, 11), None,
                                           flags=cv2.CALIB_CB_ASYMMETRIC_GRID)  

        if ret == True:
            objpoints.append(objp)  

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


  cap.release()

_, leftCameraMatrix, leftDistortionCoefficients, _, _ , objpoints0, imgpoints0 = distortion_matrix("./calibration/left/", objpoints0, imgpoints0)
_, rightCameraMatrix, rightDistortionCoefficients, _, _, objpoints1, imgpoints1 = distortion_matrix("./calibration/right/", objpoints1, imgpoints1)



(_, _, _, _, _, rotationMatrix, translationVector, _, _) = cv2.stereoCalibrate( objp, imgpoints0, imgpoints1, 
                                                                            leftCameraMatrix, leftDistortionCoefficients, 
                                                                            rightCameraMatrix, rightDistortionCoefficients, 
                                                                            imageSize, None, None, None, None,
                                                                            cv2.CALIB_FIX_INTRINSIC, TERMINATION_CRITERIA )

大多数时候,当这个被抛出时,似乎 Message 引用的是空的或未均匀填充的数组(imgpoint 和 objpoint)。但最后都得到了长度 20(我扫描了 20 张图像,所以这看起来是正确的)并且数组的每个单元格都存储了 44 个数组(我使用的圆形网格有 44 个点,所以这看起来也正确)。

**编辑:** 我的 objp、imgpoint 和 objpoint 定义如下:

objp = np.zeros((np.prod(pattern_size), 3), np.float32)
objp[0]  = (0, 0, 0)
objp[1]  = (0, 2, 0)
objp[2]  = (0, 4, 0)
objp[3]  = (0, 6, 0)
...


objpoints0 = []
objpoints1 = []

imgpoints0 = []
imgpoints1 = []

** 编辑 2:**

如果 NUM_IMAGES 代表图像数量,我想我现在已经掌握了。但只有在我调用distortion_matrix(). 然后代码就可以完成了。我需要测试结果,但至少这个问题似乎得到了解决。

非常感谢

标签: pythonopencvcameracalibrationstereoscopy

解决方案


你说你正在做立体校准,是否有任何情况下你的网格上的某些点从其他相机看不到?当您的一个视图无法检测到校准图案上的所有点时,可能会出现此错误。要考虑的三点是
1- 确保您的对象点是 3d
2- 确保您的左点、右点和对象点具有相同的大小(视图数量)。
3-确保您的左点、右点和对象点在列表的每个索引处具有相同数量的点。

编辑:您的对象点 objp 必须包含 3d 点的列表/向量,目前它的形状类似于 (44, 3),它必须是 (NUM_IMAGES, 44, 3)。你可以用objp = np.repeat(objp[np.newaxis, :, :], NUM_IMAGES, axis=0)


推荐阅读