/*****************************************************
测试程序 【形态学运算】
时间:2016年8月25日
******************************************************/
// Global variables
Mat src, dst;
int morph_elem = 0;
int morph_size = 0;
int morph_operator = 0;
int const max_operator = 4;
int const max_elem = 2;
int const max_kernel_size = 21;
const char* window_name = "Morphology Transformations Demo";
void Morphology_Operations( int, void* );
int main(void)
{
/// Load an image
src = imread("YY01.jpg",1);
if( !src.data )
{ return -1; }
//【创建窗口】
namedWindow( window_name, WINDOW_AUTOSIZE );
//【创建滚动条回调函数】
//【选择形态学类型】
createTrackbar("Operator:\n 0: Opening - 1: Closing \n 2: Gradient - 3: Top Hat \n 4: Black Hat", window_name, &morph_operator, max_operator, Morphology_Operations );
//【选择核形状】
createTrackbar( "Element:\n 0: Rect - 1: Cross - 2: Ellipse", window_name,
&morph_elem, max_elem,
Morphology_Operations );
//【选择核尺寸】
createTrackbar( "Kernel size:\n 2n +1", window_name,
&morph_size, max_kernel_size,
Morphology_Operations );
//【默认初始化】
Morphology_Operations( 0, 0 );
waitKey(0);
return 0;
}
//【形态学操作】
void Morphology_Operations( int, void* )
{
// Since MORPH_X : 2,3,4,5 and 6
int operation = morph_operator + 2;
Mat element = getStructuringElement( morph_elem, Size( 2*morph_size + 1, 2*morph_size+1 ), Point( morph_size, morph_size ) );
/// Apply the specified morphology operation
morphologyEx( src, dst, operation, element );
imshow( window_name, dst );
}
【opencv练习16 - 形态学运算】
最新推荐文章于 2023-08-09 08:55:53 发布