1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
#include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" #include <iostream> using namespace cv; using namespace std;
#define WINDOW_NAME "result"
Mat g_srcImage, g_grayImage; int g_maxCornerNumber = 33; int g_maxTrackbarNumber = 500; RNG g_rng(12345);
void on_GoodFeaturesToTrack(int, void *) { if (g_maxCornerNumber <= 1) { g_maxCornerNumber = 1; }
vector<Point2f> corners; double qualityLevel = 0.01; double minDistance = 10; int blockSize = 3; double k = 0.04; Mat copy = g_srcImage.clone();
goodFeaturesToTrack(g_grayImage, corners, g_maxCornerNumber, qualityLevel, minDistance, Mat(), blockSize, false, k);
cout << "\n\t>-------------此次检测到的角点数量为:" << corners.size() << endl;
int r = 4; for (unsigned int i = 0; i < corners.size(); i++) { circle(copy, corners[ i ], r, Scalar(g_rng.uniform(0, 255), g_rng.uniform(0, 255), g_rng.uniform(0, 255)), -1, 8, 0); }
imshow(WINDOW_NAME, copy);
Size winSize = Size(5, 5); Size zeroZone = Size(-1, -1); TermCriteria criteria = TermCriteria(TermCriteria::EPS + TermCriteria::MAX_ITER, 40, 0.001);
cornerSubPix(g_grayImage, corners, winSize, zeroZone, criteria);
for (int i = 0; i < corners.size(); i++) { cout << " \t>>精确角点坐标[" << i << "] (" << corners[ i ].x << "," << corners[ i ].y << ")" << endl; } }
int main() { g_srcImage = imread("1.jpg", 1); cvtColor(g_srcImage, g_grayImage, COLOR_BGR2GRAY);
namedWindow(WINDOW_NAME, WINDOW_AUTOSIZE); createTrackbar("最大角点数", WINDOW_NAME, &g_maxCornerNumber, g_maxTrackbarNumber, on_GoodFeaturesToTrack); imshow(WINDOW_NAME, g_srcImage); on_GoodFeaturesToTrack(0, 0);
waitKey(0); return 0; }
|