OpenCV学习笔记——2维特征Feature2D,SURF

基于特征点的图像匹配是图像处理中经常会遇到的问题,手动选取特征点太麻烦了。比较经典常用的特征点自动提取的办法有Harris特征、SIFT特征、SURF特征。

先介绍利用SURF特征的特征描述办法,其操作封装在类SurfFeatureDetector中,利用类内的detect函数可以检测出SURF特征的关键点,保存在vector容器中。第二部利用SurfDescriptorExtractor类进行特征向量的相关计算。将之前的vector变量变成向量矩阵形式保存在Mat中。最后强行匹配两幅图像的特征向量,利用了类BruteForceMatcher中的函数match。代码如下:

[cpp]  view plain copy
  1. /** 
  2.  * @file SURF_descriptor 
  3.  * @brief SURF detector + descritpor + BruteForce Matcher + drawing matches with OpenCV functions 
  4.  * @author A. Huaman 
  5.  */  
  6.   
  7. #include <stdio.h>  
  8. #include <iostream>  
  9. #include "opencv2/core/core.hpp"  
  10. #include "opencv2/features2d/features2d.hpp"  
  11. #include "opencv2/highgui/highgui.hpp"  
  12.   
  13. using namespace cv;  
  14.   
  15. void readme();  
  16.   
  17. /** 
  18.  * @function main 
  19.  * @brief Main function 
  20.  */  
  21. int main( int argc, char** argv )  
  22. {  
  23.   if( argc != 3 )  
  24.   { return -1; }  
  25.   
  26.   Mat img_1 = imread( argv[1], CV_LOAD_IMAGE_GRAYSCALE );  
  27.   Mat img_2 = imread( argv[2], CV_LOAD_IMAGE_GRAYSCALE );  
  28.     
  29.   if( !img_1.data || !img_2.data )  
  30.   { return -1; }  
  31.   
  32.   //-- Step 1: Detect the keypoints using SURF Detector  
  33.   int minHessian = 400;  
  34.   
  35.   SurfFeatureDetector detector( minHessian );  
  36.   
  37.   std::vector<KeyPoint> keypoints_1, keypoints_2;  
  38.   
  39.   detector.detect( img_1, keypoints_1 );  
  40.   detector.detect( img_2, keypoints_2 );  
  41.   
  42.   //-- Step 2: Calculate descriptors (feature vectors)  
  43.   SurfDescriptorExtractor extractor;  
  44.   
  45.   Mat descriptors_1, descriptors_2;  
  46.   
  47.   extractor.compute( img_1, keypoints_1, descriptors_1 );  
  48.   extractor.compute( img_2, keypoints_2, descriptors_2 );  
  49.   
  50.   //-- Step 3: Matching descriptor vectors with a brute force matcher  
  51.   BruteForceMatcher< L2<float> > matcher;  
  52.   std::vector< DMatch > matches;  
  53.   matcher.match( descriptors_1, descriptors_2, matches );  
  54.   
  55.   //-- Draw matches  
  56.   Mat img_matches;  
  57.   drawMatches( img_1, keypoints_1, img_2, keypoints_2, matches, img_matches );   
  58.   
  59.   //-- Show detected matches  
  60.   imshow("Matches", img_matches );  
  61.   
  62.   waitKey(0);  
  63.   
  64.   return 0;  
  65. }  
  66.   
  67. /** 
  68.  * @function readme 
  69.  */  
  70. void readme()  
  71. { std::cout << " Usage: ./SURF_descriptor <img1> <img2>" << std::endl; }  

当然,进行强匹配的效果不够理想,这里再介绍一种FLANN特征匹配算法。前两步与上述代码相同,第三步利用FlannBasedMatcher类进行特征匹配,并只保留好的特征匹配点,代码如下:

