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
| #include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" #include <opencv2/opencv.hpp> using namespace cv; using namespace std;
#define WINDOW_NAME1 "harris" #define WINDOW_NAME2 "result"
Mat g_srcImage, g_srcImage1, g_grayImage; int thresh = 30; int max_thresh = 175;
void on_CornerHarris(int, void *);
int main(int argc, char **argv) {
g_srcImage = imread("circles.jpg", 1); if (!g_srcImage.data) { printf("读取图片错误,请确定目录下是否有imread函数指定的图片存在~! \n"); return false; } imshow("src", g_srcImage); g_srcImage1 = g_srcImage.clone();
cvtColor(g_srcImage1, g_grayImage, COLOR_BGR2GRAY);
namedWindow(WINDOW_NAME1, WINDOW_AUTOSIZE); createTrackbar("阈值: ", WINDOW_NAME1, &thresh, max_thresh, on_CornerHarris);
on_CornerHarris(0, 0);
waitKey(0); return (0); }
void on_CornerHarris(int, void *) { Mat dstImage; Mat normImage; Mat scaledImage;
dstImage = Mat::zeros(g_srcImage.size(), CV_32FC1); g_srcImage1 = g_srcImage.clone();
cornerHarris(g_grayImage, dstImage, 2, 3, 0.04, BORDER_DEFAULT);
normalize(dstImage, normImage, 0, 255, NORM_MINMAX, CV_32FC1, Mat()); convertScaleAbs(normImage, scaledImage);
for (int j = 0; j < normImage.rows; j++) { for (int i = 0; i < normImage.cols; i++) { if ((int)normImage.at<float>(j, i) > thresh + 80) { circle(g_srcImage1, Point(i, j), 5, Scalar(10, 10, 255), 2, 8, 0); circle(scaledImage, Point(i, j), 5, Scalar(0, 10, 255), 2, 8, 0); } } } imshow(WINDOW_NAME1, g_srcImage1); imshow(WINDOW_NAME2, scaledImage); }
|