图像处理--从频率角度分析中值滤波
1.均值滤波
对于中值滤波器,就是设定一定大小的核,计算核包含的像素点对应的中值。
m
e
d
i
a
n
[
a
11
a
12
⋯
a
1
n
a
21
a
22
⋯
a
2
n
⋮
⋮
⋱
⋮
a
m
1
a
m
2
⋯
a
m
n
]
median{\begin{bmatrix} {a_{11}}&{a_{12}}&{\cdots}&{a_{1n}}\\ {a_{21}}&{a_{22}}&{\cdots}&{a_{2n}}\\ {\vdots}&{\vdots}&{\ddots}&{\vdots}\\ {a_{m1}}&{a_{m2}}&{\cdots}&{a_{mn}}\\ \end{bmatrix} }
median⎣⎢⎢⎢⎡a11a21⋮am1a12a22⋮am2⋯⋯⋱⋯a1na2n⋮amn⎦⎥⎥⎥⎤
那么对应的中值滤波核如下所示:
F
i
l
t
e
r
=
m
e
d
i
a
n
×
A
Filter=median\times A
Filter=median×A
2.代码
import numpy as np
from skimage import io
import cv2
from matplotlib import pyplot as plt
path = "D:/2_project/0_test/median_filter/anr/input/inputfull.jpg"
I = io.imread(path) #R*0.299+G*0.587+B*0.114
def medianfilter(image, winsize):
rows, cols, channel = image.shape
winH, winW = winsize
halfwinH = (winH-1)//2 +1
halfwinW = (winW-1)//2 +1
medianfilterimage = np.zeros(image.shape, image.dtype)
for k in range(channel):
for i in range(rows):
for j in range(cols):
rtop = 0 if i-halfwinH < 0 else i - halfwinH
rbootom = rows-1 if i + halfwinH > rows -1 else i + halfwinH
cleft = 0 if j - halfwinW < 0 else j - halfwinW
cright = cols-1 if j + halfwinW > cols -1 else j + halfwinW
region = image[rtop:rbootom+1, cleft:cright+1, k]
medianfilterimage[i][j][k] = np.median(region)
return medianfilterimage
window = (9, 9)
output = medianfilter(I, window)
3.Kernel 大小分析
不同大小的核对图像进行滤波得到的图像信息不同,随着核的大小的增大,计算的像素点越多,也就意味着滤波后的图像包含了更多的低频信息,这样,随着核大小的增大,高频信息丢失。
对不同大小的图像进行中值滤波,图2是图1缩放四倍,图3是图1缩放16倍。
以下是不同大小的核(包括size=3和size=9)中值滤波后的图像:
4.对比均值滤波和中值滤波