[190216][opencv] getStructuringElement()

本文详细介绍了OpenCV中用于形态学操作的结构元素构造方法,包括椭圆、十字形和矩形三种形状,并通过示例代码展示了不同尺寸结构元素的生成及形状特性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、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 <iostream>

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:
figure1note: 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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值