如何让构造卷积的核函数kernel

Structuring Element

We manually created a structuring elements in the previous examples with help of Numpy. It is rectangular shape. But in some cases, you may need elliptical/circular shaped kernels. So for this purpose, OpenCV has a function, cv.getStructuringElement(). You just pass the shape and size of the kernel, you get the desired kernel.

在前面的示例中,我们在Numpy的帮助下手动创建了一个结构化元素(structuring element)。它是长方形的。但在某些情况下,你可能需要椭圆形或圆形的核函数。为此,OpenCV有一个函数,即cv.getStructuringElement()。只需传递内核的形状和大小,就可以得到所需的内核。

例子:

# Rectangular Kernel 长方形的核函数

>>> cv.getStructuringElement(cv.MORPH_RECT,(5,5))

array([[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]], dtype=uint8)

# Elliptical Kernel 椭圆形的核函数

>>> cv.getStructuringElement(cv.MORPH_ELLIPSE,(5,5))

array([[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]], dtype=uint8)

# Cross-shaped Kernel 十字形的核函数

>>> cv.getStructuringElement(cv.MORPH_CROSS,(5,5))

array([[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]], dtype=uint8)

来源于:https://docs.opencv.org/4.1.2/d9/d61/tutorial_py_morphological_ops.html

猜你喜欢

转载自blog.csdn.net/weixin_40244676/article/details/104146873
今日推荐