1 | initial version |
It should work:
Code (needs OpenCV 3 or later):
cv::Ptr<cv::Feature2D> surf = cv::xfeatures2d::SURF::create();
cv::Mat ref = cv::imread("15094723456792949.jpg");
cv::Mat img = cv::imread("15094723542694177.jpg");
// cv::Mat ref = cv::imread("1509472382233744.jpg");
// cv::Mat img = cv::imread("15094725804738763.jpg");
std::vector<cv::KeyPoint> keypoints_ref, keypoints_img;
cv::Mat descriptors_ref, descriptors_img;
surf->detectAndCompute(ref, cv::noArray(), keypoints_ref, descriptors_ref);
surf->detectAndCompute(img, cv::noArray(), keypoints_img, descriptors_img);
cv::Ptr<cv::DescriptorMatcher> matcher = cv::DescriptorMatcher::create("BruteForce");
std::vector<std::vector<cv::DMatch> > matches;
matcher->knnMatch(descriptors_ref, descriptors_img, matches, 2);
std::vector<cv::DMatch> matches_filtered;
for(size_t i = 0; i < matches.size(); i++) {
if(matches[i].size() >= 2) {
float ratio = matches[i][0].distance / matches[i][1].distance;
if (ratio < 0.7) {
matches_filtered.push_back(cv::DMatch(matches[i][0].queryIdx, matches[i][0].trainIdx, matches[i][0].distance));
}
}
}
cv::Mat img_matches;
cv::drawMatches(ref, keypoints_ref, img, keypoints_img, matches_filtered, img_matches);
cv::imshow("Matches", img_matches);
cv::imwrite("matches.png", img_matches);
cv::waitKey();