-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_xys.py
87 lines (69 loc) · 2.58 KB
/
make_xys.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
import numpy as np
from skimage import io
from skimage.transform import rotate, resize
import os
import cv2
import pandas as pd
train_ids = [11, 13, 1, 21, 23, 26, 28, 30, 32, 34, 37, 3, 5, 7]
infer_ids = [15,17]
FOLDER='../groundtruth_2D/' #folder with 2D groundtruth tif images. For conversion see
# https://github.com/nshaud/DeepNetsForEO/blob/master/legacy/notebooks/convert_gt.py
step=64
patch_s=256
def sliding_window(i_city, ss, labeled_areas, label, window_size, step):
city=[]
fpatches_labels=[]
x=0
while (x!=label.shape[0]):
y=0
while(y!=label.shape[1]):
if (not y+window_size > label.shape[1]) and (not x+window_size > label.shape[0]):
line=np.array([x,y, labeled_areas.index(ss)])
city.append(line)
stride=step
if y + window_size == label.shape[1]:
break
if y + window_size > label.shape[1]:
y = label.shape[1] - window_size
else:
y = y+stride
if x + window_size == label.shape[0]:
break
if x + window_size > label.shape[0]:
x = label.shape[0] - window_size
else:
x = x+stride
return np.asarray(city)
cities=[]
print('train areas:')
for i in train_ids:
path=FOLDER+'top_mosaic_09cm_area{}.tif'.format(i)
im_name = os.path.basename(path)
ss = im_name[im_name.find('_area')+5:im_name.find('.tif')]
print(ss)
train_gt = io.imread(path)
xy_city = sliding_window(i, int(ss), train_ids, train_gt, patch_s, step)
cities.append(xy_city)
final_cities = np.concatenate(cities, axis=0)
df = pd.DataFrame({'X': list(final_cities[:,0]),
'Y': list(final_cities[:,1]),
'image_ID': list(final_cities[:,2]),
})
df.to_csv('../myxys_train.csv', index=False, columns=["X", "Y", "image_ID"])
#####################################################################
cities=[]
print('validation areas:')
for i in infer_ids: ##THIS
path=FOLDER+'top_mosaic_09cm_area{}.tif'.format(i)
im_name = os.path.basename(path)
ss = im_name[im_name.find('_area')+5:im_name.find('.tif')]
print(ss)
train_gt = io.imread(path)
xy_city = sliding_window(i, int(ss), infer_ids, train_gt, patch_s, patch_s)
cities.append(xy_city)
final_cities = np.concatenate(cities, axis=0)
df = pd.DataFrame({'X': list(final_cities[:,0]),
'Y': list(final_cities[:,1]),
'image_ID': list(final_cities[:,2]),
})
df.to_csv('../myxys_val.csv', index=False, columns=["X", "Y", "image_ID"])