python中实现:(a) Load the image 'mandrill.jpg'. (b) Add salt&Pepper noise with different intensities (at least 3 kinds). (Tips: skimage.util.random_noise) (c) Perform smoothing linear filtering to smooth these noised images respectively. (Tips: ImageFilter.BLUR) (d) Display the images in the same figure with sub-figures. Add the corresponding title to each sub-figure.
时间: 2023-05-18 11:04:50 浏览: 140
和椒盐噪声到图像中。 (c) 使用中值滤波器去除噪声。
以下是实现该功能的Python代码:
```python
import cv2
import numpy as np
# Load the image
img = cv2.imread('mandrill.jpg')
# Add salt and pepper noise
noise = np.zeros(img.shape, np.uint8)
cv2.randu(noise, 0, 255)
salt = noise > 245
pepper = noise < 10
img[salt] = 255
img[pepper] = 0
# Apply median filter to remove noise
filtered_img = cv2.medianBlur(img, 5)
# Display the original and filtered images
cv2.imshow('Original Image', img)
cv2.imshow('Filtered Image', filtered_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
这段代码首先使用`cv2.imread()`函数加载了名为`mandrill.jpg`的图像。然后,使用`cv2.randu()`函数生成了与原始图像大小相同的随机噪声,并将其转换为二值图像,其中亮像素表示盐噪声,暗像素表示椒噪声。接下来,将盐噪声像素设置为255(白色),将椒噪声像素设置为0(黑色),从而将噪声添加到原始图像中。最后,使用`cv2.medianBlur()`函数对图像进行中值滤波,以去除噪声。最终,使用`cv2.imshow()`函数显示原始图像和过滤后的图像。
阅读全文