sfm triangulatePoints input array assertion failed

asked 2017-08-13 16:04:10 -0500

Kozuch gravatar image

updated 2017-08-13 17:40:25 -0500

I want to use cv::sfm::triangulatePoints from opencv_contrib in OpenCV 3.2.0. I get following error:

OpenCV Error: Assertion failed (points2d_tmp[i].rows == 2 && points2d_tmp[i].cols == n_points) in triangulatePoints, file /home/kozuch/install/opencv_contrib-3.2.0/modules/sfm/src/triangulation.cpp, line 140 terminate called after throwing an instance of 'cv::Exception' what(): /home/kozuch/install/opencv_contrib-3.2.0/modules/sfm/src/triangulation.cpp:140: error: (-215) points2d_tmp[i].rows == 2 && points2d_tmp[i].cols == n_points in function triangulatePoints

Here is my code:

vector<cv::Point2f> points1, points2;
featureDetection(img1, points1); // my custom function
featureTracking(img1,img2,points1,points2); // my custom function

vector<vector<cv::Point2f> > sfmPoints2d;
sfmPoints2d.push_back(points1);
sfmPoints2d.push_back(points2);

//~ cv::Mat matFeatures1(points1);
//~ cv::Mat matFeatures2(points2);
//~ matFeatures1 = matFeatures1.t(); // transpose - change rows for columns
//~ matFeatures2 = matFeatures2.t();
//~ vector<cv::Mat> sfmPoints2d;
//~ sfmPoints2d.push_back(matFeatures1);
//~ sfmPoints2d.push_back(matFeatures2);

vector<cv::Mat> sfmProjMats;
sfmProjMats.push_back(projMat0);
sfmProjMats.push_back(projMat1);
cv::Mat points3d;

cv::sfm::triangulatePoints(sfmPoints2d, sfmProjMats, points3d);

sfmPoints2d does the porblem. I tried to use Mats instead of inner vectors but the error remained the same. Even tried to transpose the Mats (change rows for cols). The docs here say:

points2d - Input vector of vectors of 2d points (the inner vector is per image). Has to be 2 X N.

projection_matrices - Input vector with 3x4 projections matrices of each image.

points3d - Output array with computed 3d points. Is 3 x N.

Can someone tell me what am I doing wrong?

FOUND AN ANSWER (can not post real answer so putting it here):

Found the solution finally (the matrices in the top should be ideally empty).

cv::Mat points1Mat = (cv::Mat_<double>(2,1) << 1, 1);
cv::Mat points2Mat = (cv::Mat_<double>(2,1) << 1, 1);

for (int i=0; i < points1.size(); i++) {
    cv::Point2f myPoint1 = points1.at(i);
    cv::Point2f myPoint2 = points2.at(i);
    cv::Mat matPoint1 = (cv::Mat_<double>(2,1) << myPoint1.x, myPoint1.y);
    cv::Mat matPoint2 = (cv::Mat_<double>(2,1) << myPoint2.x, myPoint2.y);
    cv::hconcat(points1Mat, matPoint1, points1Mat);
    cv::hconcat(points2Mat, matPoint2, points2Mat);
}

vector<cv::Mat> sfmPoints2d;
sfmPoints2d.push_back(points1Mat);
sfmPoints2d.push_back(points2Mat);

vector<cv::Mat> sfmProjMats;
sfmProjMats.push_back(projMat0);
sfmProjMats.push_back(projMat1);
cv::Mat points3d;

//~ cout << "matFeatures1 = " << endl << " " << matFeatures1 << endl << endl;

cv::sfm::triangulatePoints(sfmPoints2d, sfmProjMats, points3d);
edit retag flag offensive close merge delete