Sebastian
Sebastian

Reputation: 51

OpenCV error with projectPoints

I am trying to quantify the accuracy of my camera calibration using OpenCV. In my program I am reading an image of a chessboard pattern and calling the calibrateCamera function to get an initial guess of my camera instrinsics and extrinsics. I am aware that using only one image does not yield a perfect calibration and that the calibrateCamera returns the reprojection error. Nevertheless, I want to use the projectPoints function, to get the image points of my detected corners on the calibration board for further processing. I am using the code below for the calibration but as it tries to run the projectPoints function, the program crashes at runtime. If I remove the function call the code works just fine.

Mat image_;
Mat gray_image_;
Size chessboard_size_;
vector<Point2f> corners_;
vector< vector< Point2f> > imagePoints_;
vector< Point2f> imagePointsProjected_;
vector< vector< Point3f> > objectPoints_;
bool corners_found;
float measure_ = 35;

chessboard_size_ = Size(CHESSBOARD_INTERSECTIONS_HORIZONTAL, CHESSBOARD_INTERSECTIONS_VERTICAL);

// image of type CV_8UC3 is read, with 8 bit & 3 channels
image_ = imread("/home/fes1rng/left.png");

if(!image_.data )
{
  printf( "No image data \n" );
  return;
}

// image is converted to grayscale, afterwards it is of type CV_8UC1
cvtColor(image_, gray_image_, CV_RGB2GRAY);

// detect corners and draw them
corners_found = findChessboardCorners(gray_image_, Size(CHESSBOARD_INTERSECTIONS_HORIZONTAL, CHESSBOARD_INTERSECTIONS_VERTICAL), corners_);
if (corners_found)
{
  cornerSubPix(gray_image_, corners_, Size(11, 11), Size(-1, -1), TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 30, 0.1));

  drawChessboardCorners(image_, Size(CHESSBOARD_INTERSECTIONS_HORIZONTAL, CHESSBOARD_INTERSECTIONS_VERTICAL), corners_, corners_found);
}

vector< Point2f> v_tImgPT;
vector< Point3f> v_tObjPT;
//save 2d coordinate and world coordinate
for(int j=0; j< corners_.size(); ++j)
{
 Point2d tImgPT;
 Point3d tObjPT;

 tImgPT.x = corners_[j].x;
 tImgPT.y = corners_[j].y;

 tObjPT.x = j%CHESSBOARD_INTERSECTIONS_HORIZONTAL*measure_;
 tObjPT.y = j/CHESSBOARD_INTERSECTIONS_HORIZONTAL*measure_;
 tObjPT.z = 0;

 v_tImgPT.push_back(tImgPT);
 v_tObjPT.push_back(tObjPT);
}
imagePoints_.push_back(v_tImgPT);
objectPoints_.push_back(v_tObjPT);

Mat rvec(3,1, CV_64FC1);
Mat tvec(3,1, CV_64FC1);
vector<Mat> rvecs;
vector<Mat> tvecs;
rvecs.push_back(rvec);
tvecs.push_back(tvec);
Mat intrinsic_Matrix(3,3, CV_64FC1);
Mat distortion_coeffs(8,1, CV_64FC1);

calibrateCamera(objectPoints_, imagePoints_, image_.size(), intrinsic_Matrix, distortion_coeffs, rvecs, tvecs);

projectPoints(objectPoints_, rvecs, tvecs, intrinsic_Matrix, distortion_coeffs, imagePointsProjected_);

cv::namedWindow( "Display Image", CV_WINDOW_AUTOSIZE );
cv::imshow( "Display Image", image_ );

waitKey(0);

The error message is:

OpenCV Error: Assertion failed (0 <= i && i < (int)vv.size()) in getMat, file /build/buildd/opencv-2.4.8+dfsg1/modules/core/src/matrix.cpp, line 977
terminate called after throwing an instance of 'cv::Exception'
what():  /build/buildd/opencv-2.4.8+dfsg1/modules/core/src/matrix.cpp:977: error: (-215) 0 <= i && i < (int)vv.size() in function getMat

As the error occurs at runtime and in a subfunction call, I assume that it is caused by wrong datatypes of the matrices. But as the function projectPoints is internally used in calibrateCamera, I am confused why a single function call with the same parameters is causing the error.

Upvotes: 3

Views: 3526

Answers (2)

Sebastian
Sebastian

Reputation: 51

Using the following expression solved the issue!

projectPoints(objectPoints_.front(), rvecs.front(), tvecs.front(), intrinsic_Matrix, distortion_coeffs, imagePointsProjected_);

Upvotes: 2

Kornel
Kornel

Reputation: 5354

As the first parameter, projectPoints waits an std::vector<cv::Point3f> and not a std::vector<std::vector<cv::Point3f>>.

Upvotes: 4

Related Questions