Python openCV 2.4.3 cv2.SolvePnP错误

时间:2013-02-04 15:01:36

标签: python opencv numpy

我是Python,OpenCV和Numpy的新手。我一直在尝试实现一个python版本的C ++代码,它确定了摄像头与已知标记的相对姿势。代码使用了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:>错误:(-215)npoints> = 0&& npoints == std :: max(ipoints.checkVector(2,CV_32F),> ipoints.checkVector(2,CV_64F))

帮助实现此功能以及有关我出错的信息将不胜感激。还有很多东西需要学习!

2 个答案:

答案 0 :(得分:2)

您向solvePnP提供的参数形状不正确。 objectPoints应为Nx3,imagePoints应为Nx2。如果我在您的示例中切换tPointsmPoints如何分配给这些参数,则不会引发异常。

答案 1 :(得分:0)

如果尝试将数组切片为输入,这也会引起问题。取自{{3}}

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.akshay.turnonhotspot2">
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>


</manifest>