今天我遇到了与这个问题相同的问题。如果您不关心运行时,Appleman1234 提出的答案很好。如果您关心运行时,我相信for 循环总是会让您付出高昂的代价。cv::KeyPoint::convert()
所以我在 OpenCV 中偶然发现了这个有趣的函数std::vector<KeyPoint> keypoints_vector
(std::vector<cv::Point2f> point2f_vector
在您的情况下,它可以按如下方式使用:
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
有关更多详细信息,请参阅此处的文档。