前面学过了自定义角点检测器(Harris),现在学习tomasi自定义角点检测,总体上来说tomasi的代码计算量比较harris少,具体步骤跟harris自定义角点检测类似,主要少了"计算响应"(在博客自定义角点检测器(harris自定义)的代码注释里)的那一步,主要使用cornerMinEigenVal()函数来计算最小特征值。
参考:https://blog.csdn.net/u014310328/article/details/46434321
cornerMinEigenVal()
作用:计算梯度矩阵的最小特征值用于角点检测。
形式:void cornerMinEigenVal(InputArray src, OutputArray dst, int blockSize, int ksize=3, int borderType=BORDER_DEFAULT );
参数:
src:输入单通道8位或浮点图像;
dst:用来存储最小特征值的图像;它有src相同的大小和类型为CV_32FC1;
blockSize:领域尺寸;一般取3
ksize:Sobel()算子的孔径参数;一般取3,5,7
borderType:像素外推方式;
现在放上代码:
#include<opencv2/opencv.hpp>
#include<iostream>
#include<math.h>
using namespace cv;
using namespace std;
Mat src, gray_src;
Mat tomasi_res;
//存储图像中的最大值和最小值
double tomasi_min;
double tomasi_max;
//计算百分比,拖动TrackBar
int current_value = 30;
int max_value = 100;
const char* output_title = "custom tomasi corner title";
void custom_tomasi_corner(int, void*);
int main(int argc, char** argv)
{
src = imread("D:/test/大厦.jpg");
if (!src.data)
{
cout << "the image could not found..." << endl;
return -1;
}
cvtColor(src, gray_src, COLOR_BGR2GRAY);
tomasi_res = Mat::zeros(src.size(), CV_32FC1);
imshow("input title", src);
//计算最小特征值
int blocksize = 3;
int ksize = 3;
cornerMinEigenVal(gray_src, tomasi_res, blocksize, ksize, BORDER_DEFAULT);
minMaxLoc(tomasi_res, &tomasi_min, &tomasi_max, 0, 0, Mat());
namedWindow(output_title, CV_WINDOW_AUTOSIZE);
createTrackbar("tomasi-bar", output_title, ¤t_value, max_value, custom_tomasi_corner);
custom_tomasi_corner(0, 0);
waitKey(0);
return 0;
}
void custom_tomasi_corner(int, void *)
{
if (current_value < 10)
{
current_value = 10;
}
Mat resultImg = src.clone();
float t = tomasi_min + (((double)current_value) / max_value)*(tomasi_max - tomasi_min);
for (int row = 0; row < src.rows; row++)
{
for (int col = 0; col < src.cols; col++)
{
float v = tomasi_res.at<float>(row, col);
if (v > t)
{
circle(resultImg, Point(col, row), 2, Scalar(0, 0, 255), 2, 8, 0);
}
}
}
imshow(output_title, resultImg);
}
运行结果:
原图:
结果图(值=30):
值=11:
与上一篇博客的运行结果做比较,tomasi的反应速度较快,不容易出现窗口崩溃的情况,但tomasi的计算结果比较低,同是值=30,但harris的角点检测数远远大于tomasi的检点检测数。