码迷,mamicode.com
首页 > 其他好文 > 详细

OpenCV中feature2D学习——FAST特征点检测与SIFT/SURF/BRIEF特征提取与匹配

时间:2015-03-31 14:45:41      阅读:802      评论:0      收藏:0      [点我收藏+]

标签:opencv   fast   brief   

       在前面的文章《OpenCV中feature2D学习——FAST特征点检测》中讲了利用FAST算子进行特征点检测,这里尝试使用FAST算子来进行特征点检测,并结合SIFT/SURF/BRIEF算子进行特征点提取和匹配。

I、结合SIFT算子进行特征点提取和匹配

由于数据类型的不同,SIFT和SURF算子只能采用FlannBasedMatcher或者BruteForceMatcher来进行匹配(参考OpenCV中feature2D学习——BFMatcher和FlannBasedMatcher)。

/**
* @概述:采用FAST算子检测特征点,采用SIFT算子对特征点进行特征提取,并使用BruteForce匹配法进行特征点的匹配
* @类和函数:FastFeatureDetector + SiftDescriptorExtractor + BruteForceMatcher
* @author:holybin
*/

#include <stdio.h>
#include <iostream>
#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("cat3d120.jpg");
	Mat src_2 = imread("cat0.jpg");
	if( !src_1.data || !src_2.data )
	{ 
		cout<< " --(!) Error reading images "<<endl;
		return -1; 
	}

	//-- Step 1: 使用FAST算子检测特征点
	FastFeatureDetector fast(20);	
	vector<KeyPoint> keypoints_1, keypoints_2;
	fast.detect( src_1, keypoints_1 );	//FAST(src_1, keypoints_1, 20); 
	fast.detect( src_2, keypoints_2 );	//FAST(src_2, keypoints_2, 20); 
	cout<<"img1--number of keypoints: "<<keypoints_1.size()<<endl;
	cout<<"img2--number of keypoints: "<<keypoints_2.size()<<endl;

	//-- Step 2: 使用SIFT算子提取特征(计算特征向量)
	SiftDescriptorExtractor extractor;	//SurfDescriptorExtractor extractor;
	Mat descriptors_1, descriptors_2;
	extractor.compute( src_1, keypoints_1, descriptors_1 );
	extractor.compute( src_2, keypoints_2, descriptors_2 );

	//-- Step 3: 使用BruteForce法进行暴力匹配
	BruteForceMatcher< L2<float> > matcher;	//FlannBasedMatcher matcher;
	vector< DMatch > 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,
		Scalar::all(-1), Scalar::all(-1), vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS); 
	imshow("matching result", matchImg );
	imwrite("match_result.png", matchImg);
	waitKey(0);

	return 0;
}

运行结果如下:

技术分享

技术分享


II、结合BRIEF算子进行特征点提取和匹配

BRIEF算子只能采用BruteForceMatcher来进行匹配(参考OpenCV中feature2D学习——BFMatcher和FlannBasedMatcher)。

/**
* @概述:采用FAST算子检测特征点,采用BRIEF算子对特征点进行特征提取,并使用BruteForce匹配法进行特征点的匹配
* @类和函数:FastFeatureDetector + BriefDescriptorExtractor + BruteForceMatcher
* @author:holybin
*/

#include <stdio.h>
#include <iostream>
#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("cat3d120.jpg");
	Mat src_2 = imread("cat0.jpg");
	if( !src_1.data || !src_2.data )
	{ 
		cout<< " --(!) Error reading images "<<endl;
		return -1; 
	}

	//-- Step 1: 使用FAST算子检测特征点
	FastFeatureDetector fast(20);	
	vector<KeyPoint> keypoints_1, keypoints_2;
	fast.detect( src_1, keypoints_1 );	//FAST(src_1, keypoints_1, 20); 
	fast.detect( src_2, keypoints_2 );	//FAST(src_2, keypoints_2, 20); 
	cout<<"img1--number of keypoints: "<<keypoints_1.size()<<endl;
	cout<<"img2--number of keypoints: "<<keypoints_2.size()<<endl;

	//-- Step 2: 使用BRIEF算子提取特征(计算特征向量)
	BriefDescriptorExtractor extractor;
	Mat descriptors_1, descriptors_2;
	extractor.compute( src_1, keypoints_1, descriptors_1 );
	extractor.compute( src_2, keypoints_2, descriptors_2 );

	//-- Step 3: 使用BruteForce法进行暴力匹配
	BruteForceMatcher< L2<float> > matcher;	//FlannBasedMatcher matcher;
	vector< DMatch > 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,
		Scalar::all(-1), Scalar::all(-1), vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS); 
	imshow("matching result", matchImg );
	imwrite("match_result.png", matchImg);
	waitKey(0);

	return 0;
}

运行结果如下:

技术分享

技术分享



OpenCV中feature2D学习——FAST特征点检测与SIFT/SURF/BRIEF特征提取与匹配

标签:opencv   fast   brief   

原文地址:http://blog.csdn.net/holybin/article/details/44778747

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!