#include <iostream>
Mat src, erosion_dst, dilation_dst;
int erosion_elem = 0;
int erosion_size = 0;
int dilation_elem = 0;
int dilation_size = 0;
int const max_elem = 2;
int const max_kernel_size = 21;
void Erosion( int, void* );
void Dilation( int, void* );
int main(
int argc,
char** argv )
{
src = imread( samples::findFile( parser.get<
String>(
"@input" ) ), IMREAD_COLOR );
{
cout << "无法打开或找到图像!\n" << endl;
cout << "使用方法: " << argv[0] << " <输入图像>" << endl;
return -1;
}
namedWindow( "腐蚀演示", WINDOW_AUTOSIZE );
namedWindow( "膨胀演示", WINDOW_AUTOSIZE );
moveWindow(
"膨胀演示", src.
cols, 0 );
createTrackbar( "元素:\n 0: 矩形 \n 1: 十字形 \n 2: 椭圆形", "腐蚀演示",
&erosion_elem, max_elem,
Erosion );
createTrackbar( "内核大小:\n 2n +1", "腐蚀演示",
&erosion_size, max_kernel_size,
Erosion );
createTrackbar( "元素:\n 0: 矩形 \n 1: 十字形 \n 2: 椭圆形", "膨胀演示",
&dilation_elem, max_elem,
Dilation );
createTrackbar( "内核大小:\n 2n +1", "膨胀演示",
&dilation_size, max_kernel_size,
Dilation );
Erosion( 0, 0 );
Dilation( 0, 0 );
waitKey(0);
return 0;
}
void Erosion( int, void* )
{
int erosion_type = 0;
if( erosion_elem == 0 ){ erosion_type =
MORPH_RECT; }
else if( erosion_elem == 1 ){ erosion_type =
MORPH_CROSS; }
Size( 2*erosion_size + 1, 2*erosion_size+1 ),
Point( erosion_size, erosion_size ) );
erode( src, erosion_dst, element );
imshow(
"腐蚀演示", erosion_dst );
}
void Dilation( int, void* )
{
int dilation_type = 0;
if( dilation_elem == 0 ){ dilation_type =
MORPH_RECT; }
else if( dilation_elem == 1 ){ dilation_type =
MORPH_CROSS; }
Size( 2*dilation_size + 1, 2*dilation_size+1 ),
Point( dilation_size, dilation_size ) );
dilate( src, dilation_dst, element );
imshow(
"膨胀演示", dilation_dst );
}
用于命令行解析。
定义 utility.hpp:820
bool empty() const
如果数组没有元素,则返回 true。
用于指定图像或矩形大小的模板类。
定义 types.hpp:335
std::string String
定义 cvstd.hpp:151
void imshow(const String &winname, InputArray mat)
在指定的窗口中显示图像。
void dilate(InputArray src, OutputArray dst, InputArray kernel, Point anchor=Point(-1,-1), int iterations=1, int borderType=BORDER_CONSTANT, const Scalar &borderValue=morphologyDefaultBorderValue())
使用特定的结构元素膨胀图像。
Mat getStructuringElement(int shape, Size ksize, Point anchor=Point(-1,-1))
返回用于形态学操作的指定大小和形状的结构元素。
void erode(InputArray src, OutputArray dst, InputArray kernel, Point anchor=Point(-1,-1), int iterations=1, int borderType=BORDER_CONSTANT, const Scalar &borderValue=morphologyDefaultBorderValue())
使用特定的结构元素腐蚀图像。
@ MORPH_RECT
矩形结构元素
定义 imgproc.hpp:235
@ MORPH_CROSS
定义 imgproc.hpp:236
@ MORPH_ELLIPSE
定义 imgproc.hpp:238
int main(int argc, char *argv[])
定义 highgui_qt.cpp:3
与磁盘上的文件关联的文件存储的“黑盒”表示。
定义 core.hpp:102