原理见:https://blog.csdn.net/qq_33208851/article/details/95349944
产生噪声:
#coding:utf-8
#file: tutorial.py
#@author: young
#@contact: [email protected]
#@time: 2019/12/15 23:42
import cv2 as cv
import numpy as np
def clamp(pv):
#控制大小,防止溢出
if pv > 255:
return 255
if pv < 0:
return 0
else:
return pv
def gaussian_noise(image):
h, w, c = image.shape
#获取长宽深度
#对三通道加上随机值
for row in range(h):
for col in range(w):
s = np.random.normal(0, 20, 3)
#在0—20之间随机生成3个数字
#产生三个数给三通道
b = image[row, col, 0] # blue
g = image[row, col, 1] # green
r = image[row, col, 2] # red
image[row, col, 0] = clamp(b + s[0])
image[row, col, 1] = clamp(g + s[1])
image[row, col, 2] = clamp(r + s[2])
cv.imshow("noise image", image)
src = cv.imread("lena.jpg")
cv.imshow("input images",src)
gaussian_noise(src)
cv.waitKey(0)
cv.destroyAllWindows()
高斯模糊(对噪音有抑制作用)
src = cv.imread("lena.jpg")
cv.imshow("input images",src)
gaussian_noise(src)
dst = cv.GaussianBlur(src,(0,0),15)
cv.imshow("GaussianBlur",dst)
cv.waitKey(0)
cv.destroyAllWindows()