概述

利用上周所得到的枚举的划分标准,编写降噪程序

代码示例

部分枚举的情况因为时间原因暂时未处理,用最接近的值代替

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# python3.8
# utf-8
# followed by 20211020 report

import cv2 as cv


class NineGrid:
def __init__(self, a, d, flag):
self.a = a
self.d = d
self.flag = flag


def best_pixel(nine, k):
min_d = 255
min_x = 0
min_y = 0
for i in range(3):
for j in range(3):
if nine.flag[i][j] == k and nine.d[i][j] < min_d:
min_x = i
min_y = j
min_d = nine.d[i][j]
return nine.a[min_x][min_y]


def _1d_2_2d(x):
if x == 0:
return 0, 0
elif x == 1:
return 0, 1
elif x == 2:
return 0, 2
elif x == 3:
return 1, 2
elif x == 4:
return 2, 2
elif x == 5:
return 2, 1
elif x == 6:
return 2, 0
elif x == 7:
return 1, 0


def find_best(channel, x, y):
a = [[0]*3 for i in range(3)]
for i in range(3):
for j in range(3):
a[i][j] = channel[x - 1 + i][y - 1 + j]
d = [[0]*3 for i in range(3)]
for i in range(3):
for j in range(3):
d[i][j] = abs(int(a[i][j]) - int(a[1][1]))
dp = [0] * 8
for i in range(8):
dx, dy = _1d_2_2d(i)
dp[i] = d[dx][dy]
dps = sorted(dp)
max_index = dps.index(max(dps))
flag = [[0]*3 for i in range(3)]
for i in range(3):
for j in range(3):
if d[i][j] >= dps[max_index]:
flag[i][j] = 1
# 中心点暂时不分区
flag[1][1] = 2

# 枚举匹配flag特征
# A:B = 1:7
cnt = 0
for i in range(3):
for j in range(3):
if flag[i][j] == 0:
cnt += 1
min_d = 255
min_x = 0
min_y = 0
if cnt == 0:
for i in range(3):
for j in range(3):
if d[i][j] < min_d:
min_d = d[i][j]
min_x = i
min_y = j
elif cnt == 1:
for i in range(3):
for j in range(3):
if flag[i][j] == 1 and d[i][j] < min_d:
min_d = d[i][j]
min_x = i
min_y = j
elif cnt == 7:
for i in range(3):
for j in range(3):
if flag[i][j] == 0 and d[i][j] < min_d:
min_d = d[i][j]
min_x = i
min_y = j
elif cnt == 2:
for i in range(3):
for j in range(3):
if flag[i][j] == 1 and d[i][j] < min_d:
min_d = d[i][j]
min_x = i
min_y = j
elif cnt == 6:
for i in range(3):
for j in range(3):
if flag[i][j] == 0 and d[i][j] < min_d:
min_d = d[i][j]
min_x = i
min_y = j
elif cnt == 3:
for i in range(3):
for j in range(3):
if flag[i][j] == 1 and d[i][j] < min_d:
min_d = d[i][j]
min_x = i
min_y = j
elif cnt == 5:
for i in range(3):
for j in range(3):
if flag[i][j] == 0 and d[i][j] < min_d:
min_d = d[i][j]
min_x = i
min_y = j
elif cnt == 4:
for i in range(3):
for j in range(3):
if d[i][j] < min_d:
min_d = d[i][j]
min_x = i
min_y = j
return a[min_x][min_y]


def denoise(channel):
row, col = channel.shape
for i in range(1, row - 1):
for j in range(1, col - 1):
channel[i][j] = find_best(channel, i, j)
return channel


def main(addr):
img = cv.imread(addr, 1)
b, g, r = cv.split(img)
b = denoise(b)
g = denoise(g)
r = denoise(r)
img = cv.merge((b, g, r))
cv.imwrite('denoise_' + addr, img)


if __name__ == '__main__':
img_addr = 'noise_img.png'
main(img_addr)

效果

noise_img

denoise_noise_img

在原来已降噪过的图像的基础上,还略有效果