OpenCV2.4 例程解析一 图像匹配

来源:互联网 发布:大数据会烂大街吗 编辑:程序博客网 时间:2024/05/21 06:14

以下程序来自OpenCV自带的例程:

[cpp] view plaincopy
  1. #include "stdafx.h"  
  2. #include <stdio.h>  
  3. #include "opencv2/core/core.hpp"  
  4. #include "opencv2/features2d/features2d.hpp"  
  5. #include "opencv2/highgui/highgui.hpp"  
  6. #include "opencv2/nonfree/nonfree.hpp"  
  7.   
  8. using namespace cv;  
  9.   
  10. void help()  
  11. {  
  12.     printf("\nThis program demonstrates using features2d detector, descriptor extractor and simple matcher\n"  
  13.         "Using the SURF desriptor:\n"  
  14.         "\n"  
  15.         "Usage:\n matcher_simple <image1> <image2>\n");  
  16. }  
  17.   
  18. int main(int argc, char** argv)  
  19. {  
  20.     //if(argc != 3)  
  21.     //{  
  22.     //  help();  
  23.     //  return -1;  
  24.     //}  
  25.   
  26.     Mat img1 = imread("H:\\OpenCV2.4\\opencv\\samples\\c\\box.png");//(argv[1], CV_LOAD_IMAGE_GRAYSCALE);  
  27.     Mat img2 = imread("H:\\OpenCV2.4\\opencv\\samples\\c\\box_in_scene.png");//(argv[2], CV_LOAD_IMAGE_GRAYSCALE);  
  28.     if(img1.empty() || img2.empty())  
  29.     {  
  30.         printf("Can't read one of the images\n");  
  31.         return -1;  
  32.     }  
  33.   
  34.     // detecting keypoints  
  35.     //检测关键点  
  36.     SurfFeatureDetector detector(400);  
  37.     vector<KeyPoint> keypoints1, keypoints2;  
  38.     detector.detect(img1, keypoints1);  
  39.     detector.detect(img2, keypoints2);  
  40.   
  41.     // computing descriptors  
  42.     // 计算描述器  
  43.     SurfDescriptorExtractor extractor;  
  44.     Mat descriptors1, descriptors2;  
  45.     extractor.compute(img1, keypoints1, descriptors1);  
  46.     extractor.compute(img2, keypoints2, descriptors2);  
  47.   
  48.     // matching descriptors  
  49.     //匹配描述器  
  50.     BFMatcher matcher(NORM_L2);  
  51.     vector<DMatch> matches;  
  52.     matcher.match(descriptors1, descriptors2, matches);  
  53.   
  54.     // drawing the results  
  55.     // 画结果  
  56.     namedWindow("matches", 1);  
  57.     Mat img_matches;  
  58.     drawMatches(img1, keypoints1, img2, keypoints2, matches, img_matches);  
  59.     imshow("matches", img_matches);  
  60.     waitKey(0);  
  61.   
  62.     return 0;  
  63. }  

此例程比较简单不做深入解释。
转载自 http://blog.csdn.net/hjh2005/article/details/7610794


原创粉丝点击