// 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;
}