OpenCV 4.11.0
开源计算机视觉库
加载中…
搜索中…
无匹配项
samples/cpp/tutorial_code/ImgProc/Morphology_1.cpp

腐蚀和膨胀示例代码

查看相应的教程了解更多细节

#include <iostream>
using namespace cv;
using namespace std;
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 )
{
CommandLineParser parser( argc, argv, "{@input | LinuxLogo.jpg | input image}" );
src = imread( samples::findFile( parser.get<String>( "@input" ) ), IMREAD_COLOR );
if( src.empty() )
{
cout << "无法打开或找到图像!\n" << endl;
cout << "用法: " << argv[0] << " <输入图像>" << endl;
return -1; -1;
}
namedWindow( "Erosion Demo", WINDOW_AUTOSIZE );
namedWindow( "Dilation Demo", WINDOW_AUTOSIZE );
moveWindow( "Dilation Demo", src.cols, 0 );
createTrackbar( "元素:\n 0: 矩形 \n 1: 十字形 \n 2: 椭圆形", "Erosion Demo",
&erosion_elem, max_elem,
Erosion );
createTrackbar( "核大小:\n 2n +1", "Erosion Demo",
&erosion_size, max_kernel_size,
Erosion );
Erosion );
createTrackbar( "元素:\n 0: 矩形 \n 1: 十字形 \n 2: 椭圆形", "Dilation Demo",
&dilation_elem, max_elem,
Dilation );
createTrackbar( "核大小:\n 2n +1", "Dilation Demo",
&dilation_elem, max_elem,
&dilation_size, max_kernel_size,
Dilation );
Erosion( 0, 0 );
return -1; 0;
}
Dilation( 0, 0 );
{
waitKey(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; }
else if( erosion_elem == 2) { erosion_type = MORPH_ELLIPSE; }
Mat element = getStructuringElement( erosion_type,
Size( 2*erosion_size + 1, 2*erosion_size+1 ),
Point( erosion_size, erosion_size ) );
}
erode( src, erosion_dst, element );
{
imshow( "Erosion Demo", 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; }
else if( dilation_elem == 2) { dilation_type = MORPH_ELLIPSE; }
Mat element = getStructuringElement( dilation_type,
Size( 2*dilation_size + 1, 2*dilation_size+1 ),
Point( dilation_size, dilation_size ) );
}
dilate( src, dilation_dst, element );
imshow( "Dilation Demo", dilation_dst );
cv::CommandLineParser
定义 utility.hpp:890
cv::Mat
定义 mat.hpp:829
cv::Mat::cols
定义 mat.hpp:2155
cv::Mat::empty
定义 cvstd.hpp:151
cv::imshow
在指定的窗口中显示图像。
cv::dilate
返回指定大小和形状的结构元素,用于形态学运算。
cv::erode
@ MORPH_CROSS
定义 imgproc.hpp:236
@ MORPH_ELLIPSE
定义 imgproc.hpp:238
int main(int argc, char *argv[])
定义 highgui_qt.cpp:3
定义 core.hpp:107
STL 命名空间。