对原始图像增加椒盐噪声,并进行最大值和最小值滤波的python代码
时间: 2024-05-05 07:18:40 浏览: 130
图像椒盐噪声滤波代码
5星 · 资源好评率100%
以下是对原始图像加椒盐噪声并进行最大值和最小值滤波的 Python 代码:
```python
import cv2
import numpy as np
# Load image
img = cv2.imread('image.jpg')
# Add salt and pepper noise
noise = np.zeros(img.shape, np.uint8)
cv2.randu(noise, 0, 255)
black = noise < 30
white = noise > 225
img[black] = 0
img[white] = 255
# Apply max filter
kernel_size = 5
max_img = cv2.dilate(img, np.ones((kernel_size, kernel_size), np.uint8))
# Apply min filter
min_img = cv2.erode(img, np.ones((kernel_size, kernel_size), np.uint8))
# Show results
cv2.imshow('Original', img)
cv2.imshow('Max filter', max_img)
cv2.imshow('Min filter', min_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
请问还有其他问题吗?
阅读全文