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 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
|
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 find_noise(x, y, channel, flag, flag0, sub_flag): 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 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 return flag0, sub_flag
def shrunk(channel, k): x, y = channel.shape result = np.zeros((k, k), dtype=type(channel)) for i in range(k): for j in range(k): result0 = np.zeros((x // k, y // k)) for m in range(0, x // k): for n in range(0, y // k): if m * k + i < x and n * k + j < y: result0[m][n] = channel[m * k + i][n * k + j] result[i][j] = result0 return result
def merge_array(array, x, y, k): result = np.zeros((x, y)) for i in range(k): for j in range(k): for m in range(x // k): for n in range(y // k): result[m * k + i][n * k + j] = array[i][j][m][n] return result
def denoise_1_7(channel, flag, k): """ :param channel: 待检测通道 :param flag: 噪声标记 :param k: 多尺度指数 :return: 被找到的噪声个数 """ if k == 2: x, y = channel.shape flag0 = np.zeros((k, k), dtype=type(channel)) sub_flag = 0 shrunk_channel = shrunk(channel, k) for i in range(k): for j in range(k): sx, sy = shrunk_channel[0][0].shape flag00 = np.zeros((sx, sy)) flag00, sub_flag = find_noise(sx, sy, shrunk_channel[i][j], flag, flag00, sub_flag) flag0[i][j] = flag00
for m in range(1, sx - 1): for n in range(1, sy - 1): if flag[m * k + i][n * k + j] == 1: shrunk_channel[i][j][m][n] = find_best(shrunk_channel[i][j], m, n, flag00)
channel = merge_array(shrunk_channel, x, y, k) ''' 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_1.png' noise_addr = 'img_2.png' main(origin_addr, noise_addr, 2)
|