# [数字图像学笔记] 6.几种常见的空间滤波器（均值滤波器、中值滤波器）

OpenCV与数字图像处理 专栏收录该内容
14 篇文章 0 订阅

# 空间滤波（Spatial Filtering）

I o = I i ⨀ f s I_o = I_i \bigodot f_s

P 1 ⋅ K 1 + P 2 ⋅ K 2 + P 3 ⋅ K 3 + ⋯ + P 9 ⋅ K 9 = P i j ∗ P_1 \cdot K_1 + P_2 \cdot K_2 + P_3 \cdot K_3 + \cdots + P_9 \cdot K_9 = P_{ij}^{*}

I o = ∑ ∑ I i , j ⨀ f s I_o = \sum \sum I_{i,j} \bigodot f_s

# 均值滤波器(Mean Filter)

K = 1 K w × K h ∑ f ( x , y ) ⋅ W ( x , y ) K = \frac{1}{K_w \times K_h} \sum f(x, y) \cdot W(x, y)

def mean_kernel(image):
mean = 9
width, height = image.shape
backup = np.zeros(image.shape, np.uint8)

for i in range(1, width - 1):
for j in range(1, height - 1):
px000 = int(image[i - 1][j - 1])
px001 = int(image[i][j - 1])
px002 = int(image[i + 1][j - 1])

px010 = int(image[i - 1][j])
px011 = int(image[i][j])
px012 = int(image[i + 1][j])

px100 = int(image[i - 1][j + 1])
px101 = int(image[i][j + 1])
px102 = int(image[i + 1][j + 1])

sigma = px000 + px001 + px002 +\
px010 + px011 + px012 +\
px100 + px101 + px102

backup[i][j] = round(sigma / mean)

return backup


# 中值滤波器(Median Filter)

[ 0 , 2 , 3 , 3 , 4 , 6 , 10 , 15 , 97 ] [0, 2, 3, 3, 4, 6,10,15, 97]

def adjustable_median_kernel(image, size, i, j):
if size % 2 == 0:
size = size - 1

mid = int((size - 1) / 2)
numbs_list = []

for subtile_x in range(size):
for subtile_y in range(size):
numbs_list.append(image[i + subtile_x - mid][j + subtile_y - mid])

if len(numbs_list) % 2 == 0:
numbs_list = np.sort(numbs_list)
n1 = numbs_list[int(len(numbs_list) / 2)]
n2 = numbs_list[int(len(numbs_list) / 2 - 1)]

return int((n1 + n2) / 2)
else:
numbs_list = np.sort(numbs_list)
return numbs_list[int(len(numbs_list) / 2)]


• 1
点赞
• 2
评论
• 1
收藏
• 一键三连
• 扫一扫，分享海报

12-02
07-13 4601

09-01 1万+
05-17 3万+
05-24 1万+
10-28 2万+
10-07 1319
05-10
10-17 2万+
11-05 1445
10-25 11万+
07-27 993
04-16 4234
01-02 2867
12-15 1581
05-04 531