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 | def tile_run(path,grid, orig_shape, img_side, over_n, score_thresh = 0):
"""Main wrapper function to start the tile stitching process for the overall image from individual tiles post-inference.
Args:
path (str): path with all the data in it - should be same as that used previously for inference.
grid ((int, int)): (n,m) values from function that sliced images
orig_shape ((int, int)): Original shape of the image inference was performed on.
img_side (int): Size of square tile image used - length of side.
over_n (int): Overlap between tiles in pixels
Returns:
tiled_img (ndarray): Final tiled image showing all the labels.
final_scores (dict): Final dictionary of all the detection scores.
"""
tiled_img = np.zeros(orig_shape[:2])
row_len = orig_shape[1]
#scores[0] is for label == 1 for future reference
overall_scores = {}
tiled, scores = load_imgs(path, img_side, grid, score_thresh)
for j in range(grid[0]):
tiled_row = np.zeros((img_side, orig_shape[1]))
row_scores = {}
for i in range(grid[1]):
current_tile_end = (i+1)*(img_side - over_n) + over_n
if current_tile_end > orig_shape[1]:
excess = current_tile_end - orig_shape[1]
else:
excess = 0
#print(i)
x = i#horizontal
#set top left corner at which they overlap
corner_x = x*(img_side-over_n) - excess
if i == 0:
tiled_row[0:img_side, corner_x:corner_x + img_side] += tiled[j*grid[1]]
row_scores = score_update(scores[j*grid[1]], np.unique(tiled[j*grid[1]])[1:],row_scores)
else:
#make sure no labels are the same across the two images
filler_label = np.unique(tiled_row)[-1]
new_img = tiled[(grid[1]*j)+i] + filler_label
new_img[new_img == filler_label] = 0
row_scores = score_update(scores[(grid[1]*j)+i], np.unique(new_img)[1:], row_scores, filler=filler_label)
tiled_row, n_img = left_overlap(tiled_row, new_img, 0, corner_x, over_n=over_n + excess ,img_side=img_side)
#update score dictionary
row_scores = post_tile_score_update(new_img, n_img, row_scores)
current_row_end = (j+1)*(img_side - over_n) + over_n
if current_row_end > orig_shape[0]:
row_excess = current_row_end - orig_shape[0]
else:
row_excess = 0
y = j#vertical
corner_y = y*(img_side - over_n) - row_excess
if j == 0:
tiled_img[corner_y:corner_y+img_side, 0:row_len] += tiled_row
#just copy the existing dictionary
overall_scores = row_scores.copy()
else:
row_filler_label = np.unique(tiled_img)[-1]
tiled_row = tiled_row + row_filler_label
tiled_row[tiled_row == row_filler_label] = 0
#add row score in to overall dict
overall_scores = row_score_update(row_scores, overall_scores, row_filler_label)
#do tiling
tiled_img, n_img = top_overlap_row(tiled_img, tiled_row, corner_y, 0, over_n=over_n+row_excess ,img_side = img_side, row_len=row_len)
#update score dictionary
overall_scores = post_tile_score_update(tiled_row, n_img, overall_scores)
#optimizing label assignment
n_unique = np.unique(tiled_img)
print(n_unique.size)
print(len(overall_scores.keys()))
final_scores = {}
for i in range(n_unique.size):
tiled_img[tiled_img == n_unique[i]] = i
if i == 0:
pass
else:
final_scores[i] = overall_scores[n_unique[i]]
return tiled_img, final_scores
|