[cpp]  view plain copy
  1. //-- Step 3: Matching descriptor vectors using FLANN matcher  
  2. FlannBasedMatcher matcher;  
  3. std::vector< DMatch > matches;  
  4. matcher.match( descriptors_1, descriptors_2, matches );  
  5.   
  6. double max_dist = 0; double min_dist = 100;  
  7.   
  8. //-- Quick calculation of max and min distances between keypoints  
  9. forint i = 0; i < descriptors_1.rows; i++ )  
  10. double dist = matches[i].distance;  
  11.   if( dist < min_dist ) min_dist = dist;  
  12.   if( dist > max_dist ) max_dist = dist;  
  13. }  
  14.   
  15. printf("-- Max dist : %f \n", max_dist );  
  16. printf("-- Min dist : %f \n", min_dist );  
  17.   
  18. //-- Draw only "good" matches (i.e. whose distance is less than 2*min_dist )  
  19. //-- PS.- radiusMatch can also be used here.  
  20. std::vector< DMatch > good_matches;  
  21.   
  22. forint i = 0; i < descriptors_1.rows; i++ )  
  23. if( matches[i].distance < 2*min_dist )  
  24.   { good_matches.push_back( matches[i]); }  
  25. }    
  26.   
  27. //-- Draw only "good" matches  
  28. Mat img_matches;  
  29. drawMatches( img_1, keypoints_1, img_2, keypoints_2,   
  30.              good_matches, img_matches, Scalar::all(-1), Scalar::all(-1),   
  31.              vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );   
  32.   
  33. //-- Show detected matches  
  34. imshow( "Good Matches", img_matches );  

在FLANN特征匹配的基础上,还可以进一步利用Homography映射找出已知物体。具体来说就是利用findHomography函数利用匹配的关键点找出相应的变换,再利用perspectiveTransform函数映射点群。具体代码如下:

[cpp]  view plain copy
  1. //-- Localize the object from img_1 in img_2   
  2. std::vector<Point2f> obj;  
  3. std::vector<Point2f> scene;  
  4.   
  5. forint 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.   
  12. Mat H = findHomography( obj, scene, CV_RANSAC );  
  13.   
  14. //-- Get the corners from the image_1 ( the object to be "detected" )  
  15. Point2f obj_corners[4] = { cvPoint(0,0), cvPoint( img_1.cols, 0 ), cvPoint( img_1.cols, img_1.rows ), cvPoint( 0, img_1.rows ) };  
  16. Point scene_corners[4];  
  17.   
  18. //-- Map these corners in the scene ( image_2)  
  19. forint i = 0; i < 4; i++ )  
  20. {  
  21.   double x = obj_corners[i].x;   
  22.   double y = obj_corners[i].y;  
  23.   
  24.   double Z = 1./( H.at<double>(2,0)*x + H.at<double>(2,1)*y + H.at<double>(2,2) );  
  25.   double X = ( H.at<double>(0,0)*x + H.at<double>(0,1)*y + H.at<double>(0,2) )*Z;  
  26.   double Y = ( H.at<double>(1,0)*x + H.at<double>(1,1)*y + H.at<double>(1,2) )*Z;  
  27.   scene_corners[i] = cvPoint( cvRound(X) + img_1.cols, cvRound(Y) );  
  28. }    
  29.    
  30. //-- Draw lines between the corners (the mapped object in the scene - image_2 )  
  31. line( img_matches, scene_corners[0], scene_corners[1], Scalar(0, 255, 0), 2 );  
  32. line( img_matches, scene_corners[1], scene_corners[2], Scalar( 0, 255, 0), 2 );  
  33. line( img_matches, scene_corners[2], scene_corners[3], Scalar( 0, 255, 0), 2 );  
  34. line( img_matches, scene_corners[3], scene_corners[0], Scalar( 0, 255, 0), 2 );  
  35.   
  36. //-- Show detected matches  
  37. imshow( "Good Matches & Object detection", img_matches );  

然后再看一下Harris特征检测,在计算机视觉中,通常需要找出两帧图像的匹配点,如果能找到两幅图像如何相关,就能提取出两幅图像的信息。我们说的特征的最大特点就是它具有唯一可识别这一特点,图像特征的类型通常指边界、角点(兴趣点)、斑点(兴趣区域)。角点就是图像的一个局部特征,应用广泛。harris角点检测是一种直接基于灰度图像的角点提取算法,稳定性高,尤其对L型角点检测精度高,但由于采用了高斯滤波,运算速度相对较慢,角点信息有丢失和位置偏移的现象,而且角点提取有聚簇现象。具体实现就是使用函数cornerHarris实现。

除了利用Harris进行角点检测,还可以利用Shi-Tomasi方法进行角点检测。使用函数goodFeaturesToTrack对角点进行检测,效果也不错。也可以自己制作角点检测的函数,需要用到cornerMinEigenVal函数和minMaxLoc函数,最后的特征点选取,判断条件要根据自己的情况编辑。如果对特征点,角点的精度要求更高,可以用cornerSubPix函数将角点定位到子像素。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值