import numpy as np
import random
import cv2
def sp_noise(image,prob):
'''
Add salt and pepper noise to image
prob: Probability of the noise
'''
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 = 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) # Only for grayscale image
noise_img = sp_noise(image,0.05)
cv2.imwrite('sp_noise.jpg', noise_img)
我不知道PythonAPI中有什么方法,但是你可以用这个简单的代码给图像添加椒盐噪声。
只要看看cv2.randu()或cv.randn(),我想它已经和matlab非常相似了。
让我们玩一会儿;):
要将其应用于现有图像,只需在所需范围内生成噪波,然后添加:
相关问题 更多 >
编程相关推荐