代码示例:
#include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> #include<iostream> #include<vector> using namespace cv; using namespace std; #define WINDOW_NAME "Shi-Tomasi角点检测" Mat src, gray; int maxCornerNum = 33; int maxTrackbarNum = 500; RNG rng(12345); //滚动条回调函数 void onGoodFeaturesToTrack(int, void*){ if (maxCornerNum <= 1){ maxCornerNum = 1; } //参数准备 vector<Point2d> corners; double qualityLevel=0.01;//角点检测可接受的最小特征值 double minDistance = 10;//角点之间的最小距离 int blockSize = 3;//计算导数自相关矩阵时的指定的领域范围 double k = 0.04;//权重系数 Mat copy = src.clone(); //进行Shi-Tomasi角点检测 goodFeaturesToTrack(gray, corners, maxCornerNum, qualityLevel, minDistance, Mat(), blockSize, false, k); //输出文字信息 cout << "此次检测到的角点数量为:" << corners.size() << endl; //绘制检测到的角点 int r = 4; for (int i = 0; i < corners.size(); i++){ circle(copy, corners[i], r, Scalar(255,0,0), -1, 8, 0); } imshow(WINDOW_NAME, copy); } int main(){ src = imread("church.jpg", 1); cvtColor(src, gray, COLOR_BGR2GRAY); namedWindow(WINDOW_NAME, WINDOW_AUTOSIZE); createTrackbar("最大角点数", WINDOW_NAME, &maxCornerNum, maxTrackbarNum, onGoodFeaturesToTrack); imshow(WINDOW_NAME, src); onGoodFeaturesToTrack(0, 0); waitKey(0); return 0; }
效果: