1 #!/user/bin/env python3 2 # -*- coding: utf-8 -*- 3 4 import math 5 import numpy as np 6 from PIL import Image 7 import matplotlib.pyplot as plt 8 import cv2 9 10 class MyGaussianBlur(): 11 12 def __init__(self, radius=None, sigema=None,channel = None): 13 self.radius = radius 14 self.sigema = sigema 15 self.channel = channel 16 17 # 高斯的计算公式 18 def calc(self, x, y): 19 res1 = 1 / (2 * math.pi * self.sigema * self.sigema) 20 res2 = math.exp(-(x * x + y * y) / (2 * self.sigema * self.sigema)) 21 return res1 * res2 22 23 # 得到滤波template(时域模板) 24 def template(self): 25 sideLength = self.radius * 2 + 1 26 ch = self.channel 27 result = np.zeros((sideLength, sideLength,ch)) 28 #3重循环对3通道彩色图像进行高斯模板函数 29 for k in range(ch): 30 for i in range(sideLength): 31 for j in range(sideLength): 32 result[i, j] = self.calc(i - self.radius, j - self.radius) 33 all0 = result[:, :, 0].sum() 34 all1 = result[:, :, 1].sum() 35 all2 = result[:, :, 2].sum() 36 return result / all0 37 38 # 滤波函数 39 def filter(self, image, template): 40 arr = np.array(image) # 因为Image没有shape,必须要转化为numpy形式 41 height = arr.shape[0] 42 width = arr.shape[1] 43 c = arr.shape[2] 44 r = arr[:,:,0] 45 g = arr[:,:,1] 46 b = arr[:,:,2] 47 newData = np.zeros((height, width, c)) 48 49 #3重循环计算3通道的高斯模糊处理后的像素值 50 for k in range(3): 51 for i in range(self.radius, height - self.radius): 52 for j in range(self.radius, width - self.radius): 53 t0 = r[i - self.radius:i+self.radius+1,j -self.radius:j+self.radius+1] 54 #a[:,:,k] = np.multiply(t[:,:,k], template[:,:,k]) 55 56 a0 = np.multiply(t0, template[:,:,0]) 57 newData[i,j,0] = a0.sum() 58 59 t1 = g[i - self.radius:i+self.radius+1, j-self.radius:j+self.radius+1] 60 a1 = np.multiply(t1, template[:,:,1]) 61 newData[i, j, 1] = a1.sum() 62 63 t2 = b[i - self.radius:i+self.radius+1, j-self.radius:j+self.radius+1] 64 a2 = np.multiply(t2, template[:,:,2]) 65 66 newData[i, j, 2] = a2.sum() 67 68 newImage = Image.fromarray(np.uint8(newData)) 69 return newImage 70 71 def gaussian_noise(self, image, mean=0, var=40): 72 image = np.array(image) 73 noise = np.random.normal(mean, var, image.shape) 74 out = np.zeros(image.shape) 75 out[:, :, 0] = image[:, :, 0] + noise[:, :, 0] 76 out[:, :, 1] = image[:, :, 1] + noise[:, :, 1] 77 out[:, :, 2] = image[:, :, 2] + noise[:, :, 2] 78 return out / 255 79 80 r = 3 # 模版半径,自由调整 81 s = 2 # sigema数值,自由调整 82 ch = 3 83 GBlur = MyGaussianBlur(radius=r, sigema=s,channel = ch) # 声明高斯模糊类 84 temp = GBlur.template() 85 86 im = Image.open("F:\lena.jpg") 87 #im = im.conver# t('P') #将rgb_3转化为gray_1 88 89 image = GBlur.filter(im, temp) # 高斯模糊滤波,得到新的图片 90 image_noise = GBlur.gaussian_noise(image) 91 92 image_arr = np.array(image) 93 img_array = np.array(image) 94 print("高斯模糊图像尺寸",img_array.shape) 95 96 plt.figure(1) 97 plt.imshow(image ) 98 plt.title('gaussian blur') 99 plt.figure(2) 100 plt.imshow(im) 101 plt.title('original image') 102 plt.figure(3) 103 plt.imshow(image_noise) 104 plt.title('gaussian_blur and gaussian_noise') 105 plt.show()
输出结果如下: