1

我试图在 OpenCV 的 solvePNP() 的帮助下获得相机的姿势。

运行我的程序后,我收到以下错误:

OpenCV Error: Assertion failed (npoints >= 0 && npoints == std::max(ipoints.checkVector(2, CV_32F), ipoints.checkVector(2, CV_64F))) in solvePnP, file /opt/local/var/macports/build/_opt_local_var_macports_sources_rsync.macports.org_release_tarballs_ports_graphics_opencv/opencv/work/OpenCV-2.4.2/modules/calib3d/src/solvepnp.cpp, line 55
libc++abi.dylib: terminate called throwing an exception

我试图搜索如何解决这些错误,但不幸的是我无法解决!

这是我的代码,非常感谢所有评论/帮助:

enum Pattern { NOT_EXISTING, CHESSBOARD, CIRCLES_GRID, ASYMMETRIC_CIRCLES_GRID };

void calcBoardCornerPositions(Size boardSize, float squareSize, vector<Point3f>& corners,
                          Pattern patternType)
{
    corners.clear();

    switch(patternType)
    {
    case CHESSBOARD:
    case CIRCLES_GRID:
        for( int i = 0; i < boardSize.height; ++i )
            for( int j = 0; j < boardSize.width; ++j )
                corners.push_back(Point3f(float( j*squareSize ), float( i*squareSize ), 0));
        break;

    case ASYMMETRIC_CIRCLES_GRID:
        for( int i = 0; i < boardSize.height; i++ )
            for( int j = 0; j < boardSize.width; j++ )
                corners.push_back(Point3f(float((2*j + i % 2)*squareSize), float(i*squareSize), 0));
        break;
    }
}

int main(int argc, char* argv[])
{
    float squareSize = 50.f;

    Pattern calibrationPattern = CHESSBOARD;

    //vector<Point2f> boardCorners;
    vector<vector<Point2f> > imagePoints(1);
    vector<vector<Point3f> > boardPoints(1);

    Size boardSize;
    boardSize.width = 9;
    boardSize.height = 6;

    vector<Mat> intrinsics, distortion;
    string filename = "out_camera_xml.xml";
    FileStorage fs(filename, FileStorage::READ);
    fs["camera_matrix"] >> intrinsics;
    fs["distortion_coefficients"] >> distortion;
    fs.release();

    vector<Mat> rvec, tvec;
    Mat img = imread(argv[1], CV_LOAD_IMAGE_GRAYSCALE); // at kell adnom egy kepet

    bool found = findChessboardCorners(img, boardSize, imagePoints[0], CV_CALIB_CB_ADAPTIVE_THRESH);

    calcBoardCornerPositions(boardSize, squareSize, boardPoints[0], calibrationPattern);
    boardPoints.resize(imagePoints.size(),boardPoints[0]);

    //***Debug start***
    cout << imagePoints.size() << endl << boardPoints.size() << endl << intrinsics.size() << endl << distortion.size() << endl;
    //***Debug end***

    solvePnP(Mat(boardPoints), Mat(imagePoints), intrinsics, distortion, rvec, tvec);

    for(int i=0; i<rvec.size(); i++) {
            cout << rvec[i] << endl;
    }

    return 0;
}

编辑(一些调试信息):

我逐行调试它。我踏入了所有的功能。我在 SolvePNP(...) 中得到断言失败。当我进入solvePNP函数时,您可以在下面看到我看到的内容。首先它跳过第一个 if 语句 / if(vec.empty()) /,然后进入第二个 if 语句 / if( !copyData ) /,当它执行最后一行 /*datalimit = dataend = datastart + rows *step[0]*/ 跳回到第一个 if 语句并返回 =>,然后我得到 Assertion failed 错误。

template<typename _Tp> inline Mat::Mat(const vector<_Tp>& vec, bool copyData)
: flags(MAGIC_VAL | DataType<_Tp>::type | CV_MAT_CONT_FLAG),
dims(2), rows((int)vec.size()), cols(1), data(0), refcount(0),
datastart(0), dataend(0), allocator(0), size(&rows)
{
    if(vec.empty())
        return;
    if( !copyData )
    {
        step[0] = step[1] = sizeof(_Tp);
        data = datastart = (uchar*)&vec[0];
        datalimit = dataend = datastart + rows*step[0];
    }
    else
        Mat((int)vec.size(), 1, DataType<_Tp>::type, (uchar*)&vec[0]).copyTo(*this);
}
4

