高斯滤波器

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

import cv2

import math

import numpy as np

import matplotlib.pyplot as plt

#高斯平滑滤波器(高斯低通滤波器)与均值滤波器区别不大,只不过是将均值改为正态函数作为权值

img = cv2.imread("E:/test1.pgm",0)

#建立一个代用的空数组(我们用他们来存储处理后的图片)

img_1 = np.zeros((256, 256),np.uint8)

img_2 = np.zeros((256, 256),np.uint8)

img_3 = np.zeros((256, 256),np.uint8)

#建立一个代用的空数组(我们在滤波时使用的是加过一圈黑框的图片)

arr_1 = np.zeros((258, 258),np.uint8)

arr_2 = np.zeros((260, 260),np.uint8)

arr_3 = np.zeros((262, 262),np.uint8)

for x in range(0, 256):

for y in range(0, 256):

arr_1[x + 1, y + 1] = img[x, y]

for x in range(0, 256):

for y in range(0, 256):

arr_2[x + 2, y + 2] = img[x, y]

for x in range(0, 256):

for y in range(0, 256):

arr_3[x + 3, y + 3] = img[x, y]

#在进行滤波处理之前要先产生高斯平滑滤波器

gauss_1 = np.zeros((3, 3))

gauss_2 = np.zeros((5, 5))

gauss_3 = np.zeros((7, 7))

mysum = 0

for x in range(0, 3):

for y in range(0, 3):

gauss_1[x, y] = (1/ (3* np.pi))* math.exp(-1*(math.pow(x - 1.5, 2)+ math.pow(y - 1.5, 2))/ 3)

mysum = mysum + gauss_1[x, y]

for x in range(0, 3):

for y in range(0, 3):

gauss_1[x, y] = gauss_1[x, y]/mysum

mysum = 0

for x in range(0, 5):

for y in range(0, 5):

gauss_2[x, y] = (1/ (3* np.pi))* math.exp(-1*(math.pow(x - 2.5, 2)+ math.pow(y - 2.5, 2))/ 3)

mysum = mysum + gauss_2[x, y]

for x in range(0, 5):

for y in range(0, 5):

gauss_2[x, y] = gauss_2[x, y]/mysum

mysum = 0

for x in range(0, 7):

for y in range(0, 7):

gauss_3[x, y] = (1/ (3* np.pi))* math.exp(-1*(math.pow(x - 3.5, 2)+ math.pow(y - 3.5, 2))/ 3)

mysum = mysum + gauss_3[x, y]

for x in range(0, 7):

for y in range(0, 7):

gauss_3[x, y] = gauss_3[x, y]/mysum

# 高斯平滑滤波部分

part_1 = np.zeros((3, 3),np.int)

for x in range(0, 256):

for y in range(0, 256):

for m in range(0, 3):

for n in range(0, 3):

part_1[m, n] = arr_1[x + m, y + n]

local_1 = np.multiply(part_1, gauss_1)

for m in range(0, 3):

for n in range(0, 3):

img_1[x, y] = img_1[x, y] + local_1[m, n]

part_2 = np.zeros((5, 5),np.int)

for x in range(0, 256):

for y in range(0, 256):

for m in range(0, 5):

for n in range(0, 5):

part_2[m, n] = arr_2[x + m, y + n]

local_2 = np.multiply(part_2, gauss_2)

for m in range(0, 5):

for n in range(0, 5):

img_2[x, y] = img_2[x, y] + local_2[m, n]

part_3 = np.zeros((7, 7),np.int)

for x in range(0, 256):

for y in range(0, 256):

for m in range(0, 7):

for n in range(0, 7):

part_3[m, n] = arr_3[x + m, y + n]

local_3 = np.multiply(part_3, gauss_3)

for m in range(0, 7):

for n in range(0, 7):

img_3[x, y] = img_3[x, y] + local_3[m, n]

相关文档
最新文档