cv2.erode 例举
时间: 2023-07-02 13:11:22 浏览: 49
cv2.erode 是 OpenCV 库中的函数之一,用于腐蚀图像。它可以通过将每个像素的值替换为它周围像素的最小值来实现。以下是使用 cv2.erode 的示例:
```python
import cv2
import numpy as np
# 读取图像
img = cv2.imread('example.jpg', 0)
# 定义一个 5x5 的结构元素
kernel = np.ones((5,5),np.uint8)
# 对图像进行腐蚀
erosion = cv2.erode(img,kernel,iterations = 1)
# 显示原图和腐蚀后的图像
cv2.imshow('Original Image',img)
cv2.imshow('Eroded Image',erosion)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
上述代码中,我们首先读取一张灰度图像,并定义了一个 5x5 的结构元素。然后,我们使用 cv2.erode 函数对图像进行了一次腐蚀操作,并将结果保存在 erosion 变量中。最后,我们使用 cv2.imshow 函数展示了原图和腐蚀后的图像。
相关问题
cv2.erode参数
cv2.erode() 是 OpenCV 库中的函数之一,用于图像的腐蚀操作。它的参数如下:
cv2.erode(src, kernel[, dst[, anchor[, iterations[, borderType[, borderValue]]]]])
其中,
- src:输入图像,可以是灰度图像或彩色图像。
- kernel:腐蚀操作的核,可以是矩形、椭圆、十字形等。
- dst:输出图像,与输入图像大小和类型相同,默认为 None。
- anchor:锚点位置,默认为(-1, -1),表示位于核中心。
- iterations:腐蚀操作的迭代次数,默认为 1。
- borderType:边界扩充类型,默认为 cv2.BORDER_CONSTANT。
- borderValue:边界扩充值,默认为 0。
以上是 cv2.erode() 函数的参数说明。
python opencv cv2.erode
The cv2.erode() function in OpenCV is used to perform erosion on an image. Erosion is a morphological operation that shrinks the boundaries of foreground objects in an image.
Syntax:
cv2.erode(src, kernel, iterations)
Parameters:
- src: Input image
- kernel: Structuring element used for erosion
- iterations: Number of times erosion is applied
Return Value:
The function returns the eroded image.
Example:
```
import cv2
import numpy as np
# Read input image
img = cv2.imread('input.jpg', 0)
# Define kernel
kernel = np.ones((5,5), np.uint8)
# Perform erosion
eroded_img = cv2.erode(img, kernel, iterations=1)
# Display images
cv2.imshow('Input Image', img)
cv2.imshow('Eroded Image', eroded_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
In this example, we perform erosion on the input image using a 5x5 kernel with one iteration. The eroded image is then displayed using OpenCV's imshow() function.