goodFeaturesToTrack()
作用:检测一张图片上的强壮的角点。
形式:void goodFeaturesToTrack(InputArray image, OutputArray corners, int maxCorners, double qualityLevel, double minDistance, InputArray mask=noArray(), int blockSize=3, bool useHarrisDetector=false, double k=0.04 );
参数:
image:输入8位或32位浮点单通道图像;
corners:输出检测角点的向量;
maxCorners:返回角落的最大数量,如果检测到更多的角度,就返回它们中的最强壮的一个;
qualityLevel:表征图像角点的最小可接受质量的参数,参数值乘以最好角点质量--最小特征值或哈里斯函数响应;
minDistance:返回角点之间可能的最小的欧氏距离;
mask:可选的感兴趣区域,如果角点是空的,它就指定了检测到角点的区域;
blockSize:用于计算每个像素领域的协变导数矩阵的平均块大小;
useHarrisDetector:使用哈里斯检测器的指示参数;
k:哈里斯探测器的自由参数;
struct TermCriteria()
作用:设置迭代算法的终止条件;
形式:TermCriteria(int type,int max_iter, double epsilon);
参数:
type:终止条件类型;
CV_TERMCRIT_ITER--max_iter达到最大值后停止算法;
CV_TERMCRIT_EPS--当算法依赖的精确度低于epsilon后,停止算法;
CV_TERMCRIT_ITER+CV_TERMCRIT_EPS--当max_iter达到最大值或算法依赖的精确度低于epsilon任一个满足时,停止算法;
max_iter:最大迭代次数;
epsilon:要求精度;
cornerSubPix()
作用:细化角点位置;
形式:void cornerSubPix(InputArray image, InputOutputArray corners, Size winSize, Size zeroZone, TermCriteria criteria);
参数:
image:输入图像;
corners:初始化输入角点的坐标,为输出提供细化的坐标;
winSize:搜索窗口的边长的一半;
zeroZone:搜索区域中间的死区的一半大小,对它在下边的求和公式不计算,有时候它用来避免可能的自相关矩阵的奇异性,(-1,-1)用来表明这里没有这样的规模;
criteria:角点细化的迭代过程的终止条件;
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include
#include
#include
using namespace cv;
using namespace std;
/// Global variables
Mat src, src_gray;
int maxCorners = 10;
int maxTrackbar = 25;
RNG rng(12345);
char* source_window = "Image";
/// Function header
void goodFeaturesToTrack_Demo( int, void* );
/** @function main */
int main( int argc, char** argv )
{
/// Load source image and convert it to gray
src = imread( argv[1], 1 );
cvtColor( src, src_gray, CV_BGR2GRAY );
/// Create Window
namedWindow( source_window, CV_WINDOW_AUTOSIZE );
/// Create Trackbar to set the number of corners
createTrackbar( "Max corners:", source_window, &maxCorners, maxTrackbar, goodFeaturesToTrack_Demo);
imshow( source_window, src );
goodFeaturesToTrack_Demo( 0, 0 );
waitKey(0);
return(0);
}
/**
* @function goodFeaturesToTrack_Demo.cpp
* @brief Apply Shi-Tomasi corner detector
*/
void goodFeaturesToTrack_Demo( int, void* )
{
if( maxCorners < 1 ) { maxCorners = 1; }
/// Parameters for Shi-Tomasi algorithm
vector corners;
double qualityLevel = 0.01;
double minDistance = 10;
int blockSize = 3;
bool useHarrisDetector = false;
double k = 0.04;
/// Copy the source image
Mat copy;
copy = src.clone();
/// Apply corner detection
goodFeaturesToTrack( src_gray,
corners,
maxCorners,
qualityLevel,
minDistance,
Mat(),
blockSize,
useHarrisDetector,
k );
/// Draw corners detected
cout<<"** Number of corners detected: "<