OpenCV立体摄像机校准错误:断言失败

时间:2018-09-09 14:39:11

标签: python opencv camera calibration stereoscopy

我知道这个问题曾被问过几次,但是答案并不能解决我的问题。

我想校准一对用作立体声输入的摄像机。 但是,当我运行代码时,出现错误消息:

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 )

在大多数情况下,抛出该消息似乎是指向空的或未均匀填充的数组(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()之后添加新轴时才可以。 然后,代码即可完成。我需要测试结果,但是至少这个问题似乎可以解决。

非常感谢您

1 个答案:

答案 0 :(得分:0)

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

编辑:您的对象点objp必须包含3d点的列表/向量,当前其形状为(44,3),必须为(NUM_IMAGES,44,3)。您可以使用objp = np.repeat(objp[np.newaxis, :, :], NUM_IMAGES, axis=0)