/******************************************************
测试程序 腐蚀膨胀
时间:2016年8月24日
******************************************************/
//【全局变量*3】 Global variables
Mat src;
Mat erosion_dst;
Mat 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(void)
{
/// Load an image
src = imread("YY01.jpg");
if( !src.data )
{ return -1; }
// Create windows
namedWindow( "Erosion Demo", WINDOW_AUTOSIZE ); //腐蚀,窗口
namedWindow( "Dilation Demo", WINDOW_AUTOSIZE ); //膨胀,窗口
moveWindow( "Dilation Demo", src.cols, 0 );
//****************************************************
//【创建回调函数滚动条】 Create Erosion Trackbar
createTrackbar( "Element:\n 0: Rect \n 1: Cross \n 2: Ellipse", "Erosion Demo",
&erosion_elem, max_elem,
Erosion );
createTrackbar( "Kernel size:\n 2n +1", "Erosion Demo",
&erosion_size, max_kernel_size,
Erosion );
/// Create Dilation Trackbar
createTrackbar( "Element:\n 0: Rect \n 1: Cross \n 2: Ellipse", "Dilation Demo",
&dilation_elem, max_elem,
Dilation );
createTrackbar( "Kernel size:\n 2n +1", "Dilation Demo",
&dilation_size, max_kernel_size,
Dilation );
Erosion( 0, 0 );
Dilation( 0, 0 );
waitKey(0);
return 0;
}
// @function Erosion
// 【腐蚀】
// 参数分析:
// int //鼠标交互传值
// void* //绑定的滚动条
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 ) );
//【参数配置完毕——应用腐蚀】 Apply the erosion operation
erode( src, erosion_dst, element );
imshow( "Erosion Demo", erosion_dst );
}
// * @function Dilation
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 ) );
//【参数配置完毕——应用膨胀】 Apply the dilation operation
dilate( src, dilation_dst, element );
imshow( "Dilation Demo", dilation_dst );
}
【opencv练习15 - 腐蚀膨胀】
最新推荐文章于 2024-03-02 11:27:25 发布