一杯清酒邀明月
天下本无事,庸人扰之而烦耳。
posts - 3121,comments - 209,views - 578万

 

复制代码
 1 #include "opencv2/imgproc/imgproc.hpp"
 2 #include "opencv2/highgui/highgui.hpp"
 3 #include "highgui.h"
 4 #include <stdlib.h>
 5 #include <stdio.h>
 6 
 7 using namespace cv;
 8 
 9 /// 全局变量
10 Mat src, erosion_dst, dilation_dst;
11 
12 int erosion_elem = 0;
13 int erosion_size = 0;
14 int dilation_elem = 0;
15 int dilation_size = 0;
16 int const max_elem = 2;
17 int const max_kernel_size = 21;
18 
19 /** Function Headers */
20 void Erosion( int, void* );
21 void Dilation( int, void* );
22 
23 /** @function main */
24 int main( int argc, char** argv )
25 {
26   /// Load 图像
27   src = imread( argv[1] );
28 
29   if( !src.data )
30   { return -1; }
31 
32   /// 创建显示窗口
33   namedWindow( "Erosion Demo", CV_WINDOW_AUTOSIZE );
34   namedWindow( "Dilation Demo", CV_WINDOW_AUTOSIZE );
35   cvMoveWindow( "Dilation Demo", src.cols, 0 );
36 
37   /// 创建腐蚀 Trackbar
38   createTrackbar( "Element:\n 0: Rect \n 1: Cross \n 2: Ellipse", "Erosion Demo",
39                   &erosion_elem, max_elem,
40                   Erosion );
41 
42   createTrackbar( "Kernel size:\n 2n +1", "Erosion Demo",
43                   &erosion_size, max_kernel_size,
44                   Erosion );
45 
46   /// 创建膨胀 Trackbar
47   createTrackbar( "Element:\n 0: Rect \n 1: Cross \n 2: Ellipse", "Dilation Demo",
48                   &dilation_elem, max_elem,
49                   Dilation );
50 
51   createTrackbar( "Kernel size:\n 2n +1", "Dilation Demo",
52                   &dilation_size, max_kernel_size,
53                   Dilation );
54 
55   /// Default start
56   Erosion( 0, 0 );
57   Dilation( 0, 0 );
58 
59   waitKey(0);
60   return 0;
61 }
62 
63 /**  @function Erosion  */
64 void Erosion( int, void* )
65 {
66   int erosion_type;
67   if( erosion_elem == 0 ){ erosion_type = MORPH_RECT; }
68   else if( erosion_elem == 1 ){ erosion_type = MORPH_CROSS; }
69   else if( erosion_elem == 2) { erosion_type = MORPH_ELLIPSE; }
70 
71   Mat element = getStructuringElement( erosion_type,
72                                        Size( 2*erosion_size + 1, 2*erosion_size+1 ),
73                                        Point( erosion_size, erosion_size ) );
74 
75   /// 腐蚀操作
76   erode( src, erosion_dst, element );
77   imshow( "Erosion Demo", erosion_dst );
78 }
79 
80 /** @function Dilation */
81 void Dilation( int, void* )
82 {
83   int dilation_type;
84   if( dilation_elem == 0 ){ dilation_type = MORPH_RECT; }
85   else if( dilation_elem == 1 ){ dilation_type = MORPH_CROSS; }
86   else if( dilation_elem == 2) { dilation_type = MORPH_ELLIPSE; }
87 
88   Mat element = getStructuringElement( dilation_type,
89                                        Size( 2*dilation_size + 1, 2*dilation_size+1 ),
90                                        Point( dilation_size, dilation_size ) );
91   ///膨胀操作
92   dilate( src, dilation_dst, element );
93   imshow( "Dilation Demo", dilation_dst );
94 }
复制代码
posted on   一杯清酒邀明月  阅读(306)  评论(0编辑  收藏  举报
编辑推荐:
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
阅读排行:
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
· 全程使用 AI 从 0 到 1 写了个小工具
· 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

点击右上角即可分享
微信分享提示