Returns a structuring element of the specified size and shape for morphological operations.
Parameters: |
|
---|
The function constructs and returns the structuring element that can be further passed to erode(), dilate() ormorphologyEx() . But you can also construct an arbitrary binary mask yourself and use it as the structuring element.
Note
When using OpenCV 1.x C API, the created structuring element IplConvKernel* element must be released in the end using cvReleaseStructuringElement(&element).
Dilates an image by using a specific structuring element.
Parameters: |
|
---|
The function dilates the source image using the specified structuring element that determines the shape of a pixel neighborhood over which the maximum is taken:
The function supports the in-place mode. Dilation can be applied several ( iterations ) times. In case of multi-channel images, each channel is processed independently.
See also
erode(), morphologyEx(), getStructuringElement()
Note
Erodes an image by using a specific structuring element.
Parameters: |
|
---|
The function erodes the source image using the specified structuring element that determines the shape of a pixel neighborhood over which the minimum is taken:
The function supports the in-place mode. Erosion can be applied several ( iterations ) times. In case of multi-channel images, each channel is processed independently.
See also
dilate(), morphologyEx(), getStructuringElement()
Note
Creates a trackbar and attaches it to the specified window.
Parameters: |
|
---|
The function createTrackbar creates a trackbar (a slider or range control) with the specified name and range, assigns a variable value to be a position synchronized with the trackbar and specifies the callback function onChange to be called on the trackbar position change. The created trackbar is displayed in the specified window winname.
Note
[Qt Backend Only] winname can be empty (or NULL) if the trackbar should be attached to the control panel.
Clicking the label of each trackbar enables editing the trackbar values manually.
Note
下面是本教程的源码, 你也可以从 here 下载。
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
using namespace cv;
Mat src, erosion_dst, dilation_dst;
int erosion_elem = 1;
int erosion_size = 1;
int dilation_elem = 1;
int dilation_size = 1;
int const max_elem = 3;
int const max_kernel_size = 21;
/** Function Headers */
void Erosion( int, void* );
void Dilation( int, void* );
/** @function main */
int main( )
{
/// Load 图像
src = imread( "tupian.jpg" );
if( !src.data )
{ return -1; }
/// 创建显示窗口
namedWindow( "Erosion Demo", CV_WINDOW_AUTOSIZE );
namedWindow( "Dilation Demo", CV_WINDOW_AUTOSIZE );
cvMoveWindow( "Dilation Demo", src.cols, 0 );
/// 创建腐蚀 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 );
/// 创建膨胀 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 );
/// Default start
Erosion( 1, 0 );
Dilation( 1, 0 );
waitKey(0);
return 0;
}
/** @function Erosion */
void Erosion( int, void* )
{
int erosion_type;
if( erosion_elem == 1 ){ erosion_type = MORPH_RECT; }
else if( erosion_elem == 2 ){ erosion_type = MORPH_CROSS; }
else if( erosion_elem == 3 ) { 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 );
}
/** @function Dilation */
void Dilation( int, void* )
{
int dilation_type;
if( dilation_elem == 1){ dilation_type = MORPH_RECT; }
else if( dilation_elem == 2 ){ dilation_type = MORPH_CROSS; }
else if( dilation_elem == 3) { 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 );
}
大部分代码应该不需要解释了 (如果有任何疑问,请回头参考前面的教程)。 让我们来回顾一下本程序的总体流程:
让我们分析一下这两个函数:
Erosion:
/** @function Erosion */
void Erosion( int, void* )
{
int erosion_type;
if( erosion_elem == 1){ erosion_type = MORPH_RECT; }
else if( erosion_elem == 2 ){ erosion_type = MORPH_CROSS; }
else if( erosion_elem == 3 ) { 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 );
}
进行 腐蚀 操作的函数是 erode 。 它接受了三个参数:
src: 原图像
erosion_dst: 输出图像
element: 腐蚀操作的内核。 如果不指定,默认为一个简单的 矩阵。否则,我们就要明确指定它的形状,可以使用函数 getStructuringElement:
Mat element = getStructuringElement( erosion_type, Size( 2*erosion_size + 1, 2*erosion_size+1 ), Point( erosion_size, erosion_size ) );
我们可以为我们的内核选择三种形状之一:
- 矩形: MORPH_RECT
- 交叉形: MORPH_CROSS
- 椭圆形: MORPH_ELLIPSE
然后,我们还需要指定内核大小,以及 锚点 位置。不指定锚点位置,则默认锚点在内核中心位置。
就这些了,我们现在可以对图像进行腐蚀操作了。
Note
OpenCV的 erode 函数还有另外的参数,其中一个参数允许你一下对图像进行多次腐蚀操作。在这个简单的文档中没有用到它,但是你可以参考OpenCV的使用手册。
Dilation:
下面是膨胀的代码,你可以看到,它和 Erosion 函数是多么相似。 这里我们同样可以指定内核的形状,锚点和大小。
/** @function Dilation */
void Dilation( int, void* )
{
int dilation_type;
if( dilation_elem == 1 ){ dilation_type = MORPH_RECT; }
else if( dilation_elem == 2 ){ dilation_type = MORPH_CROSS; }
else if( dilation_elem == 3 ) { 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 );
}