OpenCV-Python之高斯模糊

1.高斯噪声函数

//将范围限制在0~255之间
def thresholdfn(pv):
    if pv > 255:
        pv = 255
    elif pv < 0:
        pv = 0
    else:
        return pv
//定义高斯噪声函数
def gaussian_demo(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]
            g = image[row, col, 1]
            r = image[row, col, 2]
            b = thresholdfn(b + s[0])
            g = thresholdfn(g + s[1])
            r = thresholdfn(r + s[2])
    cv.imshow('gaussian_demo', image)

2.测试程序

image = cv.imread('./data/lena.jpg', 1)
cv.imshow('source image', image)
t1 = cv.getTickCount()
gaussian_demo(image)
t2 = cv.getTickCount()
time = (t2 - t1) / cv.getTickFrequency()
print(time)
dst = cv.GaussianBlur(image, (0, 0), 20)
cv.imshow('GaussianBlur image', dst)
cv.waitKey(0)
cv.destroyAllWindows()

测试结果:
time: 7.665542534869055
OpenCV-Python之高斯模糊
OpenCV-Python之高斯模糊
OpenCV-Python之高斯模糊