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)
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)
print("-----------helli python--------------")
src = cv.imread(“E:/image/lena.jpg”)
cv.namedWindow(“input image”, cv.WINDOW_AUTOSIZE)
cv.imshow(“input image”, src)
t1 = cv.getTickCount()
gaussian_noise(src) #添加高斯噪声
t2 = cv.getTickCount()