失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > OpenCV中feature2D学习SIFT和SURF算子实现特征点提取与匹配

OpenCV中feature2D学习SIFT和SURF算子实现特征点提取与匹配

时间:2018-12-19 14:57:55

相关推荐

OpenCV中feature2D学习SIFT和SURF算子实现特征点提取与匹配

数据库|mysql教程

OpenCV,feature2D,学习,SIFT,SURF,

数据库-mysql教程

dnf改药源码,vscode 语法高亮,Ubuntu基本操作指令,ejb和tomcat,安卓编译sqlite,jquery里jrange插件,app小程序前端框架,骷髅爬虫素描视频,php 调用c,seo作弊手法,自带数据库的网站模板下载地址,动态网页模板百度云,dedecms会员模板调用,dede单页面模板下载,星外虚拟主机管理系统 破解版,托盘程序实例lzw

概述 之前的文章SURF和SIFT算子实现特征点检测简单地讲了利用SIFT和SURF算子检测特征点,在检测的基础上可以使用SIFT和SURF算子对特征点进行特征提取并使用匹配函数进行特征点的匹配。具体实现是首先采用SurfFeatureDetector检测特征点,再使用SurfDescripto

网页源码提取工具,ubuntu删除中文,单机tomcat做负载均衡,https 反爬虫,php 自带web服务器,东莞市价格低的seo公司lzw

特效源码,ubuntu获取动态ip,tomcat内存溢出工具,爬虫收集代理,php构造方法在线学习,seo观念lzw

概述

之前的文章SURF和SIFT算子实现特征点检测简单地讲了利用SIFT和SURF算子检测特征点,在检测的基础上可以使用SIFT和SURF算子对特征点进行特征提取并使用匹配函数进行特征点的匹配。具体实现是首先采用SurfFeatureDetector检测特征点,再使用SurfDescriptorExtractor计算特征点的特征向量,最后采用BruteForceMatcher暴力匹配法或者FlannBasedMatcher选择性匹配法(二者的不同)来进行特征点匹配。

实验所用环境是opencv2.4.0+vs+win7,需要注意opencv2.4.X版本中SurfFeatureDetector是包含在opencv2/nonfree/features2d.hpp中,BruteForceMatcher是包含在opencv2/legacy/legacy.hpp中,FlannBasedMatcher是包含在opencv2/features2d/features2d.hpp中。

BruteForce匹配法

首先使用BruteForceMatcher暴力匹配法,代码如下:

/*** @采用SURF算子检测特征点,对特征点进行特征提取,并使用BruteForce匹配法进行特征点的匹配* @SurfFeatureDetector + SurfDescriptorExtractor + BruteForceMatcher* @author holybin*/#include #include #include "opencv2/core/core.hpp"#include "opencv2/nonfree/features2d.hpp"//SurfFeatureDetector实际在该头文件中#include "opencv2/legacy/legacy.hpp"//BruteForceMatcher实际在该头文件中//#include "opencv2/features2d/features2d.hpp"//FlannBasedMatcher实际在该头文件中#include "opencv2/highgui/highgui.hpp"using namespace cv;using namespace std;int main( int argc, char** argv ){Mat src_1 = imread( "D:\\opencv_pic\\cat3d120.jpg", CV_LOAD_IMAGE_GRAYSCALE );Mat src_2 = imread( "D:\\opencv_pic\\cat0.jpg", CV_LOAD_IMAGE_GRAYSCALE );if( !src_1.data || !src_2.data ){ cout<< " --(!) Error reading images "<<endl;return -1; }//-- Step 1: 使用SURF算子检测特征点int minHessian = 400;SurfFeatureDetector detector( minHessian );vector keypoints_1, keypoints_2;detector.detect( src_1, keypoints_1 );detector.detect( src_2, keypoints_2 );cout<<"img1--number of keypoints: "<<keypoints_1.size()<<endl;cout<<"img2--number of keypoints: "<<keypoints_2.size()<<endl;//-- Step 2: 使用SURF算子提取特征(计算特征向量)SurfDescriptorExtractor extractor;Mat descriptors_1, descriptors_2;pute( src_1, keypoints_1, descriptors_1 );pute( src_2, keypoints_2, descriptors_2 );//-- Step 3: 使用BruteForce法进行暴力匹配BruteForceMatcher< L2 > matcher;vector matches;matcher.match( descriptors_1, descriptors_2, matches );cout<<"number of matches: "<<matches.size()<<endl;//-- 显示匹配结果Mat matchImg;drawMatches( src_1, keypoints_1, src_2, keypoints_2, matches, matchImg ); imshow("matching result", matchImg );waitKey(0);return 0;}

