-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathprocess_dibco.py
178 lines (133 loc) · 9.08 KB
/
process_dibco.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
import numpy as np
import os
import random
import cv2
from tqdm import tqdm
from config import Configs
def prepare_dibco_experiment(val_set,test_set, patches_size, overlap_size, patches_size_valid):
"""
Prepare the data for training
Args:
val_set (str): the vealidation dataset
the_set (str): the testing dataset
patches_size (int): patch size for training data
overlap_size (int): overlapping size between different patches (vertically and horizontally)
patches_size_valid (int): patch size for validation data
"""
folder = main_path+'DIBCOSETS/'
all_datasets = os.listdir(folder)
n_i = 1
for d_set in tqdm(all_datasets):
if d_set not in [val_set,test_set]:
# continue
for im in os.listdir(folder+d_set+'/imgs'):
img = cv2.imread(folder+d_set+'/imgs/'+im)
gt_img = cv2.imread(folder+d_set+'/gt_imgs/'+im)
for i in range (0,img.shape[0],overlap_size):
for j in range (0,img.shape[1],overlap_size):
if i+patches_size<=img.shape[0] and j+patches_size<=img.shape[1]:
p = img[i:i+patches_size,j:j+patches_size,:]
gt_p = gt_img[i:i+patches_size,j:j+patches_size,:]
elif i+patches_size>img.shape[0] and j+patches_size<=img.shape[1]:
p = (np.ones((patches_size,patches_size,3)) - random.randint(0,1) )*255
gt_p = np.ones((patches_size,patches_size,3)) *255
p[0:img.shape[0]-i,:,:] = img[i:img.shape[0],j:j+patches_size,:]
gt_p[0:img.shape[0]-i,:,:] = gt_img[i:img.shape[0],j:j+patches_size,:]
elif i+patches_size<=img.shape[0] and j+patches_size>img.shape[1]:
p = (np.ones((patches_size,patches_size,3)) - random.randint(0,1) )*255
gt_p = np.ones((patches_size,patches_size,3)) * 255
p[:,0:img.shape[1]-j,:] = img[i:i+patches_size,j:img.shape[1],:]
gt_p[:,0:img.shape[1]-j,:] = gt_img[i:i+patches_size,j:img.shape[1],:]
else:
p = (np.ones((patches_size,patches_size,3)) - random.randint(0,1) )*255
gt_p = np.ones((patches_size,patches_size,3)) * 255
p[0:img.shape[0]-i,0:img.shape[1]-j,:] = img[i:img.shape[0],j:img.shape[1],:]
gt_p[0:img.shape[0]-i,0:img.shape[1]-j,:] = gt_img[i:img.shape[0],j:img.shape[1],:]
cv2.imwrite(main_path+'train/'+str(n_i)+'.png',p)
cv2.imwrite(main_path+'train_gt/'+str(n_i)+'.png',gt_p)
n_i+=1
if d_set == test_set:
for im in os.listdir(folder+d_set+'/imgs'):
img = cv2.imread(folder+d_set+'/imgs/'+im)
gt_img = cv2.imread(folder+d_set+'/gt_imgs/'+im)
for i in range (0,img.shape[0],patches_size_valid):
for j in range (0,img.shape[1],patches_size_valid):
if i+patches_size_valid<=img.shape[0] and j+patches_size_valid<=img.shape[1]:
p = img[i:i+patches_size_valid,j:j+patches_size_valid,:]
gt_p = gt_img[i:i+patches_size_valid,j:j+patches_size_valid,:]
elif i+patches_size_valid>img.shape[0] and j+patches_size_valid<=img.shape[1]:
p = np.ones((patches_size_valid,patches_size_valid,3)) *255
gt_p = np.ones((patches_size_valid,patches_size_valid,3)) *255
p[0:img.shape[0]-i,:,:] = img[i:img.shape[0],j:j+patches_size_valid,:]
gt_p[0:img.shape[0]-i,:,:] = gt_img[i:img.shape[0],j:j+patches_size_valid,:]
elif i+patches_size_valid<=img.shape[0] and j+patches_size_valid>img.shape[1]:
p = np.ones((patches_size_valid,patches_size_valid,3)) * 255
gt_p = np.ones((patches_size_valid,patches_size_valid,3)) * 255
p[:,0:img.shape[1]-j,:] = img[i:i+patches_size_valid,j:img.shape[1],:]
gt_p[:,0:img.shape[1]-j,:] = gt_img[i:i+patches_size_valid,j:img.shape[1],:]
else:
p = np.ones((patches_size_valid,patches_size_valid,3)) * 255
gt_p = np.ones((patches_size_valid,patches_size_valid,3)) * 255
p[0:img.shape[0]-i,0:img.shape[1]-j,:] = img[i:img.shape[0],j:img.shape[1],:]
gt_p[0:img.shape[0]-i,0:img.shape[1]-j,:] = gt_img[i:img.shape[0],j:img.shape[1],:]
cv2.imwrite(main_path+'test/'+im.split('.')[0]+'_'+str(i)+'_'+str(j)+'.png',p)
cv2.imwrite(main_path+'test_gt/'+im.split('.')[0]+'_'+str(i)+'_'+str(j)+'.png',gt_p)
if d_set == val_set:
for im in os.listdir(folder+d_set+'/imgs'):
img = cv2.imread(folder+d_set+'/imgs/'+im)
gt_img = cv2.imread(folder+d_set+'/gt_imgs/'+im)
for i in range (0,img.shape[0],patches_size_valid):
for j in range (0,img.shape[1],patches_size_valid):
if i+patches_size_valid<=img.shape[0] and j+patches_size_valid<=img.shape[1]:
p = img[i:i+patches_size_valid,j:j+patches_size_valid,:]
gt_p = gt_img[i:i+patches_size_valid,j:j+patches_size_valid,:]
elif i+patches_size_valid>img.shape[0] and j+patches_size_valid<=img.shape[1]:
p = np.ones((patches_size_valid,patches_size_valid,3)) *255
gt_p = np.ones((patches_size_valid,patches_size_valid,3)) *255
p[0:img.shape[0]-i,:,:] = img[i:img.shape[0],j:j+patches_size_valid,:]
gt_p[0:img.shape[0]-i,:,:] = gt_img[i:img.shape[0],j:j+patches_size_valid,:]
elif i+patches_size_valid<=img.shape[0] and j+patches_size_valid>img.shape[1]:
p = np.ones((patches_size_valid,patches_size_valid,3)) * 255
gt_p = np.ones((patches_size_valid,patches_size_valid,3)) * 255
p[:,0:img.shape[1]-j,:] = img[i:i+patches_size_valid,j:img.shape[1],:]
gt_p[:,0:img.shape[1]-j,:] = gt_img[i:i+patches_size_valid,j:img.shape[1],:]
else:
p = np.ones((patches_size_valid,patches_size_valid,3)) * 255
gt_p = np.ones((patches_size_valid,patches_size_valid,3)) * 255
p[0:img.shape[0]-i,0:img.shape[1]-j,:] = img[i:img.shape[0],j:img.shape[1],:]
gt_p[0:img.shape[0]-i,0:img.shape[1]-j,:] = gt_img[i:img.shape[0],j:img.shape[1],:]
cv2.imwrite(main_path+'valid/'+im.split('.')[0]+'_'+str(i)+'_'+str(j)+'.png',p)
cv2.imwrite(main_path+'valid_gt/'+im.split('.')[0]+'_'+str(i)+'_'+str(j)+'.png',gt_p)
if __name__ == "__main__":
# get configs
cfg = Configs().parse()
main_path = cfg.data_path
validation_dataset = cfg.validation_dataset
testing_dataset = cfg.testing_dataset
patch_size = cfg.split_size
# augment the training data patch size to allow cropping augmentation later in data loader
p_size_train = (patch_size+128)
p_size_valid = patch_size
overlap_size = patch_size//2
# create train/val/test folders if theu are not existent
if not os.path.exists(main_path+'train/'):
os.makedirs(main_path+'train/')
if not os.path.exists(main_path+'train_gt/'):
os.makedirs(main_path+'train_gt/')
if not os.path.exists(main_path+'valid/'):
os.makedirs(main_path+'valid/')
if not os.path.exists(main_path+'valid_gt/'):
os.makedirs(main_path+'valid_gt/')
if not os.path.exists(main_path+'test/'):
os.makedirs(main_path+'test/')
if not os.path.exists(main_path+'test_gt/'):
os.makedirs(main_path+'test_gt/')
# remove old data if the folders exist
os.system('rm '+main_path+'train/*')
os.system('rm '+main_path+'train_gt/*')
os.system('rm '+main_path+'valid/*')
os.system('rm '+main_path+'valid_gt/*')
os.system('rm '+main_path+'test/*')
os.system('rm '+main_path+'test_gt/*')
# create your data...
prepare_dibco_experiment(validation_dataset, testing_dataset, p_size_train, overlap_size, p_size_valid)