projectPoints example error

asked 2014-08-11 08:39:33 -0500

Mainframe gravatar image

updated 2014-08-11 09:51:45 -0500

I'm currently trying to implement a example of OpenCV's projectPoints method. The idea behind this method is taking as input a set of 3D points, translation/rotation vector's of a given camera and its distortion coeficients, output the corresponding 2D points in the image plane. The source of code is as follows:

#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/highgui/highgui.hpp"

#include <iostream>
#include <string>

std::vector<cv::Point3d> Generate3DPoints();

int main(int argc, char* argv[])
{
// Read 3D points
std::vector<cv::Point3d> objectPoints = Generate3DPoints();
std::vector<cv::Point2d> imagePoints;

cv::Mat intrisicMat(3, 3, cv::DataType<double>::type); // Intrisic matrix
intrisicMat.at<double>(0, 0) = 1.6415318549788924e+003;
intrisicMat.at<double>(1, 0) = 0;
intrisicMat.at<double>(2, 0) = 0;

intrisicMat.at<double>(0, 1) = 0;
intrisicMat.at<double>(1, 1) = 1.7067753507885654e+003;
intrisicMat.at<double>(2, 1) = 0;

intrisicMat.at<double>(0, 2) = 5.3262822453148601e+002;
intrisicMat.at<double>(1, 2) = 3.8095355839052968e+002;
intrisicMat.at<double>(2, 2) = 1;

cv::Mat rVec(3, 1, cv::DataType<double>::type); // Rotation vector
rVec.at<double>(0) = -3.9277902400761393e-002;
rVec.at<double>(1) = 3.7803824407602084e-002;
rVec.at<double>(2) = 2.6445674487856268e-002;


cv::Mat tVec(3, 1, cv::DataType<double>::type); // Translation vector
tVec.at<double>(0) = 2.1158489381208221e+000;
tVec.at<double>(1) = -7.6847683212704716e+000;
tVec.at<double>(2) = 2.6169795190294256e+001;


cv::Mat distCoeffs(5, 1, cv::DataType<double>::type);   // Distortion vector
distCoeffs.at<double>(0) = -7.9134632415085826e-001;
distCoeffs.at<double>(1) = 1.5623584435644169e+000;
distCoeffs.at<double>(2) = -3.3916502741726508e-002;
distCoeffs.at<double>(3) = -1.3921577146136694e-002;
distCoeffs.at<double>(4) = 1.1430734623697941e+002;


std::cout << "Intrisic matrix: " << intrisicMat << std::endl << std::endl;
std::cout << "Rotation vector: " << rVec << std::endl << std::endl;
std::cout << "Translation vector: " << tVec << std::endl << std::endl;
std::cout << "Distortion coef: " << distCoeffs << std::endl << std::endl;


std::vector<cv::Point2f> projectedPoints;


cv::projectPoints(objectPoints, rVec, tVec, intrisicMat, distCoeffs, projectedPoints);

for (unsigned int i = 0; i < projectedPoints.size(); ++i)
{
    std::cout << "Image point: " << imagePoints[i] << " Projected to " << projectedPoints[i] << std::endl;
}

std::cout << "Press any key to exit.";
std::cin.ignore();
std::cin.get();

return 0;
}



std::vector<cv::Point3d> Generate3DPoints()
{
std::vector<cv::Point3d> points;

double x, y, z;

x = .5; y = .5; z = -.5;
points.push_back(cv::Point3d(x, y, z));

x = .5; y = .5; z = .5;
points.push_back(cv::Point3d(x, y, z));

x = -.5; y = .5; z = .5;
points.push_back(cv::Point3d(x, y, z));

x = -.5; y = .5; z = -.5;
points.push_back(cv::Point3d(x, y, z));

x = .5; y = -.5; z = -.5;
points.push_back(cv::Point3d(x, y, z));

x = -.5; y = -.5; z = -.5;
points.push_back(cv::Point3d(x, y, z));

x = -.5; y = -.5; z = .5;
points.push_back(cv::Point3d(x, y, z));


for(unsigned int i = 0; i < points.size(); ++i)
{
std::cout << points[i] << std::endl << std::endl;
}

return points;

}

The application crashes when I try to run the projectPoints ... (more)

edit retag flag offensive close merge delete

Comments

Can you debug to see on which line it crashes? Where do you push_back to imagePoints? What is the output of the program before it crashes?

boaz001 gravatar imageboaz001 ( 2014-08-11 10:48:40 -0500 )edit

It crashes in the following line: cv::projectPoints(objectPoints, rVec, tVec, intrisicMat, distCoeffs, projectedPoints). I'm not doing any push_back to imagePoints, just ignore that For cycle (forgot to comment it) where that variable is used. The output of the program is every "cout" up to std::cout << "Distortion coef: " << distCoeffs << std::endl << std::endl (including).

Mainframe gravatar imageMainframe ( 2014-08-11 11:26:28 -0500 )edit

My last suggestion is that you use floats just like the function description describes the parameters.

boaz001 gravatar imageboaz001 ( 2014-08-12 04:14:24 -0500 )edit

You're right boaz001, that was the problem, thanks for the feedback!

Mainframe gravatar imageMainframe ( 2014-08-12 04:42:27 -0500 )edit