-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathISVaC.py
290 lines (219 loc) · 9.13 KB
/
ISVaC.py
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
'''
Image Sensor Vacuum Cleaner.
Author:
C. Meyer
'''
import os
import imageio
import numpy as np
from numba import jit
# User parameters
user_threshold = 1000
user_dilatation = 8
if not os.path.exists("./masks_" + str(user_threshold) + "_" + str(user_dilatation)):
os.mkdir("./masks_" + str(user_threshold) + "_" + str(user_dilatation))
if not os.path.exists("./results_" + str(user_threshold) + "_" + str(user_dilatation)):
os.mkdir("./results_" + str(user_threshold) + "_" + str(user_dilatation))
images_list = []
files_list = []
file_names_list = []
files_number = 0
print("ISVaC")
# Read file names
for file in os.listdir("./data"):
if file.endswith(".JPG"):
files_list.append(os.path.join("./data", file))
file_names_list.append(file)
files_number += 1
# Read images
print("Read images")
cpt = 1
for filename in files_list:
print(str(cpt), "/", str(files_number), os.path.join("./data", file))
cpt += 1
image = imageio.imread(filename)
images_list.append(image)
# convert images list in numpy array for numba
images_list = np.array(images_list, dtype=np.uint8)
# TODO : add a verification to check that all images are the same shape
# Create the mask
# TODO : better solution than a threshold on the three channel variance
@jit(nopython=True)
def compute_mask(images, threshold):
mask = np.zeros((images.shape[1], images.shape[2]), dtype=np.uint8)
variance_map = np.zeros(images[0].shape, dtype=np.int32)
for i in range(0, images.shape[1]):
for j in range(0, images.shape[2]):
c0 = np.zeros((images.shape[0]), dtype=np.int32)
c1 = np.zeros((images.shape[0]), dtype=np.int32)
c2 = np.zeros((images.shape[0]), dtype=np.int32)
for k in range(0, images.shape[0]):
c0[k] = images[k][i][j][0]
c1[k] = images[k][i][j][1]
c2[k] = images[k][i][j][2]
variance_map[i][j][0] = c0.var()
variance_map[i][j][1] = c1.var()
variance_map[i][j][2] = c2.var()
if np.sum(variance_map[i][j]) < threshold:
mask[i][j] = 1
return mask
print("Search for common pixels, and add them to the mask)")
mask = compute_mask(images_list, user_threshold).astype(bool)
# This is completely specific to my images
def mask_remove_date_zone(mask):
for i in range(1798, 1865):
for j in range(1916, 2429):
mask[i][j] = False
return mask
print("Removing date zone from mask")
mask = mask_remove_date_zone(mask)
def mask_dilation(mask):
new_mask = np.zeros(mask.shape)
for i in range(1, mask.shape[0]-1):
for j in range(1, mask.shape[1]-1):
if mask[i][j]:
new_mask[i][j] = True
new_mask[i][j+1] = True
new_mask[i][j-1] = True
new_mask[i+1][j] = True
new_mask[i+1][j+1] = True
new_mask[i+1][j-1] = True
new_mask[i-1][j] = True
new_mask[i-1][j+1] = True
new_mask[i-1][j-1] = True
return new_mask
print("Mask dilatation")
for cpt in range(0, user_dilatation):
mask = mask_dilation(mask)
print("Write the dirt mask")
imageio.imwrite(os.path.join("./results_" + str(user_threshold) + "_" + str(user_dilatation), "MASK.JPG"), (mask*255).astype("uint8"))
"""
# for the recursive version of expand_components
import sys
sys.setrecursionlimit(5000)
def expand_components(local_mask, component, i, j):
if local_mask[i + 1][j]:
local_mask[i + 1][j] = False
component.append((i + 1, j))
local_mask, component = expand_components(local_mask, component, i + 1, j)
if local_mask[i - 1][j]:
local_mask[i - 1][j] = False
component.append((i - 1, j))
local_mask, component = expand_components(local_mask, component, i - 1, j)
if local_mask[i][j + 1]:
local_mask[i][j + 1] = False
component.append((i, j + 1))
local_mask, component = expand_components(local_mask, component, i, j + 1)
if local_mask[i][j - 1]:
local_mask[i][j - 1] = False
component.append((i, j - 1))
local_mask, component = expand_components(local_mask, component, i, j - 1)
return local_mask, component
"""
def expand_components_iterative(local_mask, component, i, j):
todo = [(i, j)]
while len(todo) > 0:
i, j = todo.pop()
if local_mask[i + 1][j]:
local_mask[i + 1][j] = False
component.append((i + 1, j))
todo.append((i + 1, j))
if local_mask[i - 1][j]:
local_mask[i - 1][j] = False
component.append((i - 1, j))
todo.append((i - 1, j))
if local_mask[i][j + 1]:
local_mask[i][j + 1] = False
component.append((i, j + 1))
todo.append((i, j + 1))
if local_mask[i][j - 1]:
local_mask[i][j - 1] = False
component.append((i, j - 1))
todo.append((i, j - 1))
return local_mask, component
def get_components(mask):
components = []
components_min_x = []
components_max_x = []
components_min_y = []
components_max_y = []
local_mask = np.copy(mask)
c = 0
for i in range(0, local_mask.shape[0]):
for j in range(0, local_mask.shape[1]):
if local_mask[i][j]:
local_mask[i][j] = False
component = [(i, j)]
local_mask, component = expand_components_iterative(mask, component, i, j)
max_x = 0
max_y = 0
min_x = 10e10
min_y = 10e10
for k in range(0, len(component)):
min_x = min(component[k][0], min_x)
max_x = max(component[k][0], max_x)
min_y = min(component[k][1], min_y)
max_y = max(component[k][1], max_y)
components.append(component)
components_min_x.append(min_x)
components_max_x.append(max_x)
components_min_y.append(min_y)
components_max_y.append(max_y)
c += 1
return components, components_min_x, components_max_x, components_min_y, components_max_y
print("Create list of 4-connected components")
components, components_min_x, components_max_x, components_min_y, components_max_y = get_components(mask)
def put_red(components, input_image):
image = np.copy(input_image)
for i in range(0, len(components)):
for j in range(0, len(components[i])):
image[components[i][j][0]][components[i][j][1]][0] = 255
image[components[i][j][0]][components[i][j][1]][1] = 0
image[components[i][j][0]][components[i][j][1]][2] = 0
return image
# TODO : there is clearly a problem with the min/max x/y rectangle with close component and close to borders,
# need to fix this.
def clean_image(components, components_min_x, components_max_x, components_min_y, components_max_y, input_image):
image = np.copy(input_image)
# color reconstruction
for i in range(0, len(components)):
minx = components_min_x[i]
maxx = components_max_x[i]
miny = components_min_y[i]
maxy = components_max_y[i]
dist_x = maxx - minx
dist_y = maxy - miny
col_left = image[minx-1][miny + round(dist_y/2)]
col_right = image[maxx+1][miny + round(dist_y/2)]
col_down = image[minx + round(dist_x/2)][miny-1]
col_top = image[minx + round(dist_x/2)][miny+1]
col_down_left = image[minx-1][miny-1]
col_top_left = image[minx-1][maxy+1]
col_down_right = image[maxx+1][miny-1]
col_top_right = image[maxx+1][maxy+1]
for j in range(0, len(components[i])):
x = components[i][j][0]
y = components[i][j][1]
prop_left = (maxx - x) / dist_x
prop_right = 1 - prop_left
prop_down = (maxy - y) / dist_y
prop_top = 1 - prop_down
prop_down_left = (prop_down + prop_left) / 4
prop_top_left = (prop_top + prop_left) / 4
prop_down_right = (prop_down + prop_right) / 4
prop_top_right = (prop_top + prop_right) / 4
col = ((prop_left * col_left) + (prop_right * col_right) + (prop_down * col_down) + (prop_top * col_top)) / 2
col += (prop_down_left * col_down_left) + (prop_top_left * col_top_left) +\
(prop_down_right * col_down_right) + (prop_top_right * col_top_right)
col /= 2
image[x][y] = col
return image
print("Remove every pixel of the mask in input image using a different rectangle of gradient")
cpt = 0
for file in file_names_list:
print(file)
# Image to view the components in red
imageio.imwrite(os.path.join("./masks_" + str(user_threshold) + "_" + str(user_dilatation), file), put_red(components, images_list[cpt]))
# Testing the solution on the first image
imageio.imwrite(os.path.join("./results_" + str(user_threshold) + "_" + str(user_dilatation), file), clean_image(components, components_min_x, components_max_x, components_min_y, components_max_y, images_list[cpt]))
cpt += 1