-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdata_processing.py
More file actions
313 lines (231 loc) · 9.53 KB
/
data_processing.py
File metadata and controls
313 lines (231 loc) · 9.53 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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# -*- coding: utf-8 -*-
"""
Created on Tue Nov 15 14:36:51 2022
@author: has081
Read and decode datasets
Three datasets are available CHASEDB1, DRIVE, and STARE
All images will be saved in dataframe with structure
NAME, DATASET_NAME, PATH_TO_ORIGINAL_IMAGE, MASK
mulptiple masks will be stored again under same name
"""
# imports
import cv2
import pandas as pd
import os
import numpy as np
from matplotlib import pyplot as plt
from PIL import Image
import argparse
def save_fig(fig_id, tight_layout=True, fig_extension="png", resolution=300):
# save images from matplotlib
path = os.path.join(IMAGES_PATH, fig_id + "." + fig_extension)
print("Saving figure", fig_id)
if tight_layout:
plt.tight_layout()
plt.savefig(path, format=fig_extension, dpi=resolution)
def plot_fig(path):
colormap1 = cv2.imread(path)
img = cv2.cvtColor(colormap1, cv2.COLOR_BGR2RGB)
# save_fig(name)
plt.figure()
plt.imshow(img)
plt.title(path.split('\\')[-1])
def plot_orig_mask(df, name):
# function to print original image with masks
df_loc = df[df['NAME'] == name]
orig_img = cv2.imread(df_loc.PATH_TO_ORIGINAL_IMAGE.unique()[0],cv2.IMREAD_UNCHANGED)
orig_img = cv2.cvtColor(orig_img, cv2.COLOR_BGR2RGB)
# number of figures depends on number of masks
fig = plt.figure(figsize=( 6*len(df_loc), 6))
fig.subplots_adjust()
for i in range(len(df_loc) + 1):
ax = fig.add_subplot(1, 3, i + 1)
if i == 0: # print ortiginal image
ax.imshow(orig_img)
ax.title.set_text(name)
else:
# masks
path = df_loc.MASK.iloc[i-1]
# check .gif due to DRIVE database
if path[-3:] == 'gif':
im = Image.open(path)
mask_img = np.array(im)
else: # readable by OpenCV
mask_img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
ax.imshow(mask_img, cmap='gray')
ax.title.set_text(path.split('\\')[-1])
fig = ax.get_figure()
fig.tight_layout()
def path_replace_HFR(line):
# change file extension
new_line = line[:-4]
new_line = new_line + '.tif'
path, base = os.path.split(new_line)
parent_path = os.path.dirname(path)
return os.path.join(parent_path,'manual1',base)
"""
os functions
"""
def get_files(path, endswith=".jpg"):
# load images
files_original = []
images_stack = []
images_folders = []
# find only .jpg
for r, d, f in os.walk(path):
# sorting files alphabetically for Unix-like systems
for file in sorted(f):
if file.endswith(endswith):
#print(os.path.join(r, file))
files_original.append(os.path.join(r, file))
# stack names
images_stack.append(file)
images_folders.append(r)
return files_original, images_stack, images_folders
# print the path to dataset
class VerboseStoreDataset(argparse.Action):
def __init__(self, option_strings, dest, nargs=None, **kwargs):
if nargs is not None:
raise ValueError('nargs not allowed')
super(VerboseStoreDataset, self).__init__(option_strings, dest, **kwargs)
def __call__(self, parser, namespace, values, option_string=None):
print('Dataset saving, name %r defined by user' % (values))
setattr(namespace, self.dest, values)
#################################
if __name__ == "__main__":
prog_desc = "Load data from datasets folder \n and creates csv with path to image and label"
parser = argparse.ArgumentParser(description=prog_desc)
parser.version = '1.2'
# Add the arguments
parser.add_argument('--path',
#metavar='path',
action='store',
type=str,
default=os.getcwd(),
help='the path to dataset, datased included. Default=current dir',
required=False)
parser.add_argument('-b',
'--save_hrf',
action='store_true',
help='include HFR dataset 3504 x 2336.csv')
parser.add_argument('-s',
'--save',
action='store_true',
help='store the dataset in data_paths.csv')
parser.add_argument('-c',
'--csv',
type=str,
action=VerboseStoreDataset,
help='store dataset in user defined *name*.csv ')
args = parser.parse_args()
# set path to datasets
# PATHCWD = 'D:\\ARG@CS.FEI.VSB Dropbox\\Martin Hasal\\Dataset - retiny\\images_from_doctor\\ML\\SEGMENTATION\\VESSELS\\datasets'
PATHCWD = os.path.join(args.path, 'datasets')
PATH_CHASEDB1 = os.path.join(PATHCWD, 'CHASEDB1')
PATH_DRIVE = os.path.join(PATHCWD, 'DRIVE')
PATH_STARE = os.path.join(PATHCWD, 'STARE', 'vessels')
PATH_HRF = os.path.join(PATHCWD, 'HRF')
""" Images functions """
# Where to save the figures
PROJECT_ROOT_DIR = "."
CHAPTER_ID = "saved_images"
IMAGES_PATH = os.path.join(PATHCWD, CHAPTER_ID)
os.makedirs(IMAGES_PATH, exist_ok=True)
"""
CHASEDB1 DATASASET --- all images in single folder
"""
paths, names, _ = get_files(PATH_CHASEDB1)
##### important model, here the df is created
df_chase = pd.DataFrame({
'NAME': [name[:-4] for name in names] ,
'DATASET_NAME': ['CHASEDB1']*len(names),
'PATH_TO_ORIGINAL_IMAGE': paths
})
# because there are two mask for every image, df is doubled
df_chase = pd.DataFrame(np.repeat(df_chase.values, 2, axis=0), columns=df_chase.columns)
# get masks, in .png
paths, names, _ = get_files(PATH_CHASEDB1, endswith=".png")
df_chase['MASK'] = paths
df = df_chase
#for i in range(10):
# plot_fig(df.iloc[i,2])
#plot_orig_mask(df, df.NAME.iloc[10])
"""
CHASEDB1 DRIVE --- all images in single folder
https://www.kaggle.com/datasets/andrewmvd/drive-digital-retinal-images-for-vessel-extraction
"""
paths, names, _ = get_files(os.path.join(PATH_DRIVE, 'training'), endswith=".tif")
df_drive = pd.DataFrame({
'NAME': [name[:-4] for name in names] ,
'DATASET_NAME': ['DRIVE']*len(names),
'PATH_TO_ORIGINAL_IMAGE': paths
})
paths, names, _ = get_files(os.path.join(PATH_DRIVE,'training','1st_manual'), endswith=".gif")
df_drive['MASK'] = paths
# concatenate
df = pd.concat([df, df_drive])
df_drive
# plot_fig(df.iloc[70,3]), problem to read .gif
#plot_orig_mask(df, df.NAME.iloc[70])
"""
STARE --- .ppm images
"""
paths, names, _ = get_files(os.path.join(PATH_STARE,'stare-images'), endswith=".ppm")
# for path in paths[:20]:
# img = cv2.imread(path, cv2.IMREAD_UNCHANGED)
# img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# plt.figure()
# plt.imshow(img)
# plt.show()
df_stare1 = pd.DataFrame({
'NAME': [name[:-4] for name in names],
'DATASET_NAME': ['STARE']*len(names),
'PATH_TO_ORIGINAL_IMAGE': paths
})
paths, names, _ = get_files(os.path.join(PATH_STARE,'labels-ah'), endswith=".ppm")
df_stare1['MASK'] = paths
paths, names, _ = get_files(os.path.join(PATH_STARE,'stare-images'), endswith=".ppm")
df_stare2 = pd.DataFrame({
'NAME': [name[:-4] for name in names],
'DATASET_NAME': ['STARE']*len(names),
'PATH_TO_ORIGINAL_IMAGE': paths
})
paths, names, _ = get_files(os.path.join(PATH_STARE, 'labels-vk'), endswith=".ppm")
df_stare2['MASK'] = paths
df_stare = pd.concat([df_stare1, df_stare2])
del df_stare1
del df_stare2
df = pd.concat([df, df_stare])
"""
HRF --- .ppm images
"""
if (args.save_hrf):
paths, names, _ = get_files(os.path.join(PATH_HRF,'images'), endswith=".JPG")
df_hfr_JPG = pd.DataFrame({
'NAME': [name[:-4] for name in names],
'DATASET_NAME': ['HFR']*len(names),
'PATH_TO_ORIGINAL_IMAGE': paths,
'MASK' : paths
})
paths, names, _ = get_files(os.path.join(PATH_HRF,'images'), endswith=".jpg")
df_hfr_jpg = pd.DataFrame({
'NAME': [name[:-4] for name in names],
'DATASET_NAME': ['HFR']*len(names),
'PATH_TO_ORIGINAL_IMAGE': paths,
'MASK' : paths
})
df_hfr = pd.concat([df_hfr_JPG, df_hfr_jpg])
# set right path to mask
df_hfr['MASK'] = df_hfr.MASK.apply(path_replace_HFR)
del df_hfr_JPG
del df_hfr_jpg
df = pd.concat([df, df_hfr])
# SAVE PATHS
#plot_orig_mask(df, df.NAME.iloc[110])
print(df.DATASET_NAME.value_counts())
# save paths
if (args.csv or args.save):
if args.csv:
df.reset_index().to_csv(args.csv, index=False)
else:
df.reset_index().to_csv('data_paths.csv', index=False)