整理了一下常见的滤波方法,效果图就不放了。
import cv2 as cv
import numpy as np
def rgb2gray(img):
h=img.shape[0]
w=img.shape[1]
img1=np.zeros((h,w),np.uint8)
for i in range(h):
for j in range(w):
img1[i,j]=0.144*img[i,j,0]+0.587*img[i,j,1]+0.299*img[i,j,2]
return img1
def noise(img,snr):
h=img.shape[0]
w=img.shape[1]
img1=img.copy()
sp=h*w # 计算图像像素点个数
NP=int(sp*(1-snr)) # 计算图像椒盐噪声点个数
for i in range (NP):
randx=np.random.randint(1,h-1) # 生成一个 1 至 h-1 之间的随机整数
randy=np.random.randint(1,w-1) # 生成一个 1 至 w-1 之间的随机整数
if np.random.random()<=0.5: # np.random.random()生成一个 0 至 1 之间的浮点数
img1[randx,randy]=0
else:
img1[randx,randy]=255
return img1
image=cv.imread("C:\\Users\\Kris\\Desktop\\lena.jpg")
grayimage=rgb2gray(image)
jiaoyannoiseimage=noise(grayimage,0.6) # 将信噪比设定为0.6
cv.imshow("image",image)
cv.imshow("grayimage",grayimage)
cv.imshow("jiaoyannoiseimage",jiaoyannoiseimage)
image =cv.imwrite('C:\\Users\\Kris\\Desktop\\quzao\\image.png',image,[int( cv.IMWRITE_JPEG_QUALITY), 95])
grayimage =cv.imwrite('C:\\Users\\Kris\\Desktop\\quzao\\grayimage.png',grayimage,[int( cv.IMWRITE_JPEG_QUALITY), 95])
jiaoyannoiseimage =cv.imwrite('C:\\Users\\Kris\\Desktop\\quzao\\jiaoyannoiseimage.png',jiaoyannoiseimage,[int( cv.IMWRITE_JPEG_QUALITY), 95])
cv.waitKey(0)
cv.destroyAllWindows()
import cv2 as cv
import numpy as np
def clamp(pv):
if pv > 255:
return 255
elif pv < 0:
return 0
else:
return pv
#给图片增加高斯噪声,计算花费很长时间
def gaussian_noise(src):
image = src
h, w, c = image.shape
for row in range(h):
for col in range(w):
#获取三个高斯随机数
#第一个参数:概率分布的均值,对应着整个分布的中心
#第二个参数:概率分布的标准差,对应于分布的宽度
#第三个参数:生成高斯随机数数量
s = np.random.normal(0, 20, 3)
#获取每个像素点的bgr值
b = image[row, col, 0] #blue
g = image[row, col, 1] #green
r = image[row, col, 2] #red\
#给每个像素值设置新的bgr值
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.namedWindow('Gaussian_noise', 0)
cv.resizeWindow('Gaussian_noise', 600, 600)
cv.imshow('Gaussian_noise', image)
def gaussian_blur(src): # 定义高斯模糊
image = src
dst = cv.GaussianBlur(image, (0, 0), 15)
cv.namedWindow('Gaussian_blur', 0)
cv.resizeWindow('Gaussian_blur', 600, 600)
cv.imshow('Gaussian_blur', dst)
src = cv.imread('C:\\Users\\Kris\\Desktop\\lena.jpg')
cv.namedWindow('inputimage', 0)
cv.resizeWindow('inputimage', 600, 600)
cv.imshow('inputimage', src)
#gaussiannoise = gaussian_noise(src) # 调用上述定义的高斯噪声
# gaussian_blur = gaussian_blur(src) # 调用高斯模糊函数
inputimage =cv.imwrite('C:\\Users\\Kris\\Desktop\\quzao\\inputimage.png',src,[int(cv.IMWRITE_JPEG_QUALITY), 95])
Gaussian_noise =cv.imwrite('C:\\Users\\Kris\\Desktop\\quzao\\Gaussian_noise.png',src,[int(cv.IMWRITE_JPEG_QUALITY), 95])
#Gaussian_blur =cv.imwrite('C:\\Users\\Kris\\Desktop\\quzao\\Gaussian_blur.png',src,[int(cv.IMWRITE_JPEG_QUALITY), 95])
cv.waitKey(0)
cv.destroyAllWindows()
import cv2
o = cv2.imread("C:\\Users\\Kris\\Desktop\\quzao\\jiaoyannoiseimage.png") # 读取噪声图像
r5 = cv2.blur(o,(5,5))
r30 = cv2.blur(o,(30,30))
cv2.imshow("original",o)
cv2.imshow("result5",r5)
cv2.imshow("result30",r30)
r5 = cv2.imwrite('C:\\Users\\Kris\\Desktop\\quzao\\jiaoyanjunzhi5.png',r5,[int(cv2.IMWRITE_JPEG_QUALITY), 95])
r30 = cv2.imwrite('C:\\Users\\Kris\\Desktop\\quzao\\jiaoyanjunzhi30.png',r30,[int(cv2.IMWRITE_JPEG_QUALITY), 95])
cv2.waitKey(0)
cv2.destroyALLWindows()
import cv2
o = cv2.imread("C:\\Users\\Kris\\Desktop\\quzao\\Gaussian_noise.png") # 读取噪声图像
# r = cv2.boxFilter(o,-1,(5,5)) # normalize=1进行归一化,相当于均值滤波
r = cv2.boxFilter(o,-1,(5,5),normalize=0) # normalize=0不进行归一化
cv2.imshow("original",o)
cv2.imshow("result",r)
r = cv2.imwrite('C:\\Users\\Kris\\Desktop\\quzao\\gaosibox0.png',r,[int(cv2.IMWRITE_JPEG_QUALITY), 95])
cv2.waitKey(0)
cv2.destroyALLWindows()
import cv2
o = cv2.imread("C:\\Users\\Kris\\Desktop\\quzao\\jiaoyannoiseimage.png") # 读取噪声图像
r = cv2.medianBlur(o,7)
cv2.imshow("original",o)
cv2.imshow("result",r)
r = cv2.imwrite('C:\\Users\\Kris\\Desktop\\quzao\\jiaoyanmedianBlur7.png',r,[int(cv2.IMWRITE_JPEG_QUALITY), 95])
cv2.waitKey(0)
cv2.destroyALLWindows()
import cv2
o = cv2.imread("C:\\Users\\Kris\\Desktop\\quzao\\jiaoyannoiseimage.png") # 读取噪声图像
r5 = cv2.GaussianBlur(o,(5,5),0,0)
cv2.imshow("original",o)
cv2.imshow("result",r5)
#r = cv2.imwrite('C:\\Users\\Kris\\Desktop\\quzao\\jiaoyanmedianBlur7.png',r5,[int(cv2.IMWRITE_JPEG_QUALITY), 95])
cv2.waitKey(0)
cv2.destroyALLWindows()
import cv2
o = cv2.imread("C:\\Users\\Kris\\Desktop\\quzao\\jiaoyannoiseimage.png") # 读取噪声图像
r5 = cv2.bilateralFilter(o,25,200,200)
cv2.imshow("original",o)
cv2.imshow("result",r5)
r = cv2.imwrite('C:\\Users\\Kris\\Desktop\\quzao\\jiaoyanbilateralFilter200.png',r5,[int(cv2.IMWRITE_JPEG_QUALITY), 95])
cv2.waitKey(0)
cv2.destroyALLWindows()