-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplot_filtered_bounds.py
More file actions
288 lines (228 loc) · 12.2 KB
/
plot_filtered_bounds.py
File metadata and controls
288 lines (228 loc) · 12.2 KB
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
# -*- coding: utf-8 -*-
"""
Created on Sat Aug 12 12:44:26 2023
@author: ADIL003
"""
# %% Imports
from pathlib import Path
import copy
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import rasterio as rio
import rasterio.warp
from rasterio.windows import Window
import pandas as pd
#import geowombat as gw
import scipy
from scipy import ndimage as ndi
from scipy.ndimage import distance_transform_edt
from scipy.ndimage import binary_dilation
import skimage
from skimage import morphology
from skimage.feature import peak_local_max
from skimage.segmentation import watershed
from sklearn.preprocessing import minmax_scale
from sklearn.neighbors import NearestNeighbors
from enum import Enum
from tqdm import tqdm
import gc
import os
import uuid
from adjustText import adjust_text
import itertools
# %%
os.chdir('/home/pitter/Tree-Classification')
dirpaths = [
R'/home/pitter/Tree-Classification/casuarina//93deg',
R'/home/pitter/Tree-Classification/casuarina//183deg',
R'/home/pitter/Tree-Classification/chestnut//10May2021',
R'/home/pitter/Tree-Classification/chestnut//18Dec2020',
]
dirpaths = [Path(i) for i in dirpaths]
# %% Load each image and its corresponding bounds
images = []
for dirpath in dirpaths:
if (adjusted := dirpath / 'adjusted.tif').exists():
image = rio.open(adjusted)
unadjusted = None
else:
image = rio.open(unadjusted := dirpath / 'result.tif')
adjusted = None
filtered_bounds = pd.read_csv(bounds := dirpath / '{}_filtered_bounds.csv'.format(dirpath.name))
print('Loaded {}'.format(adjusted or unadjusted))
print('Loaded {}'.format(bounds))
images.append((image, adjusted or unadjusted, filtered_bounds, bounds))
# %% Draw the bounds for each image
for image, image_path, bounds, bounds_path in images:
px_height, px_width = image.height, image.width
dpi = 100
in_height, in_width = px_height/dpi, px_width/dpi
fig, ax = plt.subplots(figsize=(in_width, in_height))
img = image.read([1, 2, 3]).transpose(1, 2, 0)
idx = (img[...,:3] == np.array((0,0,0))).all(axis=-1)
img[idx] = 127
plt.imshow(img, interpolation='none')
texts = []
for idx, props in bounds.iterrows():
minr, minc, maxr, maxc = props[['minr', 'minc', 'maxr', 'maxc']]
bx = (minc, maxc, maxc, minc, minc)
by = (minr, minr, maxr, maxr, minr)
ax.plot(bx, by, '-', color='white', linewidth=10)
texts.append(ax.text(minc+5, maxr-5, '{}: {}'.format(str(idx), props['uuid']), fontsize=20))
#adjust_text(texts)
plt.savefig(bounds_path.parent / 'bboxes.jpg')
# %% Coregister the images
for pair in list(itertools.pairwise(images))[::2]:
image1, image_path1, bounds1, bounds_path1 = pair[0]
image2, image_path2, bounds2, bounds_path2 = pair[1]
image1_bounds = image1.bounds
image2_bounds = image2.bounds
union_bounds = rio.coords.BoundingBox(min(image1_bounds[0], image2_bounds[0]),
min(image1_bounds[1], image2_bounds[1]),
max(image1_bounds[2], image2_bounds[2]),
max(image1_bounds[3], image2_bounds[3]))
union_px_topleft_image1 = image1.index(union_bounds[0], union_bounds[3])
union_px_botright_image1 = image1.index(union_bounds[2], union_bounds[1])
union_px_topleft_image2 = image2.index(union_bounds[0], union_bounds[3])
union_px_botright_image2 = image2.index(union_bounds[2], union_bounds[1])
window_im1 = Window(col_off=union_px_topleft_image1[1],
row_off=union_px_topleft_image1[0],
width=union_px_botright_image1[1]-union_px_topleft_image1[1],
height=union_px_botright_image1[0]-union_px_topleft_image1[0])
window_im2 = Window(col_off=union_px_topleft_image2[1],
row_off=union_px_topleft_image2[0],
width=union_px_botright_image2[1]-union_px_topleft_image2[1],
height=union_px_botright_image2[0]-union_px_topleft_image2[0])
im1_lined_up = image1.read([1, 2, 3], window=window_im1, boundless=True)
im2_lined_up = image2.read([1, 2, 3], window=window_im2, boundless=True)
with rasterio.open(
str(bounds_path1.parent.parent / 'superimposed.tif'),
'w',
driver='GTiff',
height=im1_lined_up.shape[1],
width=im1_lined_up.shape[2],
count=2,
dtype=im1_lined_up.dtype,
crs=image1.crs,
transform=rio.windows.transform(window_im1, image1.transform),
) as dst:
dst.write(im1_lined_up.mean(axis=0), 1)
dst.write(im2_lined_up.mean(axis=0), 2)
# %% Now, get the nearest neighbours
for pair in list(itertools.pairwise(images))[::2]:
image1, image_path1, bounds1, bounds_path1 = pair[0]
image2, image_path2, bounds2, bounds_path2 = pair[1]
image1_bounds = image1.bounds
image2_bounds = image2.bounds
union_bounds = rio.coords.BoundingBox(min(image1_bounds[0], image2_bounds[0]),
min(image1_bounds[1], image2_bounds[1]),
max(image1_bounds[2], image2_bounds[2]),
max(image1_bounds[3], image2_bounds[3]))
union_px_topleft_image1 = image1.index(union_bounds[0], union_bounds[3])
union_px_botright_image1 = image1.index(union_bounds[2], union_bounds[1])
union_px_topleft_image2 = image2.index(union_bounds[0], union_bounds[3])
union_px_botright_image2 = image2.index(union_bounds[2], union_bounds[1])
window_im1 = Window(col_off=union_px_topleft_image1[1],
row_off=union_px_topleft_image1[0],
width=union_px_botright_image1[1]-union_px_topleft_image1[1],
height=union_px_botright_image1[0]-union_px_topleft_image1[0])
window_im2 = Window(col_off=union_px_topleft_image2[1],
row_off=union_px_topleft_image2[0],
width=union_px_botright_image2[1]-union_px_topleft_image2[1],
height=union_px_botright_image2[0]-union_px_topleft_image2[0])
bounds1[['maxr_fixed', 'minr_fixed']] = bounds1[['maxr', 'minr']] - window_im1.row_off
bounds1[['maxc_fixed', 'minc_fixed']] = bounds1[['maxc', 'minc']] - window_im1.col_off
bounds2[['maxr_fixed', 'minr_fixed']] = bounds2[['maxr', 'minr']] - window_im2.row_off
bounds2[['maxc_fixed', 'minc_fixed']] = bounds2[['maxc', 'minc']] - window_im2.col_off
for bounds in [bounds1, bounds2]:
bounds['center_r'] = (bounds['minr_fixed'] + bounds['maxr_fixed']) / 2
bounds['center_c'] = (bounds['minc_fixed'] + bounds['maxc_fixed']) / 2
bounds['row_rad'] = (bounds['maxr_fixed'] - bounds['minr_fixed']) / 2
bounds['col_rad'] = (bounds['maxc_fixed'] - bounds['minc_fixed']) / 2
bounds['rad'] = bounds[['row_rad', 'col_rad']].min(axis=1)
new = bounds1
old = bounds2
nn = NearestNeighbors()
nn.fit(X=old[['center_r', 'center_c']])
nn_out = nn.kneighbors(new[['center_r', 'center_c']], 1)
dists, preds = nn_out[0].squeeze(), nn_out[1].squeeze()
matched = pd.concat((new['uuid'].reset_index(drop=True),
old['uuid'].loc[preds].reset_index(drop=True)),
axis=1)\
.reset_index(drop=True)
matched.columns = [bounds_path1.parent.name, bounds_path2.parent.name]
matched['distance_px'] = dists
preds_filtered = preds[dists < new['rad']]
dists_filtered = dists[dists < new['rad']]
temp = pd.Series(dists < new['rad'])
temp.index = new.index
new_uuids_filtered = new['uuid'][temp]
matched_filtered = pd.concat((new_uuids_filtered.reset_index(drop=True),
old['uuid'].loc[preds_filtered].reset_index(drop=True)),
axis=1)\
.reset_index(drop=True)
matched_filtered.columns = [bounds_path1.parent.name, bounds_path2.parent.name]
matched_filtered['distance_px'] = dists_filtered
matched.to_csv(bounds_path1.parent.parent / '{}_{}_{}_bounding_boxes_match.csv'
.format(bounds_path1.parent.parent.name, bounds_path1.parent.name, bounds_path2.parent.name))
matched_filtered.to_csv(bounds_path1.parent.parent / '{}_{}_{}_bounding_boxes_match_filtered.csv'
.format(bounds_path1.parent.parent.name, bounds_path1.parent.name, bounds_path2.parent.name))
bounds1[bounds1['uuid'].isin(matched_filtered[bounds_path1.parent.name])]\
.to_csv(bounds_path1.parent / (bounds_path1.name.split('.')[0] + '_without_unmatched.csv'))
bounds2[bounds2['uuid'].isin(matched_filtered[bounds_path2.parent.name])]\
.to_csv(bounds_path2.parent / (bounds_path2.name.split('.')[0] + '_without_unmatched.csv'))
# %% To JSON
for csv in Path(r'/home/pitter/Tree-Classification/FilesForGui/').glob('*'):
df = pd.read_csv(csv)
df.to_json(csv.parent / (csv.name.split('.')[0] + '.json'), default_handler=str)
# %% Redraw the bounds with the nearest-neighbours visualised
for pair in list(itertools.pairwise(images))[::2]:
image1, image_path1, bounds1, bounds_path1 = pair[0]
image2, image_path2, bounds2, bounds_path2 = pair[1]
with rasterio.open(
str(bounds_path1.parent.parent / 'superimposed.tif'),
'r') as dst:
px_height, px_width = dst.height, dst.width
dpi = 100
in_height, in_width = px_height/dpi, px_width/dpi
fig, ax = plt.subplots(figsize=(in_width, in_height))
image = dst.read([1, 2])
image = np.stack((image[0], image[1], np.zeros_like(image[0]))).transpose(1, 2, 0)
idx = (image[...,:3] == np.array((0,0,0))).all(axis=-1)
image[idx] = 127
plt.imshow(image, interpolation='none')
bounds1['origin'] = bounds_path1.parent.name
bounds2['origin'] = bounds_path2.parent.name
matched_filtered = pd.read_csv(bounds_path1.parent.parent / '{}_{}_{}_bounding_boxes_match_filtered.csv'
.format(bounds_path1.parent.parent.name, bounds_path1.parent.name,
bounds_path2.parent.name),
index_col = 0)
bounds1['in_matched_filtered'] = bounds1['uuid'].isin(matched_filtered[bounds_path1.parent.name])
bounds2['in_matched_filtered'] = bounds2['uuid'].isin(matched_filtered[bounds_path2.parent.name])
bounds = pd.concat((bounds1, bounds2))
texts = []
for idx, props in bounds.iterrows():
minr, minc, maxr, maxc = props[['minr_fixed', 'minc_fixed', 'maxr_fixed', 'maxc_fixed']]
bx = (minc, maxc, maxc, minc, minc)
by = (minr, minr, maxr, maxr, minr)
ax.plot(bx, by, '-',
color='white' if not props['in_matched_filtered'] else
('blue' if props['origin'] == bounds_path1.parent.name else 'red'),
linewidth=10)
texts.append(ax.text(minc+5, maxr-5, '{}: {}'.format(str(idx), props['uuid']), fontsize=12))
#adjust_text(texts)
matched_filtered = matched_filtered.merge(bounds1[['uuid', 'center_r', 'center_c']],
left_on=bounds_path1.parent.name,
right_on='uuid',
suffixes=('_m', '_n'))
matched_filtered = matched_filtered.merge(bounds2[['uuid', 'center_r', 'center_c']],
left_on=bounds_path2.parent.name,
right_on='uuid',
suffixes=('_n', '_o'))
for idx, row in matched_filtered.iterrows():
r_n, c_n = row[['center_r_n', 'center_c_n']]
r_o, c_o = row[['center_r_o', 'center_c_o']]
ax.arrow(c_n, r_n, c_o-c_n, r_o-r_n, width=10, color='blue')
print("plotted bounds for {}".format(bounds_path1.parent.parent))
plt.savefig(str(bounds_path1.parent.parent / 'bboxes.jpg'))