zoukankan      html  css  js  c++  java
  • Opencv Sift算子特征提取与匹配

    SIFT算法的过程实质是在不同尺度空间上查找特征点(关键点),用128维方向向量的方式对特征点进行描述,最后通过对比描述向量实现目标匹配。


    概括起来主要有三大步骤:

    1、提取关键点;

    2、对关键点附加详细的信息(局部特征)也就是所谓的描述器;

    3、通过两方特征点(附带上特征向量的关键点)的两两比较找出相互匹配的若干对特征点,建立物体间的对应关系。

     

    Opencv中Sift算子的特征提取是在SiftFeatureDetector类中的detect方法实现的。

    特征点描述是在SiftDescriptorExtractor类中的compute方法实现的。

    特征点匹配是在BruteForceMatcher类中的match方法实现的。


    这其中还用到两个比较有意思的方法:drawKeypoints和drawMatches。以下demo演示Sift的特征提取与匹配的步骤,和这两个方法的用法:


    #include "highgui/highgui.hpp"
    #include "opencv2/nonfree/nonfree.hpp"
    #include "opencv2/legacy/legacy.hpp"
    
    using namespace cv;
    
    int main(int argc,char *argv[])
    {
    	Mat image01=imread(argv[1]);
    	Mat image02=imread(argv[2]);
    	Mat image1,image2;
    	GaussianBlur(image01,image1,Size(3,3),0.5);
    	GaussianBlur(image02,image2,Size(3,3),0.5);
    
    	//提取特征点
    	SiftFeatureDetector siftDetector(30);  //限定提起前15个特征点
    	vector<KeyPoint> keyPoint1,keyPoint2;
    	siftDetector.detect(image1,keyPoint1);
    	siftDetector.detect(image2,keyPoint2);
    
    	//绘制特征点
    	drawKeypoints(image1,keyPoint1,image1,Scalar::all(-1),DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
    	drawKeypoints(image2,keyPoint2,image2,Scalar::all(-1),DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
    	namedWindow("KeyPoints of image1",0);
    	namedWindow("KeyPoints of image2",0);
    
    	imshow("KeyPoints of image1",image1);
    	imshow("KeyPoints of image2",image2);
    
    	//特征点描述,为下边的特征点匹配做准备
    	SiftDescriptorExtractor siftDescriptor;
    	Mat imageDesc1,imageDesc2;
    	siftDescriptor.compute(image1,keyPoint1,imageDesc1);
    	siftDescriptor.compute(image2,keyPoint2,imageDesc2);
    
    	//特征点匹配并显示匹配结果
    	BruteForceMatcher<L2<float>> matcher;
    	vector<DMatch> matchePoints;
    	matcher.match(imageDesc1,imageDesc2,matchePoints,Mat());
    	Mat imageOutput;
    	drawMatches(image01,keyPoint1,image02,keyPoint2,matchePoints,imageOutput);
    	namedWindow("Mathch Points",0);
    	imshow("Mathch Points",imageOutput);
    	waitKey();
    	return 0;
    }



    图1中提取到的特征点:



    图2中提取到的特征点:



    图1和图2中分别有30个特征点,点数的多少可以人为设定。

    drawKeypoints方法中第4个参数若设置为Scalar::all(-1),会绘制随机颜色;

    本例中drawKeypoints最后一个参数使用的是DrawMatchesFlags::DRAW_RICH_KEYPOINTS,则会绘制特征点的位置、大小、方向信息。若最后一个参数设置为DrawMatchesFlags::DEFAULT;则只会绘制特征的位置信息,表现出来只是一个点。


    匹配结果:



    Opencv中使用Sift算子需要加头文件"opencv2/nonfree/nonfree.hpp",注意这个是非免费的,Sift算法的专利权属于哥伦比亚大学,如果在商业软件中使用,可能有风险。


  • 相关阅读:
    mysqldump 命令的使用
    linux find 命令查找文件和文件夹
    linux定时任务
    find: `./folder': No such file or directory 错误处理
    利用mysqldump 与 nginx定时器 定时备份mysql库
    vue项目在nginx中不能刷新问题
    CodeReview规范
    VUE npm run build的项目出现跨域请求的问题npm run dev没有这个问题
    composer.json和composer.lock到底是什么以及区别?
    K近邻算法小结
  • 原文地址:https://www.cnblogs.com/mtcnn/p/9411959.html
Copyright © 2011-2022 走看看