cvFindCornerSubPix利用cvGoodFeaturesToTrack检测出来的角点的基础上,能够将角点位置精确到亚像素级精度。
cvGoodFeaturesToTrack的使用:
// http://blog.csdn.net/lov_zhu 2012.11.23
#include "stdio.h"
#include "cv.h"
#include "cxcore.h"
#include "highgui.h"
int main(int argc, char** argv)
{
FILE* fp = fopen("d:\\result.txt","w+" ) ;
const char* filename = "D:\\01.jpg" ;
//const char* windowname = "http://blog.csdn.net/lov_zhu" ;
//img AND img_copy is the same,use to draw different result
IplImage* img = cvLoadImage(filename,CV_LOAD_IMAGE_COLOR );
IplImage* img_copy = cvCloneImage( img ) ;//a copy of img
IplImage* img_gray = cvCreateImage(cvGetSize( img ),IPL_DEPTH_8U, 1) ;
IplImage* eig_image = cvCreateImage(cvGetSize( img ),IPL_DEPTH_32F, 1);
IplImage* temp_image = cvCloneImage( eig_image ) ;
cvCvtColor(img,img_gray,CV_BGR2GRAY);
const int MAX_CORNERS = 200 ;
CvPoint2D32f* corners = new CvPoint2D32f[ MAX_CORNERS ];
int corner_count = MAX_CORNERS;
double quality_level = 0.05; //OR 0.01
double min_distance = 50;
cvGoodFeaturesToTrack(
img_gray,
eig_image,
temp_image,
corners,
&corner_count,
quality_level,
min_distance
);
printf("发现角点的个数= %d\n",corner_count);
//draw corners from cvGoodFeaturesToTrack on "img"
for (int i = 0 ; i < corner_count ; ++i){
cvLine(
img,
cvPoint((int)corners[i].x,(int)corners[i].y) ,
cvPoint((int)corners[i].x,(int)corners[i].y),
CV_RGB(255,0,0),
5
);
fprintf(fp,"\t%f\t%f\n",corners[i].x,corners[i].y) ;
}
fprintf(fp,"\n\n\") ;
int half_win_size = 3;//the window size will be 3+1+3=7
int iteration = 20;
double epislon = 0.1;
cvFindCornerSubPix(
img_gray,
corners,
corner_count,
cvSize(half_win_size,half_win_size),
cvSize(-1,-1),//no ignoring the neighbours of the center corner
cvTermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS,iteration,epislon)
);
printf("发现角点的个数= %d\n",corner_count);
//draw subpix corners on "img_copy"
for (int i=0;i<corner_count;i++){
cvLine(
img_copy,
cvPoint((int)corners[i].x,(int)corners[i].y) ,
cvPoint((int)corners[i].x,(int)corners[i].y),
CV_RGB(255,0,0),
5
);
fprintf(fp,"\t%f\t%f\n",corners[i].x,corners[i].y) ;
}
cvNamedWindow("cvFindCornerSubPix", CV_WINDOW_AUTOSIZE );
cvShowImage( "cvFindCornerSubPix", img );
cvNamedWindow("cvGoodFeaturesToTrack", CV_WINDOW_AUTOSIZE );
cvShowImage( "cvGoodFeaturesToTrack", img_copy );
cvWaitKey(0);
cvReleaseImage(&img_gray);
cvReleaseImage(&img);
cvReleaseImage(&img_copy);
cvDestroyWindow("cvGoodFeaturesToTrack");
cvDestroyWindow("cvFindCornerSubPix");
return 0;
}