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 272 273
|
""" 1.找出噪声点: 新划分方法 标记次数 2.修改 找最接近的未标记过的点 """ import cv2 as cv import numpy as np
threshold = 1
class PixelChannel: def __init__(self, channel, noise, row, col): self.noise = noise self.channel = channel self.row = row self.col = col
class Part: def __init__(self, x, y, area): self.x = x self.y = y self.area = area
def create_pixel_channel(img_channel): (row, col) = img_channel.shape result = PixelChannel(img_channel, noise_check(img_channel), row, col) return result
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 division1(_8area): result = [0] * 8 p1 = [_8area[0][0], _8area[0][1], _8area[0][2], _8area[1][2], _8area[2][2], _8area[2][1], _8area[2][0], _8area[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): result[p1.index(p[i])] = 1 for i in range(8): if result[i] != 1: result[i] = 2
return result, True
def division2(_8area, del_num): r = [0] * 8 p1 = [_8area[0][0], _8area[0][1], _8area[0][2], _8area[1][2], _8area[2][2], _8area[2][1], _8area[2][0], _8area[1][0]] p2 = [] for i in range(8): if i == del_num: continue p2.append(p1[i]) result = [0] * 8
p = sorted(p2) 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]))] if max(q) < 10: return result, False max_index = q.index(max(q)) for i in range(0, max_index + 1): result[p1.index(p[i])] = 1 for i in range(8): if result[i] != 1: result[i] = 2 result[del_num] = 0 return result, True
def noise_check(img_channel): (row, col) = img_channel.shape result = [[0] * col for i in range(row)] for i in range(1, row - 1): for j in range(1, col - 1): _8_area = [[0] * 3 for i in range(3)] _8_area[0][0] = img_channel[i - 1][j - 1] _8_area[0][1] = img_channel[i - 1][j] _8_area[0][2] = img_channel[i - 1][j + 1] _8_area[1][2] = img_channel[i][j + 1] _8_area[2][2] = img_channel[i + 1][j + 1] _8_area[2][1] = img_channel[i + 1][j] _8_area[2][0] = img_channel[i + 1][j - 1] _8_area[1][0] = img_channel[i][j - 1] part1, flag1 = division1(_8_area) if not flag1: continue
part2 = [] for k in range(7): cnt = 0 part2, flag2 = division2(_8_area, k) if not flag2: if k == 0 or k == 7: cnt += 1 else: continue
for m in range(8): if part2[m] != 0 and part1[m] != part2[m]: cnt += 1 if cnt >= 1: x, y = _1d_2_2d(k) result[i - 1 + x][j - 1 + y] += 1
return result
def mark(pixel_channel): for i in range(pixel_channel.row): for j in range(pixel_channel.col): if pixel_channel.noise[i][j] >= threshold: pixel_channel.channel[i][j] = find_best(pixel_channel, i, j) return pixel_channel
def find_best(pixel_channel, x, y): p = pixel_channel.channel[x][y] neighborhood = [] if pixel_channel.row > x - 1 >= 0 and pixel_channel.col > y - 1 >= 0 and pixel_channel.noise[x - 1][y - 1] < threshold: neighborhood.append(pixel_channel.channel[x - 1][y - 1]) if pixel_channel.row > x - 1 >= 0 and pixel_channel.col > y >= 0 and pixel_channel.noise[x - 1][y] < threshold: neighborhood.append(pixel_channel.channel[x - 1][y]) if pixel_channel.row > x - 1 >= 0 and pixel_channel.col > y + 1 >= 0 and pixel_channel.noise[x - 1][y + 1] < threshold: neighborhood.append(pixel_channel.channel[x - 1][y + 1]) if pixel_channel.row > x >= 0 and pixel_channel.col > y + 1 >= 0 and pixel_channel.noise[x][y + 1] < threshold: neighborhood.append(pixel_channel.channel[x][y + 1]) if pixel_channel.row > x + 1 >= 0 and pixel_channel.col > y + 1 >= 0 and pixel_channel.noise[x + 1][y + 1] < threshold: neighborhood.append(pixel_channel.channel[x + 1][y + 1]) if pixel_channel.row > x + 1 >= 0 and pixel_channel.col > y >= 0 and pixel_channel.noise[x + 1][y] < threshold: neighborhood.append(pixel_channel.channel[x + 1][y]) if pixel_channel.row > x + 1 >= 0 and pixel_channel.col > y - 1 >= 0 and pixel_channel.noise[x + 1][y - 1] < threshold: neighborhood.append(pixel_channel.channel[x + 1][y - 1]) if pixel_channel.row > x >= 0 and pixel_channel.col > y - 1 >= 0 and pixel_channel.noise[x][y - 1] < threshold: neighborhood.append(pixel_channel.channel[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 repair(pixel_channel, half_channel): for i in range(half_channel[0].row): for j in range(half_channel[0].col): if half_channel[0].noise[i][j] >= threshold: pixel_channel.noise[i * 2][j * 2] += half_channel[0].noise[i][j]
for i in range(half_channel[1].row): for j in range(half_channel[1].col): if half_channel[1].noise[i][j] >= threshold: pixel_channel.noise[i * 2][j * 2 + 1] += half_channel[1].noise[i][j]
for i in range(half_channel[2].row): for j in range(half_channel[2].col): if half_channel[2].noise[i][j] >= threshold: pixel_channel.noise[i * 2 + 1][j * 2] += half_channel[2].noise[i][j]
for i in range(half_channel[3].row): for j in range(half_channel[3].col): if half_channel[3].noise[i][j] >= threshold: pixel_channel.noise[i * 2 + 1][j * 2 + 1] += half_channel[3].noise[i][j]
for i in range(pixel_channel.row): for j in range(pixel_channel.col): if pixel_channel.noise[i][j] >= threshold: pixel_channel.channel[i][j] = find_best(pixel_channel, i, j)
return pixel_channel
def main(): img_address = "img_noise.png" img = cv.imread(img_address, 1) cv.imshow("img_noise.png", img) channel_b, channel_g, channel_r = cv.split(img) b = create_pixel_channel(channel_b) g = create_pixel_channel(channel_g) r = create_pixel_channel(channel_r)
fp = open('b.noise.csv', 'w') for i in range(b.row): for j in range(b.col): print(b.noise[i][j], file=fp, end='') print(",", file=fp, end='') print("", file=fp)
fp = open('b.pixel.csv', 'w') for i in range(b.row): for j in range(b.col): print(b.channel[i][j], file=fp, end='') print(",", file=fp, end='') print("", file=fp)
new_img = cv.merge((mark(b).channel, mark(g).channel, mark(r).channel))
cv.imwrite("denoised_img.png", new_img) cv.imshow("denoised_img.png", new_img) cv.waitKey() cv.destroyAllWindows()
if __name__ == '__main__': main()
|