Opencv Python 简明教程
OpenCV Python - Morphological Transformations
基于形状图像上的简单操作称为形态变换。最常见的两种转换是 erosion and dilation 。
Simple operations on an image based on its shape are termed as morphological transformations. The two most common transformations are erosion and dilation.
Erosion
腐蚀会消除前景对象的边界。类似于 2D 卷积,内核滑过图像 A。如果内核下的所有像素均为 1,则保留原始图像中的像素。
Erosion gets rid of the boundaries of the foreground object. Similar to 2D convolution, a kernel is slide across the image A. The pixel in the original image is retained, if all the pixels under the kernel are 1.
否则将其变为 0,从而导致腐蚀。丢弃所有边界附近的像素。此过程对去除白噪声很有用。
Otherwise it is made 0 and thus, it causes erosion. All the pixels near the boundary are discarded. This process is useful for removing white noises.
OpenCV 中 erode() 函数的命令如下 −
The command for the erode() function in OpenCV is as follows −
cv.erode(src, kernel, dst, anchor, iterations)
Parameters
OpenCV 中的 erode() 函数使用以下参数 −
The erode() function in OpenCV uses following parameters −
src 和 dst 参数是大小相同的输入和输出图像数组。Kernel 是用于腐蚀的结构化元素矩阵。例如,3X3 或 5X5。
The src and dst parameters are input and output image arrays of the same size. Kernel is a matrix of structuring elements used for erosion. For example, 3X3 or 5X5.
anchor 参数默认为 -1,这意味着锚元素位于中心。Iterations 指腐蚀应用的次数。
The anchor parameter is -1 by default which means the anchor element is at center. Iterations refers to the number of times erosion is applied.
Dilation
它与腐蚀正好相反。此处,如果内核下的至少一个像素为 1,则像素元素为 1。因此,它增加了图像中的白色区域。
It is just the opposite of erosion. Here, a pixel element is 1, if at least one pixel under the kernel is 1. As a result, it increases the white region in the image.
dilate() 函数的命令如下 −
The command for the dilate() function is as follows −
cv.dilate(src, kernel, dst, anchor, iterations)
Parameters
dilate() 函数具有与 erode() 函数相同参数。这两个函数可以有 BorderType 和 borderValue 这两个其他可选参数。
The dilate() function has the same parameters such as that of erode() function. Both functions can have additional optional parameters as BorderType and borderValue.
BorderType 是图像边界的一种枚举类型(CONSTANT、REPLICATE、TRANSPERANT 等)
BorderType is an enumerated type of image boundaries (CONSTANT, REPLICATE, TRANSPERANT etc.)
borderValue 用于恒定边界的情况。默认情况下,它为 0。
borderValue is used in case of a constant border. By default, it is 0.
Example
下面给出了一个示例程序显示 erode() 和 dilate() 函数的使用 −
Given below is an example program showing erode() and dilate() functions in use −
import cv2 as cv
import numpy as np
img = cv.imread('LinuxLogo.jpg',0)
kernel = np.ones((5,5),np.uint8)
erosion = cv.erode(img,kernel,iterations = 1)
dilation = cv.dilate(img,kernel,iterations = 1)
cv.imshow('Original', img)
cv.imshow('Erosion', erosion)
cv.imshow('Dialation', dilation)