OpenCV——ORB特征检测与匹配
Posted long5683
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了OpenCV——ORB特征检测与匹配相关的知识,希望对你有一定的参考价值。
原文链接:https://mp.weixin.qq.com/s/S4b1OGjRWX1kktefyHAo8A
1 #include <opencv2/opencv.hpp> 2 #include <opencv2/xfeatures2d.hpp> 3 #include <iostream> 4 5 using namespace cv; 6 using namespace cv::xfeatures2d; 7 using namespace std; 8 9 int main(int argc, char** argv) { 10 Mat src = imread("test.jpg", IMREAD_GRAYSCALE); 11 if (src.empty()) { 12 printf("could not load image... "); 13 return -1; 14 } 15 namedWindow("input image", CV_WINDOW_AUTOSIZE); 16 imshow("input image", src); 17 18 // ORB特征点检测 19 int minHessian = 100; 20 Ptr<ORB> detector = ORB::create(minHessian);//和surf的区别:只是SURF→ORB 21 vector<KeyPoint> keypoints; 22 detector->detect(src, keypoints, Mat());//找出关键点 23 24 // 绘制关键点 25 Mat keypoint_img; 26 drawKeypoints(src, keypoints, keypoint_img, Scalar::all(-1), DrawMatchesFlags::DEFAULT); 27 imshow("KeyPoints Image", keypoint_img); 28 29 waitKey(0); 30 return 0; 31 }
匹配
1 #include <opencv2/opencv.hpp> 2 #include <iostream> 3 #include <math.h> 4 #define RATIO 0.4 5 using namespace cv; 6 using namespace std; 7 int main(int argc, char** argv) { 8 Mat box = imread("2.png"); 9 Mat scene = imread("数字.jpg"); 10 if (scene.empty()) { 11 printf("could not load image... "); 12 return -1; 13 } 14 imshow("input image", scene); 15 vector<KeyPoint> keypoints_obj, keypoints_sence; 16 Mat descriptors_box, descriptors_sence; 17 Ptr<ORB> detector = ORB::create(); 18 detector->detectAndCompute(scene, Mat(), keypoints_sence, descriptors_sence); 19 detector->detectAndCompute(box, Mat(), keypoints_obj, descriptors_box); 20 vector<DMatch> matches; 21 // 初始化flann匹配 22 // Ptr<FlannBasedMatcher> matcher = FlannBasedMatcher::create(); // default is bad, using local sensitive hash(LSH) 23 Ptr<DescriptorMatcher> matcher = makePtr<FlannBasedMatcher>(makePtr<flann::LshIndexParams>(12, 20, 2)); 24 matcher->match(descriptors_box, descriptors_sence, matches); 25 // 发现匹配 26 vector<DMatch> goodMatches; 27 printf("total match points : %d ", matches.size()); 28 float maxdist = 0; 29 for (unsigned int i = 0; i < matches.size(); ++i) { 30 printf("dist : %.2f ", matches[i].distance); 31 maxdist = max(maxdist, matches[i].distance); 32 } 33 for (unsigned int i = 0; i < matches.size(); ++i) { 34 if (matches[i].distance < maxdist*RATIO) 35 goodMatches.push_back(matches[i]); 36 } 37 Mat dst; 38 drawMatches(box, keypoints_obj, scene, keypoints_sence, goodMatches, dst); 39 imshow("output", dst); 40 waitKey(0); 41 return 0; 42 }
1 Ptr<DescriptorMatcher> matcher = makePtr<FlannBasedMatcher> 2 (makePtr<flann::LshIndexParams>(12, 20, 2));
以上是关于OpenCV——ORB特征检测与匹配的主要内容,如果未能解决你的问题,请参考以下文章