原文链接:https://mp.weixin.qq.com/s/S4b1OGjRWX1kktefyHAo8A
#include <opencv2/opencv.hpp>
#include <opencv2/xfeatures2d.hpp>
#include <iostream> using namespace cv;
using namespace cv::xfeatures2d;
using namespace std; int main(int argc, char** argv) {
Mat src = imread("test.jpg", IMREAD_GRAYSCALE);
if (src.empty()) {
printf("could not load image...\n");
return -;
}
namedWindow("input image", CV_WINDOW_AUTOSIZE);
imshow("input image", src); // ORB特征点检测
int minHessian = ;
Ptr<ORB> detector = ORB::create(minHessian);//和surf的区别:只是SURF→ORB
vector<KeyPoint> keypoints;
detector->detect(src, keypoints, Mat());//找出关键点 // 绘制关键点
Mat keypoint_img;
drawKeypoints(src, keypoints, keypoint_img, Scalar::all(-), DrawMatchesFlags::DEFAULT);
imshow("KeyPoints Image", keypoint_img); waitKey();
return ;
}
匹配
#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>
#define RATIO 0.4
using namespace cv;
using namespace std;
int main(int argc, char** argv) {
Mat box = imread("2.png");
Mat scene = imread("数字.jpg");
if (scene.empty()) {
printf("could not load image...\n");
return -;
}
imshow("input image", scene);
vector<KeyPoint> keypoints_obj, keypoints_sence;
Mat descriptors_box, descriptors_sence;
Ptr<ORB> detector = ORB::create();
detector->detectAndCompute(scene, Mat(), keypoints_sence, descriptors_sence);
detector->detectAndCompute(box, Mat(), keypoints_obj, descriptors_box);
vector<DMatch> matches;
// 初始化flann匹配
// Ptr<FlannBasedMatcher> matcher = FlannBasedMatcher::create(); // default is bad, using local sensitive hash(LSH)
Ptr<DescriptorMatcher> matcher = makePtr<FlannBasedMatcher>(makePtr<flann::LshIndexParams>(, , ));
matcher->match(descriptors_box, descriptors_sence, matches);
// 发现匹配
vector<DMatch> goodMatches;
printf("total match points : %d\n", matches.size());
float maxdist = ;
for (unsigned int i = ; i < matches.size(); ++i) {
printf("dist : %.2f \n", matches[i].distance);
maxdist = max(maxdist, matches[i].distance);
}
for (unsigned int i = ; i < matches.size(); ++i) {
if (matches[i].distance < maxdist*RATIO)
goodMatches.push_back(matches[i]);
}
Mat dst;
drawMatches(box, keypoints_obj, scene, keypoints_sence, goodMatches, dst);
imshow("output", dst);
waitKey();
return ;
}
Ptr<DescriptorMatcher> matcher = makePtr<FlannBasedMatcher>
(makePtr<flann::LshIndexParams>(, , ));