为什么要将非线性滤波和线性滤波结合起来使用?
因为单独使用非线性滤波的计算量较大(需要对其模板下的所有像素的灰度值排序),结合计算量较小的线性滤波,可达到(接近)期望要求。
结合的步骤(以均值滤波和中值滤波结合为例)
- 对较大区域进行线性滤波
- 计算线性滤波输出的中值作为混合滤波的结果
以1-D信号混合为例
是一个 1-D 信号,用子结构
、
、
、......、
组成的线性中值混合滤波可定义为:
对一副图像进行线性中值混合滤波
模板:
对圈起来的像素分别求其灰度值的均值,再取四个均值和中心像素灰度值(共五个元素)的中值为滤波结果图像的模板中心像素的灰度值。
代码实现:
import cv2
import numpy as np
import matplotlib.pyplot as plt
from time import time
from math import ceil
def nonlin(image):
"""
非线性中值滤波函数
:param image:输入图像
:return:滤波结果图像
"""
# 扩充边界
border_filling = cv2.copyMakeBorder(image, 2, 2, 2, 2, cv2.BORDER_REPLICATE)
image_lin_and_nolin = image.copy()
# 漫游重合
for i in range(2, border_filling.shape[0] - 2):
for j in range(2, border_filling.shape[1] - 2):
# 读取需要的像素点的灰度值
all_of_list = []
for s in range(i-2, i+3):
for t in range(j-2, j+3):
all_of_list.append(border_filling[s][t])
# 对灰度值列表排序并取中值
all_of_list.sort()
image_lin_and_nolin[i - 2][j - 2] = all_of_list[12]
return image_lin_and_nolin
def lin_and_nolin(image):
"""
线性平均滤波和非线性中值滤波相结合的函数
:param image: 输入图像
:return: 滤波结果
"""
# 扩充边界
border_filling = cv2.copyMakeBorder(image, 2, 2, 2, 2, cv2.BORDER_REPLICATE)
image_lin_and_nolin = image.copy()
# 漫游重合
for i in range(2, border_filling.shape[0]-2):
for j in range(2, border_filling.shape[1]-2):
# 读取需要的像素点的灰度值
part_of_list = [int((int(border_filling[i-1][j-1]) + int(border_filling[i-2][j-2])) / 2),
int((int(border_filling[i+1][j+1]) + int(border_filling[i+2][j+2])) / 2),
int((int(border_filling[i+1][j-1]) + int(border_filling[i+2][j-2])) / 2),
int((int(border_filling[i-1][j+1]) + int(border_filling[i-2][j-2])) / 2)]
# 对所有组成区域的像素点求平均值
#linear_result = [int(np.mean(part_of_list[0:5])), int(np.mean(part_of_list[5:10])),
# int(np.mean(part_of_list[10:15])), int(np.mean(part_of_list[15:])),
# border_filling[i][j]]
"""
刚开始用的是np.mean函数来对各个区域像素求均值,但是发现结果和理论不符,
改进之后不使用该函数发现结果快了很多
"""
# 对平均值结果排序并取中值
part_of_list.sort()
image_lin_and_nolin[i-2][j-2] = part_of_list[2]
return image_lin_and_nolin
# 加椒盐噪声的函数
def saltPepper(image, salt, pepper):
height = image.shape[0]
width = image.shape[1]
pertotal = salt + pepper #总噪声占比
noiseImage = image.copy()
noiseNum = int(pertotal * height * width)
for i in range(noiseNum):
rows = np.random.randint(0, height-1)
cols = np.random.randint(0,width-1)
if(np.random.randint(0,100)<salt*100):
noiseImage[rows,cols] = 255
else:
noiseImage[rows,cols] = 0
return noiseImage
def show_images(images):
'''
显示函数
:param images:
:return:
'''
for i in range(len(images)):
img = images[i]
#行,列,索引
x = 1
plt.subplot(x, ceil(len(images)/x), i+1)
plt.imshow(img, cmap="gray")
title = "("+str(i+1)+")"
plt.title(title,fontsize=10)
plt.xticks([])
plt.yticks([])
plt.show()
if __name__ == "__main__":
image = cv2.imread("images\\flower8.jpg", 0)
imageNoise = saltPepper(image, 0.075, 0.075)
begin1 = time()
for i in range(10):
image_nolin = nonlin(imageNoise)
end1 = time()
begin2 = time()
for i in range(10):
image_lin_nolin = lin_and_nolin(imageNoise)
end2 = time()
time1 = end1 - begin1
time2 = end2 - begin2
print("使用5*5模板时:")
print("仅使用非线性中值滤波处理一张图像10次的时间为:", time1)
print("使用线性均值和非线性中值混合滤波处理一张图像10次的时间为:", time2)
images = [image, imageNoise, image_nolin, image_lin_nolin]
show_images(images)
控制台结果:
图像输出:
(1)原始图像
(2)加了椒盐噪声的图像
(3)仅使用非线性中值滤波5×5模板处理的结果图像
(4)使用线性中值混合滤波5×5模板滤波处理的结果图像
放大图:
结论:
就去噪效果而言,单独使用非线性滤波确实比线性非线性混合滤波好一些,但是就其速度而言,相对于单独使用非线性滤波是快了很多的,应用中可根据实际需求来择优使用。