Orb匹配算法代码

来源:互联网 发布:算法交易软件 编辑:程序博客网 时间:2024/04/30 02:44


[cpp] view plaincopy
  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 

[cpp] view plaincopy
  1. /* 
  2. SIFT sift; 
  3. sift(img_1, Mat(), keyPoints_1, descriptors_1); 
  4. sift(img_2, Mat(), keyPoints_2, descriptors_2); 
  5. BruteForceMatcher<L2<float> >  matcher; 
  6. */  
  7.   
  8. /* 
  9. SURF surf; 
  10. surf(img_1, Mat(), keyPoints_1); 
  11. surf(img_2, Mat(), keyPoints_2); 
  12. SurfDescriptorExtractor extrator; 
  13. extrator.compute(img_1, keyPoints_1, descriptors_1); 
  14. extrator.compute(img_2, keyPoints_2, descriptors_2); 
  15. BruteForceMatcher<L2<float> >  matcher; 
  16. */  

效果:


另外一个是寻找目标匹配

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


效果如下:




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

[cpp] view plaincopy
  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 0
原创粉丝点击