来一段可以在python中对图像加入椒盐噪声的代码
时间: 2023-02-09 08:50:07 浏览: 185
最小二乘法去除图像椒盐噪声的python代码和实验报告.zip
```python
import numpy as np
import cv2
def salt_pepper_noise(image,prob):
output = np.zeros(image.shape,np.uint8)
thres = 1 - prob
for i in range(image.shape[0]):
for j in range(image.shape[1]):
rdn = np.random.random()
if rdn < prob:
output[i][j] = 0
elif rdn > thres:
output[i][j] = 255
else:
output[i][j] = image[i][j]
return output
image = cv2.imread('image.jpg',0)
noise_img = salt_pepper_noise(image,0.05)
cv2.imshow('noise image', noise_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
代码中的prob表示加入噪声的概率。
阅读全文