[190216][opencv] getStructuringElement()

一、Prototype

Mat cv::getStructuringElement 	( 	int  	shape,
									Size  	ksize,
									Point  	anchor = Point(-1,-1) 
	) 	

returns a structuring element of the specified size and shape for morphological operations.
the function constructs and returns the structuring element that can be further passed to erode, dilate or morphologyEx. but you can also construct an arbitrary binary mask yourself and use it as the structuring element.

Parameters
shape Element shape that could be one of MorphShapes
ksize Size of the structuring element.
anchor Anchor position within the element. The default value (−1,−1) means that the anchor is at the center. Note that only the shape of a cross-shaped element depends on the anchor position. In other cases the anchor just regulates how much the result of the morphological operation is shifted.

二、Demo

#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include 

using namespace std;
using namespace cv;

int main()
{
	Mat element;
	element = getStructuringElement(MORPH_ELLIPSE, Size(100, 100));
	//other parameters: element = getStructuringElement(...,Size(..., ...));
	cout << element;
	return 0;
}

三、Result

1/element = getStructuringElement(MORPH_ELLIPSE, Size(100, 100));
copy the result to an Excel file and find out its shape is an ellipse, the picture is as follow:
[190216][opencv] getStructuringElement()_第1张图片note: 1/(about the picture) i don’t know why there is a yellow block at the right bottom after i check the settings in Excel which turns out to be no wrong.
2/the shape of ellipse is not so obvious when the second parameter is set small. i.e.

element = getStructuringElement(MORPH_ELLIPSE, Size(5, 5));

result:
[ 0, 0, 1, 0, 0;
1, 1, 1, 1, 1;
1, 1, 1, 1, 1;
1, 1, 1, 1, 1;
0, 0, 1, 0, 0]
2/element = getStructuringElement(MORPH_CROSS, Size(4, 4));
result:
[ 0, 0, 1, 0;
0, 0, 1, 0;
1, 1, 1, 1;
0, 0, 1, 0]

element = getStructuringElement(MORPH_CROSS, Size(5, 5));

result:
[ 0, 0, 1, 0, 0;
0, 0, 1, 0, 0;
1, 1, 1, 1, 1;
0, 0, 1, 0, 0;
0, 0, 1, 0, 0]

element = getStructuringElement(MORPH_CROSS, Size(6, 6));

result:
[ 0, 0, 0, 1, 0, 0;
0, 0, 0, 1, 0, 0;
0, 0, 0, 1, 0, 0;
1, 1, 1, 1, 1, 1;
0, 0, 0, 1, 0, 0;
0, 0, 0, 1, 0, 0]

note:1/when the dimension of ‘element’ is odd, the crossing begins at the row/col whose index is (dimension/2 + 1).
3/element = getStructuringElement(MORPH_RECT, Size(5, 5));
result:
[ 1, 1, 1, 1, 1;
1, 1, 1, 1, 1;
1, 1, 1, 1, 1;
1, 1, 1, 1, 1;
1, 1, 1, 1, 1]
note:1/quite obvious, nothing to analyze.

你可能感兴趣的:(opencv)