Ask Your Question
0

solvePNP() - Assertion failed

asked 2012-10-09 09:55:40 -0600

this post is marked as community wiki

This post is a wiki. Anyone with karma >50 is welcome to improve it.

Hello!

I am trying to get the pose of the camera with the help of solvePNP().

After running my program I get the following errors:

OpenCV Error: Assertion failed (0 <= i && i < (int)vv.size()) in getMat, 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/core/src/matrix.cpp, line 971
libc++abi.dylib: terminate called throwing an exception

I tried to search how to solve these errors, but I couldn't resolve it unfortunately!

Here is my code, all comment/help is much appreciated:

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;
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, boardCorners, CV_CALIB_CB_ADAPTIVE_THRESH);

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

solvePnP(boardPoints, boardCorners, intrinsics, distortion, rvec, tvec);

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

return 0;

}

edit retag flag offensive close merge delete

Comments

I faced with 'assertion failed' problem in another place of this function (OpenCV 2.4.3) According to the documentation objectPoints have to be 3xN/Nx3 1-channel or 1xN/Nx1 3-channel (the same for imagePoints). In my case it works only for default method flag=CV_ITERATIVE. In case of CV_P3P or CV_EPNP 3-channel arrays must be. Otherwise Assertion failed (CV_IS_MAT(_src) && CV_IS_MAT(_dst) && (_src->rows == 1 || _src->cols == 1) && (_dst->rows == 1 || _dst->cols == 1) && _src->cols + _src->rows - 1 == _dst->rows + _dst->cols - 1 && (CV_MAT_TYPE(_src->type) == CV_32FC2 || CV_MAT_TYPE(_src->type) == CV_64FC2) ... in cvUndistortPoints, file OpenCV-2.4.3/modules/imgproc/src/undistort.cpp, line 279

ag gravatar imageag ( 2013-01-22 01:35:31 -0600 )edit

1 answer

Sort by » oldest newest most voted
0

answered 2014-02-25 04:04:30 -0600

somedev gravatar image

updated 2014-02-25 04:06:53 -0600

the solvePnP() method takes the following as input :

vector<point3f> ObjectPoints, vector<point2f> imagePoints, Mat cameraMatrix, Mat distortionCoeffs, Mat rvec, Mat tvec

The parameters you seem to be passing are:

vector<vector<point3f> > boardPoints, vector<point2f> boardCorners, vector<mat> intrinsics, vector<mat> distortion, vector<mat> rvec, vector<mat> tvec

solvePnP() doesn't implicitly convert vector<vector<t> > to vector<t> like the calibrateCamera() method does, try to pass the parameters iteratively, it will work. Someone should include this in the documentation to avoid confusion.

edit flag offensive delete link more

Comments

That‘s right!Thank you!

Cpt gravatar imageCpt ( 2014-03-11 07:33:18 -0600 )edit

Question Tools

Stats

Asked: 2012-10-09 09:55:40 -0600

Seen: 6,617 times

Last updated: Feb 25 '14