Harris角点检测实现

 

 

 1 #include <opencv2/opencv.hpp>
 2 #include <iostream>
 3 
 4 using namespace cv;
 5 using namespace std;
 6 Mat src, gray_src;
 7 int thresh = 130;
 8 int max_count = 255;
 9 const char* output_title = "HarrisCornerDetection Result";
10 void Harris_Demo(int, void*);
11 int main(int argc, char** argv) {
12 
13     src = imread("L:/6.jpg");
14     if (src.empty()) {
15         printf("could not load image...\n");
16         return -1;
17     }
18     namedWindow("input image", CV_WINDOW_AUTOSIZE);
19     imshow("input image", src);
20 
21     namedWindow(output_title, CV_WINDOW_AUTOSIZE);
22     cvtColor(src, gray_src, COLOR_BGR2GRAY);
23     createTrackbar("Threshold:", output_title, &thresh, max_count, Harris_Demo);
24     //&thresh为滑动窗口初始默认值130
25     Harris_Demo(0, 0);
26 
27     waitKey(0);
28     return 0;
29 }
30 
31 void Harris_Demo(int, void*) {
32     Mat dst, norm_dst, normScaleDst;
33     dst = Mat::zeros(gray_src.size(), CV_32FC1);
34 
35     int blockSize = 2;  
36     int ksize = 3;
37     double k = 0.04;
38     cornerHarris(gray_src, dst, blockSize, ksize, k, BORDER_DEFAULT);
39     //参数:输入灰度图像,输出图像,blockSize参数矩阵的大小,ksize窗口大小,k角度响应参数0.04-0.06, 默认
40     normalize(dst, norm_dst, 0, 255, NORM_MINMAX, CV_32FC1, Mat());
41     //归一化函数:NORM_MINMAX为一般常用的方法,CV_32FC1 输出矩阵与输入矩阵类型相同
42     convertScaleAbs(norm_dst, normScaleDst);
43     //对矩阵取绝对值
44 
45     Mat resultImg = src.clone();
46     // 克隆彩色图片src到resultImg
47 
48     for (int row = 0; row < resultImg.rows; row++) {
49         uchar* currentRow = normScaleDst.ptr(row);       //行指针,指向一行的数据
50         for (int col = 0; col < resultImg.cols; col++) {
51             int value = (int)*currentRow;    //通过指针拿出数据值
52             if (value > thresh) {
53                 circle(resultImg, Point(col, row), 2, Scalar(0, 0, 255), 2, 8, 0);
54                 //画圆
55             }
56             currentRow++;
57         }
58     }
59 
60     imshow(output_title, resultImg);
61 }

效果:

 

猜你喜欢

转载自www.cnblogs.com/Jack-Elvis/p/11640970.html