OpenCV Error: Assertion failed (nimages > 0 && nimages == (int)imagePoints1.tot ........ line3106

OpenCV Error: Assertion failed (nimages > 0 && nimages == (int)imagePoints1.tot ........ line3106

OLD: 尝试使用 OpenCV 教程进行相机校准。
请在第一个 python 代码部分下面的“编辑”之后寻找第二部分

我收到此错误:

OpenCV Error: Assertion failed (nimages > 0 && nimages == (int)imagePoints1.total() && (!imgPtMat2 || nimages == (int)imagePoints2.total())) in collectCalibrationData, file /build/opencv-L2vuMj/opencv-3.2.0+dfsg/modules/calib3d/src/calibration.cpp, line 3106
Traceback (most recent call last):
  File "cse598a2.py", line 46, in <module>
    None)
cv2.error: /build/opencv-L2vuMj/opencv-3.2.0+dfsg/modules/calib3d/src/calibration.cpp:3106: error: (-215) nimages > 0 && nimages == (int)imagePoints1.total() && (!imgPtMat2 || nimages == (int)imagePoints2.total()) in function collectCalibrationData

我看到过类似的错误,但未能找到 错误代码为 3106 的错误。

我也尝试查找文件calibration.cpp,但在Ubuntu 18.04 中找不到它,尝试查看问题出在哪里。尺寸确实按照其他帖子中的说明进行检查。

这是我的代码

import cv2
import os
import numpy as np
import sys


path = os.path.expanduser('~') + '/catkin_ws/src/cse598a2/images/task_1'
entries = os.listdir(path)


object_points = np.zeros((6*9,3), np.float32)
object_points[:,:2] = np.mgrid[0:9,0:6].T.reshape(-1,2)

objectt_points = [] # A 3d point in real world space
image_points = [] # for 2d points in image plane.

c = 0

for each in entries:
    if c == 5: break;
    if (each[0] == 'r'):
        image = (cv2.imread(path + '/' + each))
    
        image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        value_return, corners = cv2.findChessboardCorners(image_gray,
                                                      (6,9),
                                                      flags=cv2.CALIB_CB_FAST_CHECK)
        if value_return:
            objectt_points.append(object_points)
            image_points.append(corners)
        
    print len(objectt_points)
    print len(image_points)
    c = c + 1


#print (objectt_points)
#print (image_points)

# sys.exit(0)

value_return, matrix, dist, rvecs, tvecs = cv2.calibrateCamera(object_points,
                                                                   image_points,
                                                                   image_gray.shape[::-1],
                                                                   None,
                                                                   None)

height,  width = image_gray.shape[:2]
newcameramtx, region_interest = cv2.getOptimalNewCameraMatrix(mtx, dist, (width,height), 1, (width,height))

image_undistorted = cv2.undistort(image_gray, matrix, distance, None, newcameramtx)
x, y, width, height = region_interest
image_undistorted = image_undistorted[y:y+height, x:x+height]

print dist.shape

编辑:第二部分
这一次,我无法解决问题。上次的问题是我提供的参数有误,这次先查了下,不明白是哪里出了问题。我检查了代码示例以查看方法。 Stereo Calibration Example on Stack Overflow 这是错误

OpenCV Error: Assertion failed (nimages > 0 && nimages == (int)imagePoints1.total() && (!imgPtMat2 || nimages == (int)imagePoints2.total())) in collectCalibrationData, file /build/opencv-L2vuMj/opencv-3.2.0+dfsg/modules/calib3d/src/calibration.cpp, line 3106
Traceback (most recent call last):
  File "cse598a2_t2.py", line 81, in <module>
    flags=cv2.CALIB_FIX_INTRINSIC))
cv2.error: /build/opencv-L2vuMj/opencv-3.2.0+dfsg/modules/calib3d/src/calibration.cpp:3106: error: (-215) nimages > 0 && nimages == (int)imagePoints1.total() && (!imgPtMat2 || nimages == (int)imagePoints2.total()) in function collectCalibrationData

import cv2
import os
import numpy as np
import sys


path = os.path.expanduser('~') + '/catkin_ws/src/cse598a2/images/task_2'
entries = os.listdir(path)


object_file_storage_L = cv2.FileStorage('camera_L.yml', flags=cv2.FILE_STORAGE_READ)
object_file_storage_R = cv2.FileStorage('camera_R.yml', flags=cv2.FILE_STORAGE_READ)

dis_L = object_file_storage_L.getNode('cam_L_dispar').mat()
dis_R = object_file_storage_R.getNode('cam_R_dispar').mat()

mat_L = object_file_storage_L.getNode('cam_L_matrix').mat()
mat_R = object_file_storage_R.getNode('cam_R_matrix').mat()

object_file_storage_L.release()
object_file_storage_R.release()


object_points = np.zeros((6*9,3), np.float32)
object_points[:,:2] = np.mgrid[0:9,0:6].T.reshape(-1,2)

objectt_points = [] # A 3d point in real world space
image_points_L = [] # for 2d points in image plane.
image_points_R = [] # for 2d points in image plane.

file_l0 = 'left_0.png'
file_l1 = 'left_1.png'
file_r0 = 'right_0.png'
file_r1 = 'right_1.png'

image = cv2.imread(path + '/' + file_l0)
image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
value_return, corners = cv2.findChessboardCorners(image_gray, (6, 9), flags=cv2.CALIB_CB_FAST_CHECK)
print corners.shape

if value_return:
    objectt_points.append(object_points)
    image_points_L.append(corners)

image = cv2.imread(path + '/' + file_l1)
image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
value_return, corners = cv2.findChessboardCorners(image_gray, (6, 9), flags=cv2.CALIB_CB_FAST_CHECK)
print corners.shape

if value_return:
    objectt_points.append(object_points)
    image_points_L.append(corners)

image = cv2.imread(path + '/' + file_r0)
image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
value_return, corners = cv2.findChessboardCorners(image_gray, (6, 9), flags=cv2.CALIB_CB_FAST_CHECK)
print corners.shape

if value_return:
    objectt_points.append(object_points)
    image_points_L.append(corners)

image = cv2.imread(path + '/' + file_r1)
image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
value_return, corners = cv2.findChessboardCorners(image_gray, (6, 9), flags=cv2.CALIB_CB_FAST_CHECK)
print corners.shape

if value_return:
    objectt_points.append(object_points)
    image_points_L.append(corners)

retval, cameraMatrix1, distCoeffs1, cameraMatrix2, distCoeffs2, R, T, E, F = (
cv2.stereoCalibrate(objectt_points,
                    image_points_L,
                    image_points_R,
                    mat_L,
                    dis_L,
                    mat_R,
                    dis_R,
                    image_gray.shape,
                    flags=cv2.CALIB_FIX_INTRINSIC))

这是与
相关的第一部分的解决方案 cameraCalibration()
没关系,我想我找到了。我为函数 calibrateCamera()

提供的参数有问题

EDIT 下的 21/feb/2021 现在出现了一个新问题。

EDIT 的解决方案,问题第 2 部分

代码有错误,并且存在轻微的概念问题。 image_points_R 从未被填充,所有内容都附加到 image_points_L。概念上的问题是不需要附加 4 倍的 3D 坐标,因为单个时间帧的 L 和 R 凸轮对应于相同的坐标。

注释第 60 和 69 行,更正第 61 和 70 行