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 )

大多数时候,当它被抛出时,消息似乎指的是空的数组(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- 确保您的对象点
是 3d
的 2- 确保您的左点、右点和对象点具有相同的大小(视图数(。
3-确保您的左点,右点和对象点在每个列表索引处具有相同数量的点。

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

实现这一目标

最新更新