2

我是 Python、OpenCV 和 Numpy 的新手。我一直在尝试实现 C++ 代码的 python 版本,该代码确定相机与已知标记的相对姿势。该代码使用 cv2.SolvePnP 函数,我一直在机智地运行该函数。我在网上搜索过,但除了这个功能类似的混乱之外,什么也没找到。似乎无论我以什么形式传递我的数据,功能都不愉快。我一直在使用的测试用例是:

## target image points

tPoints = np.zeros((4,2),dtype=np.float64)
tPoints[0,0] = 384.3331
tPoints[0,1] = 162.23618
tPoints[1,0] = 385.27521
tPoints[1,1] = 135.21503
tPoints[2,0] = 409.36746
tPoints[2,1] = 165.64435

## actual marker point set

mPoints = np.zeros((4,3),dtype=np.float64)
mPoints[0,0] = -88.0
mPoints[0,1] = 88.0
mPoints[0,2] = 0
mPoints[1,0] = -88.0
mPoints[1,1] = -88.0
mPoints[1,2] = 0
mPoints[2,0] = 88.0
mPoints[2,1] = -88.0
mPoints[2,2] = 0
mPoints[3,0] = 88.0
mPoints[3,1] = 88.0
mPoints[3,2] = 0

camMatrix = np.zeros((3,3),dtype=np.float64 )
camMatrix[0][0] = 519.0
camMatrix[0][2] = 320.0
camMatrix[1][1] = 522.0
camMatrix[1][2] = 240.0
camMatrix[2][2] = 1.0

retval, rvec, tvec = cv2.solvePnP(objectPoints = tPoints, imagePoints = mPoints, cameraMatrix = camMatrix, distCoeffs = None)

返回的错误是:

cv2.error: C:\slave\WinInstallerMegaPack\src\opencv\modules\calib3d\src\solvepnp.cpp:52: >error: (-215) npoints >= 0 && npoints == std::max(ipoints.checkVector (2, CV_32F), >ipoints.checkVector(2, CV_64F))

帮助执行此操作,以及有关我哪里出错的信息将不胜感激。还有很多东西要学!

4

2 回答 2

3

您给出的参数的形状solvePnP不正确。 objectPoints应该是 Nx3,并且imagePoints应该是 Nx2。如果我在您的示例中切换了这些参数的分配方式tPointsmPoints分配方式,则不会引发异常。

于 2013-02-04T16:22:27.170 回答
3

如果您尝试将数组切片作为输入,这也会导致问题。取自 这里

World = array([[-0.5, -0.5,  3. ],
               [ 0.5, -0.5,  3. ],
               [ 0.5,  0.5,  3. ],
               [-0.5,  0. ,  3. ]])
keyPoints = array([[ 279.03286469,  139.80463604,    1.        ],
                     [ 465.40665724,  136.70519839,    1.        ],
                     [ 465.40665724,  325.1505936 ,    1.        ],
                     [ 279.03286469,  230.927896  ,    1.        ]])

objectPoints = World
imagePoints = keyPoints[:,:2] # <--- THIS SLICE IS A PROBLEM CAUSER!!!
cv2.solvePnP(objectPoints, imagePoints, np.eye(3), np.zeros(5))
于 2018-10-30T14:12:23.950 回答