關于數字影像處理中的幾種濾波演算法
// An highlighted block
# -*- coding: utf-8 -*-
"""
Created on 2021/3/24 18:40
# @Author : desires19
"""
import numpy as np
import matplotlib.pyplot as plt
import sys
from PIL import Image
def loading_image(path):
image = Image.open(path)
image = np.array(image)
r = image[:, :, 0]
g = image[:, :, 1]
b = image[:, :, 2]
# print("loading image finish!")
return r, g, b
'''圖片填充'''
def padding(c):
size = c.shape
re_size = (size[0] + 2, size[1] + 2)
row = 0 # 行
col = 0 # 列
l = [[0 for i in range(re_size[1])] for i in range(re_size[0])]
while col < re_size[0]: # 列 801
while row < re_size[1]: # 行 642
if col == 0 or row == 0 or col == (re_size[0] - 1) or row == (re_size[1] - 1):
l[col][row] = 0
else:
l[col][row] = c[col - 1][row - 1]
row += 1
col += 1
row = 0
re = np.asarray(l)
re = re.reshape(re_size)
# print("padding finish!")
return re
def Fuzzy(c, filter):
f_size = filter.shape
c_size = c.shape
# 定義卷積上下左右邊界
left = 0
right = f_size[0] - 1
top = 0
bottom = f_size[1] - 1
l = []
r = 0
f_row = 0 # 濾波器的行
f_col = 0 # 濾波器的列
while bottom < c_size[0]:
while right < int(c_size[1]):
'''大回圈'''
while f_col < f_size[0]:
while f_row < f_size[1]:
'''小回圈'''
r = r + filter[f_col][f_row] * c[top + f_col][left + f_row]
if r < 0:
r = 0
if r > 255:
r = 255
# print(r)
f_row += 1
f_col += 1
f_row = 0
l.append(int(r + 0.5))
r = 0
right += 1
left += 1
f_row = 0 # 重新歸零進行下一次卷積運算
f_col = 0
bottom += 1
top += 1
right = f_size[0] - 1
left = 0
re = np.asarray(l)
re = re.reshape(c_size[0] - 2, c_size[1] - 2)
return re
def Mid(c, n):
c_size = c.shape
# 定義卷積上下左右邊界
left = 0
right = n - 1
top = 0
bottom = n - 1
l = []
r = []
f_row = 0 # 濾波器的行
f_col = 0 # 濾波器的列
while bottom < c_size[0]:
while right < c_size[1]:
'''大回圈'''
while f_col < n:
while f_row < n:
'''小回圈'''
r.append(c[top + f_col][left + f_row])
# print(r)
f_row += 1
f_col += 1
f_row = 0
r.sort()
l.append(r[int((pow(n, 2) / 2))])
r = []
right += 1
left += 1
f_row = 0 # 重新歸零進行下一次卷積運算
f_col = 0
bottom += 1
top += 1
right = n - 1
left = 0
re = np.asarray(l)
re = re.reshape(c_size[0] - 2, c_size[1] - 2)
return re
'''均衡化'''
def Equalization(p):
count = 0
x = []
y = []
while count < 256:
x.append(count)
y.append(0)
count += 1
ls = [x, y]
# print("init finish!")
y_1 = ls[1]
size = p.shape
row = 0
col = 0
count = 0
re = []
while col < size[0]:
while row < size[1]:
y_1[p[col][row]] += 1
row += 1
col += 1
row = 0
x = np.array(ls[0])
y_0 = np.array(ls[1])
y = y_0 / (size[0] * size[1]) # 歸一化處理
'''計算累計直方圖'''
num = 0
dy = [] # 累計直方陣列
for n in y:
num = num + n
dy.append(num)
'''取整處理'''
sk = [] # 轉化后
for n in dy:
num_1 = int(255 * n + 0.5) # 四舍五入
sk.append(num_1)
'''處理映射關系'''
col = 0 # 歸零處理,重新回圈,覆寫影像
row = 0
while col < size[0]: # 列
while row < size[1]: # 行
num_2 = p[col][row]
re.append(sk[num_2]) # 進行映射
row += 1
col += 1
row = 0
im_re = np.array(re)
im_re = im_re.reshape(size)
return im_re
def image_show(r, g, b, image_name):
new_image = np.stack((r, g, b), axis=2)
# print(new_image.shape)
plt.rcParams['savefig.dip'] = 400 # 圖片像素
plt.rcParams['figure.dip'] = 300 # 解析度
plt.imshow(new_image) # 顯示圖片
plt.axis('off') # 不顯示坐標軸
plt.savefig(image_name, bbox_inches='tight')
def switch(state):
if state == 1:
'''高斯濾波器'''
Gauss = np.asarray([[1, 2, 1], [2, 4, 2], [1, 2, 1]]) / 16
g_r = Fuzzy(p_r, Gauss)
g_g = Fuzzy(p_g, Gauss)
g_b = Fuzzy(p_g, Gauss)
image_show(g_r, g_g, g_b, 'E:\Documents\python_work\dpi\output\\gauss_1.jpg')
elif state == 2:
'''均值濾波器'''
Average = np.asarray([[1, 1, 1], [1, 1, 1], [1, 1, 1]]) / 9
a_r = Fuzzy(p_r, Average)
a_g = Fuzzy(p_g, Average)
a_b = Fuzzy(p_g, Average)
image_show(a_r, a_g, a_b, 'E:\Documents\python_work\dpi\output\\average_2.jpg')
elif state == 3:
'''中值濾波器'''
m_r = Mid(p_r, 3)
m_g = Mid(p_g, 3)
m_b = Mid(p_b, 3)
image_show(m_r, m_g, m_b, 'E:\Documents\python_work\dpi\output\\mid_3.jpg')
elif state == 4:
'''銳化濾波器'''
H_1 = np.asarray([[-1, -1, -1], [-1, 8, -1], [-1, -1, -1]])
s_r = Fuzzy(p_r, H_1)
s_g = Fuzzy(p_g, H_1)
s_b = Fuzzy(p_b, H_1)
image_show(s_r, s_g, s_b, 'E:\Documents\python_work\dpi\output\\strong_4.jpg')
elif state == 0:
'''均衡化處理'''
e_r = Equalization(p_r)
e_g = Equalization(p_g)
e_b = Equalization(p_b)
image_show(e_r, e_g, e_b, 'E:\Documents\python_work\dpi\output\\transform_0.jpg')
elif state == 5:
sys.exit()
def init():
print("0為均衡化處理")
print("1為高斯濾波")
print("2為均值濾波")
print("3為中值濾波")
print("4為銳化濾波")
print("5為退出程式")
key = input("請輸入您想要進行的操作 ")
return key
if __name__ == '__main__':
im_path = './example.jpg'
r, g, b = loading_image(im_path)
p_r = padding(r)
p_g = padding(g)
p_b = padding(b)
'''
0為均衡化處理
1為高斯濾波
2為均值濾波
3為中值濾波
4為銳化濾波
'''
while(1):
k = eval(init())
switch(k)
/
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/273729.html
標籤:python
