Shi-Tomasi角点检测算法
Shi-Tomasi角点检测算法
1、Shi-Tomasi概念;
2、API;
3、Code;
Shi-Tomasi概念
1、角点检测除了Harris角点检测算法外,还常常使用Shi-Tomasi算法,Shi-Tomasi算法是harris算法的改进,因此计算速度更快 , Shi-Tomasi算法于1994年在文章《Good Features to Track》中被提出;
API
1、Shi-Tomasi角点检测API: goodFeatureToTrack()
:
参数说明:
corners : 为输出角点坐标的集合数组;
qualityLevel : 角点检测的最小特征值,通常不会超过1(常取0.10/0.01);
maxCorners : 自定检测最大角点数目;
k : 常取(0.04-0.06);
Code
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
using namespace cv;
using namespace std;
#define WIN_NAME "Shi-Tomasi角点检测"
Mat srcImage, grayImage;
int maxCornerNumber = 33;
int maxTrackbarNumber = 500;
RNG rng(12345);
void on_GoodFeatureToTrack(int, void *)
{
if(maxCornerNumber <= 1)
{
maxCornerNumber = 1;
}
//Shi-Tomasi参数准备
vector<Point2f> corners;
double qualityLevel = 0.01; //角点检测可以接受的最小特征值
double minDistance = 10; //角点间的最小像素距离设置
int blockSize = 3; //计算导数自相关矩阵时指定的领域范围
double k = 0.04; //权重系数
Mat copy = srcImage.clone(); //复制原图到一个临时变量中,作为感兴趣区域
//Shi-Tomasi Test
goodFeaturesToTrack(grayImage, corners, maxCornerNumber, qualityLevel, minDistance, Mat(), blockSize, false, k);
//输出文字信息
cout << ">此次检测到的角点数量为: " << corners.size() << endl;
//绘制检测到的角点
for (unsigned int i = 0; i < corners.size(); i++)
{
circle(copy, corners[i], 5, Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255)), -1, 8, 0);
}
imshow(WIN_NAME, copy);
}
int main(int argc, char** argv)
{
srcImage = imread("C:\\Users\\hello\\Desktop\\3.jpg");
cvtColor(srcImage, grayImage, CV_BGR2GRAY);
namedWindow(WIN_NAME, WINDOW_AUTOSIZE);
createTrackbar("最大角点数:", WIN_NAME, &maxCornerNumber, maxTrackbarNumber, on_GoodFeatureToTrack);
//imshow(WIN_NAME, srcImage);
on_GoodFeatureToTrack(0, 0);
waitKey(0);
return 0;
}
效果
(计算速度比Harris快了很多!!!Shi-Tomasi算法可以简单输出角点的坐标点)
识别角点,并输出像素值坐标: