Ask Your Question

Revision history [back]

projectPoints example error

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 method and I have no idea why. Any help would be greatly appreciated.

projectPoints example error

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 method and I have no idea why. Any help would be greatly appreciated.

EDIT: The error I get is the following: OpenCV Error: Assertion failed (mtype == type0 || (CV_MAT_CN(mtype) == CV_MAT_CN (type0) && ((1 << type0) & fixedDepthMask) != 0)) in cv::_OutputArray::create, f ile C:\builds\2_4_PackSlave-win64-vc12-shared\opencv\modules\core\src\matrix.cpp , line 1486