Stark
Stark

Reputation: 23

Convert Keypoints to cv::Mat in OpenCV with C++ for TriangulatePoints

I am new in the field of CV and trying to Triangulate Points between three Pictures but for the beginning, I want to Triangulate between two Pictures. For that, I made these Steps:

  1. Feature detection with AKAZE
  2. Feature matching by NORM_HAMMING
  3. Filter the Matches
  4. Match the filtered descriptors with the KeyPoint Coordinates

After that, I have a Vector of KeyPoints for each Image of the same size. Know i want to convert this Vector to "cv::Mat", so that i can use the TriangulatPoints function:

std::vector<cv::KeyPoint>::iterator it_1;
std::vector<cv::Point2f> points_1;

for(it_1 = keypoints_matched_1.begin() ; it_1!=keypoints_matched_1.end() ; it_1++)
{
    points_1.push_back(it_1->pt);
}
cv::Mat pointmatrix1(points_1);



std::vector<cv::KeyPoint>::iterator it_2;
std::vector<cv::Point2f> points_2;

for(it_2 = keypoints_matched_2.begin() ; it_2!=keypoints_matched_2.end() ; it_2++)
{
    points_2.push_back(it_2->pt);
}
cv::Mat pointmatrix2(points_2);

std::vector<cv::Mat> points3D;
std::vector<cv::Mat> points_all={pointmatrix1, pointmatrix2};
cv::sfm::triangulatePoints(points_all,Proj_matrices,points3D);

I keep running into this Error:

error: (-215:Assertion failed) points2d_tmp[i].rows == 2 && points2d_tmp[i].cols == n_points in function 'triangulatePoints'

Upvotes: 2

Views: 800

Answers (1)

m88
m88

Reputation: 1988

pointmatrix1 pointmatrix2 should be 2xN matrices containing floats.
Right now they're 1xN matrices containing cv::Point2f elements;

Try something like this instead:

    cv::Mat pointmatrix1(2, keypoints_matched_1.size(), CV_32F);
    int column = 0;
    for (auto& kp: keypoints_matched_1) {
        pointmatrix1.at<float>(0,column) = kp.pt.x;
        pointmatrix1.at<float>(1,column) = kp.pt.y;
        column++;
    }

Upvotes: 1

Related Questions