主要功能:
1.随机模糊图像,在图像上添加随机噪声
2.高斯模糊,通常用它来减少图像噪声以及降低细节层次
import cv2 as cv
import numpy as np
#保证所有的像素值在0到255之间
def clamp(pv):
if pv > 255:
return 255
if pv < 0:
return 0
else:
return pv
#图像加上随机噪声
def random_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]
g = image[row,col,1]
r = image[row,col,2]
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('noice image',image)
# 高斯模糊,通常用它来减少图像噪声以及降低细节层次,三个参数,src为图像,(0,0)指的x,y的值,越大越模糊。1代表方差,越大越模糊
# 如果后两个参数都不为0,则后面的起作用
def gaussian_noice(image):
det = cv.GaussianBlur(image, (0, 0), 1)
cv.imshow('Gaussian noice', det)
src = cv.imread('F:001.jpg')
#cv.namedWindow('input_image', cv.WINDOW_AUTOSIZE)
cv.imshow("0", src)
random_noise(src) #图像上加随机噪声
gaussian_noice(src) #高斯模糊
cv.waitKey(0)
cv.destroyAllWindows()
输出结果: