cvGoodFeaturesToTrack()与goodFeaturesToTrack()进行harris角点检测

1、角点检测函数和参数说明

cvGoodFeaturesToTrack()函数主要是处理IplImage数据格式的图像,而goodFeaturesToTrack()函数主要是处理Mat数据格式的图像。参数quality_level :特征值最大值最小值乘法因子;参数minDistance:角点之间最小距离;均对图像中harris角点检测的个数有影响。

2、具体代码如下

(1)harris角点检测

#include "opencv2/core/core.hpp"
#include "opencv2/flann/miniflann.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/video/video.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/ml/ml.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/contrib/contrib.hpp"

#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;

int main()
{
	//FILE* fp=fopen("E:\\实验室项目\\ImagePro\\GoodFeatures\\result.txt","w+");
	//char* filename="E:\\实验室项目\\ImagePro\\GoodFeatures\\j10.jpg";
	IplImage* img=cvLoadImage("F22.jpg",CV_LOAD_IMAGE_COLOR);
	if (!img)
	{
		cout<<"读取图像失败!"<<endl;
	}
	IplImage* img_copy=cvCloneImage(img);
	IplImage* img_gray=cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,1);
	IplImage* eig_img=cvCreateImage(cvGetSize(img),IPL_DEPTH_32F,1);
	IplImage* temp_img=cvCloneImage(eig_img);

	cvCvtColor(img,img_gray,CV_BGR2GRAY);
	const int MAX_CORNERS=1000;//定义角点个数最大值
	CvPoint2D32f* corners=new CvPoint2D32f[MAX_CORNERS];//分配保存角点的空间
	int corner_count=MAX_CORNERS;
	double quality_level=0.01;//or 0.01
	double min_distance=10;
	cvGoodFeaturesToTrack(img_gray,eig_img,temp_img,corners,&corner_count,quality_level,min_distance);

	//画角点
	for(int i=0;i<corner_count;i++)
	{
		cvCircle(img_copy,cvPoint((int)corners[i].x,(int)corners[i].y),1,CV_RGB(255,0,0),2,8);
		//fprintf(fp,"\t%f,%f\n",corners[i].x,corners[i].y);
	}
	cout<<"检测到角点个数为:"<<corner_count;
	cvNamedWindow("角点检测",CV_WINDOW_AUTOSIZE);
	cvShowImage("角点检测",img_copy);
	cvWaitKey(0);
	cvReleaseImage(&img);
	cvReleaseImage(&img_copy);
	cvReleaseImage(&img_gray);
	cvReleaseImage(&eig_img);
	cvReleaseImage(&temp_img);
	cvDestroyWindow("角点检测");
	return 0;
}


(2)shi-Tomas角点检测

#include "opencv2/core/core.hpp"
#include "opencv2/flann/miniflann.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/video/video.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/ml/ml.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/contrib/contrib.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
//#include <vector>

using namespace std;
using namespace cv;

Mat src,src_gray;

int maxCorners=1000;
int maxTrackbar=100;

RNG rng(12345);

char* source_window="Image";

void goodFeaturesToTrack_demo(int,void*);

int main()
{
	src=imread("F22.jpg",1);

	cvtColor(src,src_gray,CV_BGR2GRAY);
	namedWindow(source_window,CV_WINDOW_AUTOSIZE);
	createTrackbar("角点个数:",source_window,&maxCorners,maxTrackbar,goodFeaturesToTrack_demo);
	imshow(source_window,src);
	goodFeaturesToTrack_demo(0,0);

	waitKey(0);
	return 0;
}

void goodFeaturesToTrack_demo(int,void*)
{
	if (maxCorners<1)
	{
		maxCorners=1;
	}

	//Shi-Tomasi 角点算法参数定义
	vector<Point2f> corners;
	double qualityLevel=0.01;//最大最小特征值乘法因子
	double minDistance=10;//角点之间最小距离
	int blockSize=3;
	bool useHarrisDetector=false;
	double k=0.04;


	Mat copy;
	copy=src.clone();

	goodFeaturesToTrack(src_gray,corners,maxCorners,qualityLevel,minDistance,Mat(),blockSize,useHarrisDetector,k);
	
	cout<<"检测到角点数:"<<corners.size()<<endl;
	int r=1;
	for (int i=0;i<corners.size();i++)
	{
		circle(copy,corners[i],r,Scalar(rng.uniform(0,255),rng.uniform(0,255),rng.uniform(0,255)),2,8,0);
	}
	namedWindow(source_window,CV_WINDOW_AUTOSIZE);
	imshow(source_window,copy);
}
测试结果:
cvGoodFeaturesToTrack()与goodFeaturesToTrack()进行harris角点检测_第1张图片


题目: Shi-Tomasi角点检测子


你可能感兴趣的:(cvGoodFeaturesToTrack()与goodFeaturesToTrack()进行harris角点检测)