一杯清酒邀明月
天下本无事,庸人扰之而烦耳。

 

 1 #include "opencv2/imgproc/imgproc.hpp"
 2 #include "opencv2/highgui/highgui.hpp"
 3 #include <stdlib.h>
 4 #include <stdio.h>
 5 
 6 using namespace cv;
 7 
 8 /// 全局变量
 9 
10 Mat src, src_gray;
11 Mat dst, detected_edges;
12 
13 int edgeThresh = 1;
14 int lowThreshold;
15 int const max_lowThreshold = 100;
16 int ratio = 3;
17 int kernel_size = 3;
18 char* window_name = "Edge Map";
19 
20 /**
21  * @函数 CannyThreshold
22  * @简介: trackbar 交互回调 - Canny阈值输入比例1:3
23  */
24 void CannyThreshold(int, void*)
25 {
26   /// 使用 3x3内核降噪
27   blur( src_gray, detected_edges, Size(3,3) );
28 
29   /// 运行Canny算子
30   Canny( detected_edges, detected_edges, lowThreshold, lowThreshold*ratio, kernel_size );
31 
32   /// 使用 Canny算子输出边缘作为掩码显示原图像
33   dst = Scalar::all(0);
34 
35   src.copyTo( dst, detected_edges);
36   imshow( window_name, dst );
37  }
38 
39 
40 /** @函数 main */
41 int main( int argc, char** argv )
42 {
43   /// 装载图像
44   src = imread( argv[1] );
45 
46   if( !src.data )
47   { return -1; }
48 
49   /// 创建与src同类型和大小的矩阵(dst)
50   dst.create( src.size(), src.type() );
51 
52   /// 原图像转换为灰度图像
53   cvtColor( src, src_gray, CV_BGR2GRAY );
54 
55   /// 创建显示窗口
56   namedWindow( window_name, CV_WINDOW_AUTOSIZE );
57 
58   /// 创建trackbar
59   createTrackbar( "Min Threshold:", window_name, &lowThreshold, max_lowThreshold, CannyThreshold );
60 
61   /// 显示图像
62   CannyThreshold(0, 0);
63 
64   /// 等待用户反应
65   waitKey(0);
66 
67   return 0;
68   }
posted on 2020-01-09 13:16  一杯清酒邀明月  阅读(252)  评论(0编辑  收藏  举报