OpenCV:cvGoodFeaturesToTrack 和cvFindCorner特征点检测

角点检测(Corner Detection) cvFindCornerSubPix使用范例

原文来自OpenCV中文论坛:http://www.opencv.org.cn/forum/viewtopic.php?t=8777

这段范例包含如何使用函数cvGoodFeaturesToTrackcvFindCornerSubPix 来进行角点检测的(CornerDetection)。 我发现cvFindCornerSubPix的范例在网络上比较少,所以把我的这段程序整理了出来,并给出这个函数最后2个参数的解释,希望对大家有帮助。

注意, cvFindCornerSubPix函数的使用必须是在cvGoodFeaturesToTrack实现基础上的,因为是subpix函数是对于goodfeature函数结果的进一步精确估计。估计的方法参见OReilly.Learning.OpenCV.pp. 319-321。

另外,cvFindCornerSubPix的使用难点是最后的两个参数,第一个,cvSize(-1,-1)表示不忽略corner临近的像素进行精确估计,如果设置成cvSize(1,1)就表示成忽略掉相邻1个像素,再进行精确估计。原因是出于计算的角度,如果过于考虑相邻的像素,可能不会得出可逆的矩阵。(详见P321)

最后一个参数,对于精确估计,有2个参数可以限制精度:迭代次数(iteration)或者最小精度(epsilon),可以单一使用,也可以同时使用。在这个例子中,我同时使用了2个。(可以简单理解为,如果达不到epsilon=0.01的时候,就迭代iteration=20次吧,如果10次就达到了0.01,也就不用迭代20次了。)结果和源码如下:
#include "stdafx.h"
#include <iostream>
#include <stdlib.h>

#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include <math.h>

using namespace std;

//by Huang, Haiqiao 5 Dec. 2009
int main(int argc, char** argv)
{
cout << "Corner DetectionOpenCV!"<<endl;
char* filename="D:\OpenCV_stuff\SampleImages\pattern.bmp";
IplImage* imgRGB = cvLoadImage(filename);
IplImage* imgRGB2 = cvLoadImage(filename);
IplImage* imgGrey =cvLoadImage(filename,CV_LOAD_IMAGE_GRAYSCALE);

if (imgGrey==NULL){//image validation
cout << "No valid imageinput."<<endl;
char c=getchar();
return 1;
}
int w=imgGrey->width;
int h=imgGrey->height;

IplImage* eig_image = cvCreateImage(cvSize(w, h),IPL_DEPTH_32F,1);
IplImage* temp_image = cvCreateImage(cvSize(w, h),IPL_DEPTH_32F,1);

const int MAX_CORNERS = 140;//estimate a corner number
CvPoint2D32f corners[MAX_CORNERS] = {0};// coordinates ofcorners
//CvPoint2D32f* corners = new CvPoint2D32f[ MAX_CORNERS ];//another method of declaring an array
int corner_count = MAX_CORNERS;
double quality_level = 0.1;//threshold for the eigenvalues
double min_distance = 5;//minimum distance between twocorners
int eig_block_size = 3;//window size
int use_harris = false;//use 'harris method' or not

//----------initial guess bycvGoodFeaturesToTrack---------------
cvGoodFeaturesToTrack(imgGrey,
eig_image, // output
temp_image,
corners,
&corner_count,
quality_level,
min_distance,
NULL,
eig_block_size,
use_harris);


int r=2; //rectangle size
int lineWidth=1; // rectangle line width
//-----draw good feature corners on the original RGBimage---------
for (int i=0;i<corner_count;i++){
cvRectangle(imgRGB2, cvPoint(corners[i].x-r,corners[i].y-r),
cvPoint(corners[i].x+r,corners[i].y+r),cvScalar(255,0,0),lineWidth);
}

int half_win_size=3;//the window size will be 3+1+3=7
int iteration=20;
double epislon=0.1;
cvFindCornerSubPix(
imgGrey,
corners,
corner_count,
cvSize(half_win_size,half_win_size),
cvSize(-1,-1),//no ignoring the neighbours of the centercorner
cvTermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS,iteration,epislon)
);

//------draw subpix corners on another original RGBimage------------
for (int i=0;i<corner_count;i++){
cvRectangle(imgRGB, cvPoint(corners[i].x-r,corners[i].y-r),
cvPoint(corners[i].x+r,corners[i].y+r),cvScalar(0,0,255),lineWidth);
}

//to display a coordinate of the third corner
cout<<"x="<<corners[2].x;
cout<<",y="<<corners[2].y<<endl;

cvNamedWindow("cvFindCornerSubPix", CV_WINDOW_AUTOSIZE );
cvShowImage( "cvFindCornerSubPix", imgRGB );
cvNamedWindow("cvGoodFeaturesToTrack", CV_WINDOW_AUTOSIZE );
cvShowImage( "cvGoodFeaturesToTrack", imgRGB2 );


cvWaitKey(0);
cvReleaseImage(&imgGrey);
cvReleaseImage(&imgRGB);
cvReleaseImage(&imgRGB2);
cvDestroyWindow("cvGoodFeaturesToTrack");
cvDestroyWindow("cvFindCornerSubPix");

//char c=getchar();
return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值