实验结果:

FLANN匹配法

使用暴力匹配的结果不怎么好,下面使用FlannBasedMatcher进行特征匹配,只保留好的特征匹配点,代码如下:

/*** @采用SURF算子检测特征点,对特征点进行特征提取,并使用FLANN匹配法进行特征点的匹配* @SurfFeatureDetector + SurfDescriptorExtractor + FlannBasedMatcher* @author holybin*/#include #include #include "opencv2/core/core.hpp"#include "opencv2/nonfree/features2d.hpp"//SurfFeatureDetector实际在该头文件中//#include "opencv2/legacy/legacy.hpp"//BruteForceMatcher实际在该头文件中#include "opencv2/features2d/features2d.hpp"//FlannBasedMatcher实际在该头文件中#include "opencv2/highgui/highgui.hpp"using namespace cv;using namespace std;int main( int argc, char** argv ){Mat src_1 = imread( "D:\\opencv_pic\\cat3d120.jpg", CV_LOAD_IMAGE_GRAYSCALE );Mat src_2 = imread( "D:\\opencv_pic\\cat0.jpg", CV_LOAD_IMAGE_GRAYSCALE );if( !src_1.data || !src_2.data ){ cout<< " --(!) Error reading images "<<endl;return -1; }//-- Step 1: 使用SURF算子检测特征点int minHessian = 400;SurfFeatureDetector detector( minHessian );vector keypoints_1, keypoints_2;detector.detect( src_1, keypoints_1 );detector.detect( src_2, keypoints_2 );cout<<"img1--number of keypoints: "<<keypoints_1.size()<<endl;cout<<"img2--number of keypoints: "<<keypoints_2.size()<<endl;//-- Step 2: 使用SURF算子提取特征(计算特征向量)SurfDescriptorExtractor extractor;Mat descriptors_1, descriptors_2;pute( src_1, keypoints_1, descriptors_1 );pute( src_2, keypoints_2, descriptors_2 );//-- Step 3: 使用FLANN法进行匹配FlannBasedMatcher matcher;vector allMatches;matcher.match( descriptors_1, descriptors_2, allMatches );cout<<"number of matches before filtering: "< maxDist )maxDist = dist;}printf("max dist : %f \n", maxDist );printf("min dist : %f \n", minDist );//-- 过滤匹配点,保留好的匹配点(这里采用的标准:distance<2*minDist)vector goodMatches;for( int i = 0; i < descriptors_1.rows; i++ ){if( allMatches[i].distance < 2*minDist )goodMatches.push_back( allMatches[i]); }cout<<"number of matches after filtering: "<<goodMatches.size()<<endl;//-- 显示匹配结果Mat matchImg;drawMatches( src_1, keypoints_1, src_2, keypoints_2, goodMatches, matchImg, Scalar::all(-1), Scalar::all(-1), vector(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS //不显示未匹配的点); imshow("matching result", matchImg );//-- 输出匹配点的对应关系for( int i = 0; i < goodMatches.size(); i++ )printf( "good match %d: keypoints_1 [%d] -- keypoints_2 [%d]\n", i, goodMatches[i].queryIdx, goodMatches[i].trainIdx );waitKey(0);return 0;}

实验结果:

从第二个实验结果可以看出,经过过滤之后特征点数目从49减少到33,匹配的准确度有所上升。当然也可以使用SIFT算子进行上述两种匹配实验,只需要将SurfFeatureDetector换成SiftFeatureDetector,将SurfDescriptorExtractor换成SiftDescriptorExtractor即可。

拓展

在FLANN匹配法的基础上,还可以进一步利用透视变换和空间映射找出已知物体(目标检测),具体来说就是利用findHomography函数利用匹配的关键点找出相应的变换,再利用perspectiveTransform函数映射点群。具体可以参考这篇文章:OpenCV中feature2D学习——SIFT和SURF算法实现目标检测。

如果觉得《OpenCV中feature2D学习SIFT和SURF算子实现特征点提取与匹配》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。