特征点检测与匹配的常见算法程序集锦(harrisDetect,goodFeaturesDetect,fastDetect,surfDetect,siftDetect)

// Harriscornerdetect.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "harris.h"
#include <time.h>
#include "opencv2/nonfree/features2d.hpp"
#include "opencv2/legacy/legacy.hpp"

int typevalue=1;
int maxtype=4;
clock_t tStar,tEnd;
char *winname="cornerdetect";//显示窗口名称;
Mat image1,image2,image;
void typeCallback(int,void*);
void harrisDetect();
void goodFeaturesDetect();
void fastDetect();//快速检测算法;
void surfDetect();
void siftDetect();
int main(int argc, char *argv[])
{
    image1 = imread ("C:\\Users\\medal\\Desktop\\record\\test (11).BMP");
	image2 = imread ("C:\\Users\\medal\\Desktop\\record\\test (12).BMP",0);
	if (!image1.data||!image2.data)
	{
		printf("please input the right picture.");
		return -1;
	}
	namedWindow(winname);
	createTrackbar("CornerDetectType",winname,&typevalue,maxtype,typeCallback);
	typeCallback(0,0);
    waitKey (0);
    return 0;
}

void typeCallback(int,void*)
{
	//灰度变换
    cvtColor (image1,image,CV_BGR2GRAY);
	switch (typevalue)
	{
	case 0:
		harrisDetect();
		cout<<"harrisDetect"<<endl;
		break;
	case 1:
		goodFeaturesDetect();
		cout<<"goodFeaturesDetect"<<endl;
		break;
	case 2:
		fastDetect();
		cout<<"fastDetect"<<endl;
		break;
	case 3:
		surfDetect();
		cout<<"surfDetect"<<endl;
		break;
	case 4:
		siftDetect();
		cout<<"siftDetect"<<endl;
		break;
	}
}
void harrisDetect()
{
	// 经典的harris角点方法;
	harris Harris;
	// 计算角点;
	Harris.detect(image);
	//获得角点
	vector<Point> pts;
	Harris.getCorners(pts,0.01);
	// 标记角点;
	Harris.drawOnImage(image,pts);
	imshow (winname,image);
}

void goodFeaturesDetect()
{
	// 改进的harris角点检测方法;
	vector<Point> corners;
	goodFeaturesToTrack(image,corners,
		200,
		//角点最大数目;
		0.01,
		// 质量等级,这里是0.01*max(min(e1,e2)),e1,e2是harris矩阵的特征值
		10);
	// 两个角点之间的距离容忍度;
	harris().drawOnImage(image,corners);//标记角点;
		imshow (winname,image);
	
}
//快速检测算法;
void fastDetect()
{	
    //快速角点检测
    vector<KeyPoint> keypoints;
    FastFeatureDetector fast(40,true);
    fast.detect (image,keypoints);
    drawKeypoints (image,keypoints,image,Scalar::all(255),DrawMatchesFlags::DRAW_OVER_OUTIMG);
	imshow (winname,image);
}
void surfDetect()
{
	vector<KeyPoint> keypoints_1, keypoints_2;
	 Mat descriptors_1, descriptors_2;
	 //-- Step 1: Detect the keypoints using SURF Detector
	SurfFeatureDetector surf(2500);
	surf.detect(image,keypoints_1);
	surf.detect(image2,keypoints_2);
	//-- Step 2: Calculate descriptors (feature vectors)
	SurfDescriptorExtractor extractor;
	extractor.compute( image, keypoints_1, descriptors_1 );
	extractor.compute( image2, keypoints_2, descriptors_2 );
	//-- Step 3: Matching descriptor vectors with a brute force matcher
	BruteForceMatcher< L2<float> > matcher;
	std::vector< DMatch > matches;
	matcher.match( descriptors_1, descriptors_2, matches );
	nth_element(matches.begin(),matches.begin()+24,matches.end());
	matches.erase(matches.begin()+25,matches.end());
	//-- Draw matches
	Mat img_matches;
	drawMatches( image, keypoints_1, image2, keypoints_2, matches, img_matches,Scalar(255,255,255) );
	drawKeypoints(image,keypoints_1,image,Scalar(255,255,255),DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
	//-- Show detected matches
	imshow("Matches", img_matches );
	imshow (winname,image);
}
void siftDetect()
{
	tStar=clock();

	vector<KeyPoint> keypoints;
	SiftFeatureDetector sift(0.03,10);
	sift.detect(image,keypoints);
	drawKeypoints(image,keypoints,image,Scalar(255,255,255),DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
	imshow (winname,image);
	tEnd=clock();
	cout<<"开始时间:"<<tStar<<endl;
	cout<<"结束时间:"<<tEnd<<endl;
	cout<<"总计用时:"<<tEnd-tStar<<endl;

}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值