函数 cv2.calibrateCamera PYTHON 中类型 Point3f 的问题

Problem with type Point3f in function cv2.calibrateCamera PYTHON

大家早上好!

我在使用 opencv 函数 calibrateCamera 时遇到了以下错误:

OpenCV(4.5.4) D:\a\opencv-python\opencv-python\opencv\modules\calib3d\src\calibration.cpp:3350: error: (-210:Unsupported format or combination of formats) objectPoints should contain vector of vectors of points of type Point3f in function 'cv::collectCalibrationData'

我试图找到解决方案,但除了类型问题或长度问题外,找不到任何其他解决方案。 我对 objPoints 和 imgPoints 使用 float 32 类型的 np.array:


    objpoints = np.array(objpoints, dtype = np.float32)
    imgpoints = np.array(imgpoints, dtype = np.float32)
    ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, imsize, None, None )

变量资源管理器为使用的变量提供以下值:


    In: objpoints
    Out: 
    array([[  0. , 246.5,   0. ],
           [ 14.5, 246.5,   0. ],
           [ 43.5, 246.5,   0. ],
           ...,
           [174. ,   0. ,   0. ],
           [319. ,   0. ,   0. ],
           [333.5,   0. ,   0. ]], dtype=float32)
    
    In: imgpoints
    Out: 
    array([[1310., 1032.],
           [1258., 1032.],
           [1154., 1032.],
           [1206., 1032.],
           ...,
           [ 739.,  134.],
           [ 686.,  133.],
           [ 162.,  132.],
           [ 110.,  132.]], dtype=float32)
    
    In: imsize
    Out: (1080, 1440)
    
    In: len(imgpoints)
    Out: 440
    In: len(objpoints)
    Out: 440

我还尝试在 calibrateCamera 函数中切换 objpoints 和 imgpoints,但这导致了相同的错误消息。当我在 C++ 中阅读了很多关于这个函数的解决方案时,我想指出这是一个 Python 相关的问题。

我希望有人能帮我解决这个问题!提前致谢!

已解决: 在从它们创建一个 numpy 数组之前,我必须将 objpoints 和 imgpoints 放在一个列表中:

objpoints = np.array([objpoints], dtype = np.float32)
imgpoints = np.array([imgpoints], dtype = np.float32)

而不是:

objpoints = np.array(objpoints, dtype = np.float32)
imgpoints = np.array(imgpoints, dtype = np.float32)

ImagePoints 和 objectPoints 应该包含 点向量的向量。这意味着你应该有多个图像和对象点 images.In 你的情况我看到只有一个图像有图像和对象点。您可以做的是在这种情况下使用 np.newaxis,如下所示。但是为了让标定效果更好,尽量多张图片。

objpoints = np.array(objpoints, dtype = np.float32)[np.newaxis]
imgpoints = np.array(imgpoints, dtype = np.float32)[np.newaxis]
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, imsize, None, None )

此函数可以获取多个视图(假设 N),每个 可能 显示一个 不同的 对象。

“点向量的向量”意味着每个“点向量”允许有不同的长度,因为该对象可能与其他视图中看到的对象不同.

如果所有视图都显示相同的对象(数组具有相同 长度),但在一般情况下,您 应该 传递一个 数组列表 ,这样每个数组都可以有自己合适的大小。

objApoints = np.array(objApoints, dtype=np.float32)
img1points = np.array(img1points, dtype=np.float32)

# have another picture? img2points...
# it may show the same object (objApoints) or a different object (objBpoints)

objpointslist = [objApoints]
imgpointslist = [img1points]

cv2.calibrateCamera(objpointslist, imgpointslist, ...)