OpenCV中feature2D学习——FAST特征点检测与SIFT/SURF/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;
}

运行结果如下:





  • 2
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值