最新版的OpenCV中新增加的ORB特征的使用

来源:互联网 发布:腾讯代理绝地求生优化 编辑:程序博客网 时间:2024/06/06 09:35

看到OpenCV2.3.1里面ORB特征提取算法也在里面了,套用给的SURF特征例子程序改为ORB特征一直提示错误,类型不匹配神马的,由于没有找到示例程序,只能自己找答案。

(ORB特征论文:ORB: an efficient alternative to SIFT or SURF.点击下载论文)

经过查找发现:

描述符数据类型有是float的,比如说SIFT,SURF描述符,还有是uchar的,比如说有ORB,BRIEF

对于float 匹配方式有:

FlannBased

BruteForce<L2<float> >

BruteForce<SL2<float> >

BruteForce<L1<float> >

对于uchar有:

BruteForce<Hammin>

BruteForce<HammingLUT>

[cpp] view plain copy
  1. BruteForceMatcher< L2<float> > matcher;//改动的地方  


完整代码如下:

[cpp] view plain copy
  1. #include <iostream>  
  2. #include "opencv2/core/core.hpp"  
  3. #include "opencv2/features2d/features2d.hpp"  
  4. #include "opencv2/highgui/highgui.hpp"  
  5. #include <iostream>  
  6. #include <vector>  
  7. using namespace cv;  
  8. using namespace std;  
  9. int main()  
  10. {  
  11.     Mat img_1 = imread("D:\\image\\img1.jpg");  
  12.     Mat img_2 = imread("D:\\image\\img2.jpg");  
  13.     if (!img_1.data || !img_2.data)  
  14.     {  
  15.         cout << "error reading images " << endl;  
  16.         return -1;  
  17.     }  
  18.   
  19.     ORB orb;  
  20.     vector<KeyPoint> keyPoints_1, keyPoints_2;  
  21.     Mat descriptors_1, descriptors_2;  
  22.   
  23.     orb(img_1, Mat(), keyPoints_1, descriptors_1);  
  24.     orb(img_2, Mat(), keyPoints_2, descriptors_2);  
  25.       
  26.     BruteForceMatcher<HammingLUT> matcher;  
  27.     vector<DMatch> matches;  
  28.     matcher.match(descriptors_1, descriptors_2, matches);  
  29.   
  30.     double max_dist = 0; double min_dist = 100;  
  31.     //-- Quick calculation of max and min distances between keypoints  
  32.     forint i = 0; i < descriptors_1.rows; i++ )  
  33.     {   
  34.         double dist = matches[i].distance;  
  35.         if( dist < min_dist ) min_dist = dist;  
  36.         if( dist > max_dist ) max_dist = dist;  
  37.     }  
  38.     printf("-- Max dist : %f \n", max_dist );  
  39.     printf("-- Min dist : %f \n", min_dist );  
  40.     //-- Draw only "good" matches (i.e. whose distance is less than 0.6*max_dist )  
  41.     //-- PS.- radiusMatch can also be used here.  
  42.     std::vector< DMatch > good_matches;  
  43.     forint i = 0; i < descriptors_1.rows; i++ )  
  44.     {   
  45.         if( matches[i].distance < 0.6*max_dist )  
  46.         {   
  47.             good_matches.push_back( matches[i]);   
  48.         }  
  49.     }  
  50.   
  51.     Mat img_matches;  
  52.     drawMatches(img_1, keyPoints_1, img_2, keyPoints_2,  
  53.         good_matches, img_matches, Scalar::all(-1), Scalar::all(-1),  
  54.         vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);  
  55.     imshow( "Match", img_matches);  
  56.     cvWaitKey();  
  57.     return 0;  
  58. }  


另外: SURF SIFT 


/*
SIFT sift;
sift(img_1, Mat(), keyPoints_1, descriptors_1);
sift(img_2, Mat(), keyPoints_2, descriptors_2);
BruteForceMatcher<L2<float> >  matcher;
*/

/*
SURF surf;
surf(img_1, Mat(), keyPoints_1);
surf(img_2, Mat(), keyPoints_2);
SurfDescriptorExtractor extrator;
extrator.compute(img_1, keyPoints_1, descriptors_1);
extrator.compute(img_2, keyPoints_2, descriptors_2);
BruteForceMatcher<L2<float> >  matcher;
*/


效果:


另外一个是寻找目标匹配

在右边的场景图里面寻找左边那幅图的starbucks标志


效果如下:




需要在之前的那个imshow之前加上如下代码即可完成一个简单的功能展示:


[cpp] view plain copy
  1. // localize the object  
  2. std::vector<Point2f> obj;  
  3. std::vector<Point2f> scene;  
  4.   
  5. for (size_t i = 0; i < good_matches.size(); ++i)  
  6. {  
  7.     // get the keypoints from the good matches  
  8.     obj.push_back(keyPoints_1[ good_matches[i].queryIdx ].pt);  
  9.     scene.push_back(keyPoints_2[ good_matches[i].trainIdx ].pt);  
  10. }  
  11. Mat H = findHomography( obj, scene, CV_RANSAC );  
  12.   
  13. // get the corners from the image_1  
  14. std::vector<Point2f> obj_corners(4);  
  15. obj_corners[0] = cvPoint(0,0);  
  16. obj_corners[1] = cvPoint( img_1.cols, 0);  
  17. obj_corners[2] = cvPoint( img_1.cols, img_1.rows);  
  18. obj_corners[3] = cvPoint( 0, img_1.rows);  
  19. std::vector<Point2f> scene_corners(4);  
  20.   
  21. perspectiveTransform( obj_corners, scene_corners, H);  
  22.   
  23. // draw lines between the corners (the mapped object in the scene - image_2)  
  24. line( img_matches, scene_corners[0] + Point2f( img_1.cols, 0), scene_corners[1] + Point2f( img_1.cols, 0),Scalar(0,255,0));  
  25. line( img_matches, scene_corners[1] + Point2f( img_1.cols, 0), scene_corners[2] + Point2f( img_1.cols, 0),Scalar(0,255,0));  
  26. line( img_matches, scene_corners[2] + Point2f( img_1.cols, 0), scene_corners[3] + Point2f( img_1.cols, 0),Scalar(0,255,0));  
  27. line( img_matches, scene_corners[3] + Point2f( img_1.cols, 0), scene_corners[0] + Point2f( img_1.cols, 0),Scalar(0,255,0));  
0 0
原创粉丝点击