上次的工作

测试效果

原图:

img

噪声图:

img_1

处理图:

new

被处理噪声个数占比60+%

本次的工作

改进find_best

根据一段时间对于图像降噪的经验来看,总觉得上次的处理图怪怪的,虽然确实没有了所谓1:7的噪声区域,从结果来看似乎是正常的。但还是再调试了一下,发现确实有可改进之处

改进后代码

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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# python 3.8
# utf-8
# follow 20211124 report

import cv2 as cv
import numpy as np


# 二维数组映射到一维
def _2d_2_1d(x, y):
if x == 0 and y == 0:
return int(0)
elif x == 0 and y == 1:
return int(1)
elif x == 0 and y == 2:
return int(2)
elif x == 1 and y == 2:
return int(3)
elif x == 2 and y == 2:
return int(4)
elif x == 2 and y == 1:
return int(5)
elif x == 2 and y == 0:
return int(6)
elif x == 1 and y == 0:
return int(7)
elif x == 1 and y == 1:
return int(8)


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 division(_8):
result = [0] * 8
p1 = [_8[0][0], _8[0][1], _8[0][2], _8[1][2], _8[2][2], _8[2][1], _8[2][0],
_8[1][0]]
p = sorted(p1)
q = [abs(int(p[0]) - int(p[1])), abs(int(p[1]) - int(p[2])), abs(int(p[2]) - int(p[3])), abs(int(p[3]) - int(p[4])),
abs(int(p[4]) - int(p[5])), abs(int(p[5]) - int(p[6])), abs(int(p[6]) - int(p[7]))]
# 判断区分度
if max(q) < 10:
return result, False
max_index = q.index(max(q))
for i in range(0, max_index + 1):
for j in range(len(q)):
if p1[j] == p[i]:
result[j] = 1
for i in range(8):
if result[i] != 1:
result[i] = 2
return result, True


def find_best(a, x, y, flag):
p = a[x][y]
neighborhood = []
if flag[x - 1][y - 1] == 0:
neighborhood.append(a[x - 1][y - 1])
if flag[x - 1][y] == 0:
neighborhood.append(a[x - 1][y])
if flag[x - 1][y + 1] == 0:
neighborhood.append(a[x - 1][y + 1])
if flag[x][y + 1] == 0:
neighborhood.append(a[x][y + 1])
if flag[x + 1][y + 1] == 0:
neighborhood.append(a[x + 1][y + 1])
if flag[x + 1][y] == 0:
neighborhood.append(a[x + 1][y])
if flag[x + 1][y - 1] == 0:
neighborhood.append(a[x + 1][y - 1])
if flag[x][y - 1] == 0:
neighborhood.append(a[x][y - 1])

d = []
for i in range(len(neighborhood)):
d.append(abs(int(neighborhood[i]) - int(p)))
if len(d) == 0:
return p
min_index = d.index(min(d))

return neighborhood[min_index]


def denoise_1_7(channel, flag, k):
"""
:param channel: 待检测通道
:param flag: 噪声标记
:param k: 多尺度指数
:return: 被找到的噪声个数
"""
if k == 1:
x, y = channel.shape

flag0 = np.zeros((x, y))

sub_flag = 0

for i in range(1, x - 1):
for j in range(1, y - 1):
# 取出九宫格
a = [[0] * 3 for ix in range(3)]
for i0 in range(3):
for j0 in range(3):
a[i0][j0] = channel[i - 1 + i0][j - 1 + j0]
# 划分区域
div, div_flag = division(a)
if not div_flag:
continue
cnt = 0
for i0 in range(8):
if div[i0] == 1:
cnt += 1
if cnt == 1:
for i0 in range(8):
if div[i0] == 1:
x0, y0 = _1d_2_2d(i0)
if flag[i - 1 + x0][j - 1 + y0] == 1 and flag0[i - 1 + x0][j - 1 + y0] == 0:
sub_flag += 1
flag0[i - 1 + x0][j - 1 + y0] = 1
# channel[i - 1 + x0][j - 1 + y0] = find_best(a, x0, y0)
elif cnt == 7:
for i0 in range(8):
if div[i0] == 2:
x0, y0 = _1d_2_2d(i0)
if flag[i - 1 + x0][j - 1 + y0] == 1 and flag0[i - 1 + x0][j - 1 + y0] == 0:
sub_flag += 1
flag0[i - 1 + x0][j - 1 + y0] = 1
# channel[i - 1 + x0][j - 1 + y0] = find_best(a, x0, y0)
for i in range(1, x - 1):
for j in range(1, y - 1):
if flag[i][j] == 1:
channel[i][j] = find_best(channel, i, j, flag0)
return channel, sub_flag

def denoise(noise, noise_flag, k):
"""
:param noise: 噪声图像
:param noise_flag: 噪声标记
:param k: 多尺度指数
:return: 修改后的噪声图像, 被找到的噪声
"""
b_noise, g_noise, r_noise = cv.split(noise)

sub_flag = 0

b_noise, f = denoise_1_7(b_noise, noise_flag[0], k)
sub_flag += f
g_noise, f = denoise_1_7(g_noise, noise_flag[1], k)
sub_flag += f
r_noise, f = denoise_1_7(r_noise, noise_flag[2], k)
sub_flag += f

new_noise = cv.merge((b_noise, g_noise, r_noise))

return new_noise, sub_flag


def get_noise_flag(origin, noise):
"""
:param origin: Mat原图
:param noise: Mat噪声图
:return: flag矩阵
"""
b_origin, g_origin, r_origin = cv.split(origin)
b_noise, g_noise, r_noise = cv.split(noise)

row, col = b_origin.shape

flag = np.zeros((3, row, col))

cnt_flag = 0

for i in range(row):
for j in range(col):
if b_origin[i][j] != b_noise[i][j]:
flag[0][i][j] = 1
cnt_flag += 1
if g_origin[i][j] != g_noise[i][j]:
flag[1][i][j] = 1
cnt_flag += 1
if r_origin[i][j] != r_noise[i][j]:
flag[2][i][j] = 1
cnt_flag += 1
return flag, cnt_flag


def main(origin, noise, k):
"""
:param origin: 原始标准图片路径
:param noise: 噪声图路径
:param k: 多尺度指数
:return:
"""
img_origin = cv.imread(origin, 1)
img_noise = cv.imread(noise, 1)

noise_flag, cnt_flag = get_noise_flag(img_origin, img_noise)

print('原噪声个数:', cnt_flag)

noises = [img_noise]
for i in range(10):
new, sub_flag = denoise(noises[i], noise_flag, k)
noises.append(new)
print('[' + str(i + 1) + ']', sub_flag / cnt_flag)

cv.imwrite('new.png', noises[9])


if __name__ == '__main__':
origin_addr = 'img.png'
noise_addr = 'img_1.png'
main(origin_addr, noise_addr, 1)

测试效果

原图:

img

噪声图:

img_1

处理图:

new

结果分析

  • 这是当k = 1时,进行10次降噪之后的结果,从结果看,无明显1:7类型的噪声存在
  • 与上一次工作的处理图相比,降噪效果更好
  • 降噪在进行到第四次时,被处理掉的噪声比例已经明显降低
  • 第五次时,更低
  • 从第六次降噪完之后,就不再出现新减少的噪声