3 回答 3

1

在调试器中进入函数,并准确查看哪个断言失败。(可能它需要双精度值(CV_64F)而不是浮点数。)

OpenCV 的新“inputarray”包装器允许您调用具有任何形状的垫子、点向量等的函数 - 它会对其进行排序。但是很多函数都采用特定的 inut 格式,或者具有强制执行特定格式的过时断言。

立体声/校准系统最不适合需要特定布局,并且频繁的连续操作需要不同的布局。

于 2012-10-10T14:41:23.770 回答
0

类型似乎不正确,至少在对我有用的代码中我使用了不同的类型(如文档中所述)。

objectPoints – 对象坐标空间中的对象点数组,3xN/Nx3 1 通道或 1xN/Nx1 3 通道,其中 N 是点数。向量也可以在这里传递。

imagePoints – 对应图像点的数组,2xN/Nx2 1 通道或 1xN/Nx1 2 通道,其中 N 是点数。向量也可以在这里传递。

cameraMatrix – 输入相机矩阵 A = \vecthreethree{fx}{0}{cx}{0}{fy}{cy}{0}{0}{1}。

distCoeffs – 4、5 或 8 个元素的失真系数(k_1、k_2、p_1、p_2[、k_3[、k_4、k_5、k_6]])的输入向量。如果向量为 NULL/空,则假定零失真系数。

rvec – 输出旋转向量(参见 Rodrigues() ),它与 tvec 一起将点从模型坐标系带到相机坐标系。

tvec - 输出翻译向量。

useExtrinsicGuess – 如果为真 (1),则函数使用提供的 rvec 和 tvec 值分别作为旋转和平移向量的初始近似值,并进一步优化它们。

来自这里的文档。

vector<Mat> rvec, tvec应该Mat rvec, tvec改为。

vector<vector<Point2f> > imagePoints(1)应该vector<Point2f> imagePoints(1)改为。

vector<vector<Point3f> > boardPoints(1)应该 vector<Point3f> boardPoints(1)改为。

注意:我遇到了完全相同的问题,这对我有用(这有点令人困惑,因为calibrateCamera使用向量)。虽然还没有为 imagePoints 或 boardPoints 尝试过。(但正如上面链接中所记录的那样,vectorvector应该可以工作,我想我最好提一下),但是对于rvectrec我自己尝试过。

于 2015-07-14T09:41:45.940 回答
0

I run in exactly the same problem with solvePnP and opencv3. I tried to isolate the problem in a single test case. I seams passing a std::vector to cv::InputArray does not what is expected. The following small test works with opencv 2.4.9 but not with 3.2.

And this is exactly the problem when passing a std::vector of points to solvePnP and causes the assert at line 63 in solvepnp.cpp to fail !

Generating a cv::mat out of the vector list before passing to solvePnP works.

//create list with 3 points
std::vector<cv::Point3f> vectorList;
vectorList.push_back(cv::Point3f(1.0, 1.0, 1.0));
vectorList.push_back(cv::Point3f(1.0, 1.0, 1.0));
vectorList.push_back(cv::Point3f(1.0, 1.0, 1.0));

//to input array
cv::InputArray inputArray(vectorList);
cv::Mat mat = inputArray.getMat();
cv::Mat matDirect = cv::Mat(vectorList);

LOG_INFO("Size vector: %d mat: %d matDirect: %d", vectorList.size(), mat.checkVector(3, CV_32F), matDirect.checkVector(3, CV_32F));

QVERIFY(vectorList.size() == mat.checkVector(3, CV_32F));

Result opencv 2.4.9 macos:

TestObject: OpenCV
Size vector: 3 mat: 3 matDirect: 3

Result opencv 3.2 win64:

TestObject: OpenCV
Size vector: 3 mat: 9740 matDirect: 3
于 2017-01-20T10:56:06.950 回答