我在(opencv开源)中提取了SIFT功能,并将它们提取为关键点。现在,我想将它们转换为Matrix(带有x,y坐标)或将它们保存在文本文件中......
在这里,您可以看到用于提取关键点的示例代码,现在我想知道如何将它们转换为MAT或将它们保存在txt,xml或yaml中......
cv::SiftFeatureDetector detector;
std::vector<cv::KeyPoint> keypoints;
detector.detect(input, keypoints);
答案 0 :(得分:7)
转换为cv :: Mat如下。
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);
写入filestorage是
cv::FileStorage fs("test.yml", cv::FileStorage::WRITE);
cv::FileStorage fs2("test2.xml", cv::FileStorage::WRITE);
detector.write(fs);
detector.write(fs2);
答案 1 :(得分:1)
今天,我遇到了与该问题相同的问题。如果您不关心运行时,Appleman1234提出的答案很好。我相信 for循环如果您关心运行时,总是会付出高昂的代价。因此,我偶然发现了OpenCV中的这个有趣的函数(cv::KeyPoint::convert()
),它使您可以将KeyPoints(std::vector<KeyPoint> keypoints_vector
)的向量直接转换为Point2f(std::vector<cv::Point2f> point2f_vector
)的向量。 / p>
根据您的情况,它可以按以下方式使用:
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
有关更多详细信息,请参阅此文档here。