convert keypoints to mat or save them to text file opencv
Asked Answered
R

2

5

I have extracted SIFT features in (opencv open source) and they are extracted as keypoints. Now, I would like to convert them to Matrix (With their x,y coordinates) or save them in a text file...

Here, you can see a sample code for extracting the keypoints and now I would like to know how convert them to MAT or save them in txt, xml or yaml...

cv::SiftFeatureDetector detector;
std::vector<cv::KeyPoint> keypoints;
detector.detect(input, keypoints);
Ritz answered 4/10, 2011 at 4:32 Comment(0)
S
7

Convert to cv::Mat is as follows.

std::vector<cv::KeyPoint> keypoints;
std::vector<cv::Point2f> points;
std::vector<cv::KeyPoint>::iterator it;

for( it= keypoints.begin(); it!= keypoints.end();it++)
{
    points.push_back(it->pt);
}

cv::Mat pointmatrix(points);

Write to filestorage is

cv::FileStorage fs("test.yml", cv::FileStorage::WRITE);
cv::FileStorage fs2("test2.xml", cv::FileStorage::WRITE);

detector.write(fs);
detector.write(fs2);
Splay answered 18/2, 2012 at 23:58 Comment(0)
S
2

Today I came across the same problem as per this question. The answer proposed by Appleman1234 is nice if you don't care about runtime. I believe for loops will always cost you dearly if you care about runtime. So I stumbled upon and found this interesting function (cv::KeyPoint::convert()) in OpenCV, which allows you to directly convert a vector of KeyPoints (std::vector<KeyPoint> keypoints_vector) into a vector of Point2f (std::vector<cv::Point2f> point2f_vector).

In your case, it can be used as follows:

std::vector<cv::KeyPoint> keypoints_vector; //We define vector of keypoints
std::vector<cv::Point2f> point2f_vector; //We define vector of point2f
cv::KeyPoint::convert(keypoints_vector, point2f_vector, std::vector< int >()); //Then we use this nice function from OpenCV to directly convert from KeyPoint vector to Point2f vector
cv::Mat img1_coordinates(point2f_vector); //We simply cast the Point2f vector into a cv::Mat as Appleman1234 did

For more details, refer this documentation here.

Symbolist answered 26/7, 2018 at 19:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.