资源简介
https://blog.csdn.net/weixin_40267472/article/details/82773408
代码片段和文件信息
“““
Created on Mon May 21 10:24:12 2018
@author: XieMN
“““
from PIL import Image
import matplotlib.pyplot as plt
import math
import copy
%matplotlib inline
class BilateralFilter(object):
“““
定义双边滤波器类:通过空间域和值域模板进行平滑
变量:
ds:distance sigma
rs: range sigma
c_weight_table:空间域权重模板
s_weight_table: 值域权重模板
radius: 平滑模板的半径,即平滑模板大小为(2*radius+1)
“““
def __init__(selfds rs radiu):
“““初始化变量“““
self.ds = ds
self.rs = rs
self.c_weight_table = []
self.s_weight_table = []
self.radius = radiu
def build_distance_weight_table(self):
“““定义空间域模板“““
# size = 2 * self.radius + 1
for semi_row in range(-self.radiusself.radius+1):
self.c_weight_table.append([])
for semi_col in range(-self.radiusself.radius+1):
# calculate Euclidean distance between center point and close pixels
delta = -(semi_row * semi_row + semi_col * semi_col)/(2*(self.ds**2))
self.c_weight_table[semi_row+self.radius].append(
math.exp(delta))
def build_similarity_weight_table(self):
“““定义值域模板“““
for i in range(256): # since the color scope is 0 ~ 255
delta = -(i * i) / (2*(self.rs**2))
self.s_weight_table.append(math.exp(delta ))
def clamp(selfp):
“““return RGB color between 0 and 255“““
if p < 0:
return 0
elif p > 255:
return 255
else:
return p
def bilateral_filter(self src):
“““
双边滤波器的方法:对原始图进行双边滤波器平滑,并返回平滑过后的图
“““
height = src.size[0]
width = src.size[1]
radius = self.radius
self.build_distance_weight_table()
self.build_similarity_weight_table()
in_pixels = src.load()
raw_data=[]
out_data=copy.deepcopy(src)
# out_pixels = {}
red_sum = green_sum = blue_sum = 0 # result of convolution before normalization
cs_sum_red_weight = cs_sum_green_weight = cs_sum_blue_weight = 0 # normalization
#对于边缘像素采用不平滑的方法
for row in range(radiusheight-radius):
for col in range(radiuswidth-radius):
# 对每个像素进行平滑
tr = in_pixels[row col][0]
tg = in_pixels[row col][1]
tb = in_pixels[row col][2]
raw_data.append((tr tg tb))
for semi_row in range(-radius radius+1):
for semi_col in range(-radius radius+1):
# 获得模板内的像素
row_offset = row + semi_row
col_offset = col + semi_col
tr2 = in_pixels[row_offset col_offset][0]
tg2 = in_pixels[row_offset col_offset][1]
tb2 = in_pixels[row
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 6194 2018-05-23 13:21 XieMN_bilateral_filter\bilateral_filter.py
文件 37196 2018-05-25 21:46 XieMN_bilateral_filter\lena.jpg
文件 81147 2018-05-25 21:49 XieMN_bilateral_filter\lena_10.jpg
文件 112587 2018-05-25 21:50 XieMN_bilateral_filter\lena_20.jpg
文件 4511150 2018-05-25 23:25 XieMN_bilateral_filter\XieMN_bilateral_filter.html
目录 0 2018-05-25 23:26 XieMN_bilateral_filter\实验结果\
文件 855407 2018-05-25 23:05 XieMN_bilateral_filter\实验结果\1.png
文件 782218 2018-05-25 23:10 XieMN_bilateral_filter\实验结果\2_1.png
文件 782448 2018-05-25 23:22 XieMN_bilateral_filter\实验结果\2_2.png
文件 880623 2018-05-25 23:11 XieMN_bilateral_filter\实验结果\3.png
文件 762398 2018-05-25 23:11 XieMN_bilateral_filter\实验结果\4.png
文件 36153 2018-05-25 23:17 XieMN_bilateral_filter\实验结果\lena_10_m_11_s_10_c_25.jpg
文件 36823 2018-05-25 23:20 XieMN_bilateral_filter\实验结果\lena_10_m_13_s_10_c_25.jpg
文件 44463 2018-05-25 22:56 XieMN_bilateral_filter\实验结果\lena_10_m_3_s_1_c_25.jpg
文件 47573 2018-05-25 22:58 XieMN_bilateral_filter\实验结果\lena_10_m_3_s_10_c_10.jpg
文件 36425 2018-05-25 23:00 XieMN_bilateral_filter\实验结果\lena_10_m_3_s_10_c_25.jpg
文件 25846 2018-05-25 22:59 XieMN_bilateral_filter\实验结果\lena_10_m_3_s_150_c_150.jpg
文件 71594 2018-05-25 22:57 XieMN_bilateral_filter\实验结果\lena_10_m_3_s_25_c_1.jpg
文件 36392 2018-05-25 22:57 XieMN_bilateral_filter\实验结果\lena_10_m_3_s_25_c_25.jpg
文件 29359 2018-05-25 22:59 XieMN_bilateral_filter\实验结果\lena_10_m_3_s_60_c_60.jpg
文件 36153 2018-05-25 23:06 XieMN_bilateral_filter\实验结果\lena_10_m_5_s_10_c_25.jpg
文件 33368 2018-05-25 23:08 XieMN_bilateral_filter\实验结果\lena_10_m_5_s_10_c_50.jpg
文件 36823 2018-05-25 23:05 XieMN_bilateral_filter\实验结果\lena_10_m_7_s_10_c_25.jpg
文件 36425 2018-05-25 23:16 XieMN_bilateral_filter\实验结果\lena_10_m_9_s_10_c_25.jpg
文件 3809357 2018-05-25 23:27 XieMN_bilateral_filter\谢梦娜_双边滤波器实验报告.docx
评论
共有 条评论