-
Notifications
You must be signed in to change notification settings - Fork 2
/
data_find.py
215 lines (186 loc) · 8.17 KB
/
data_find.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
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
import os
import json
import random
import sys
import glob
import xlrd
import math
from tqdm import tqdm
from random import shuffle
import numpy as np
from PIL import Image, ImageChops
from PIL import ImageOps
from skimage.feature import hog
from skimage import io, transform, color
from skimage.filters import gaussian
parser = argparse.ArgumentParser(description='manual to this script')
parser.add_argument("--dataset", type=str, default='IDRiD', help='[IDRiD/ADAM]')
parser.add_argument("--path", type=str, default='./dataset/', help='data dir')
def IDRiD_hog_generate(data_dir):
path_list = glob.glob(data_dir+'*/*/*.jpg')
path_list.sort()
for i, file in enumerate(path_list):
name = file.split('/IDRiD/')
file_name = name[0] + '/IDRiD_hog/' + name[1][:-4]
image = Image.open(file).convert('L')
image = image.resize((768,768))
fd4, hog_image4 = hog(image, orientations=9, pixels_per_cell=(4, 4), cells_per_block=(1, 1), block_norm='L2',
visualize=True, feature_vector=False)
fd8, hog_image8 = hog(image, orientations=9, pixels_per_cell=(8, 8), cells_per_block=(1, 1), block_norm='L2',
visualize=True, feature_vector=False)
fd16, hog_image16 = hog(image, orientations=9, pixels_per_cell=(16, 16), cells_per_block=(1, 1), block_norm='L2',
visualize=True, feature_vector=False)
io.imsave(file_name+'_4.png', hog_image4)
io.imsave(file_name+'_8.png', hog_image8)
io.imsave(file_name+'_16.png', hog_image16)
def IDRiD_hog_process(data_dir, label_save_dir):
if not os.path.exists(label_save_dir):
os.makedirs(label_save_dir)
train_json = []
test_json = []
file_train = open(label_save_dir+'train_label.json', 'w')
file_test = open(label_save_dir+'test_label.json', 'w')
path_list = glob.glob(data_dir+'*/*/*.png')
path_list.sort()
print (len(path_list))
for i, file in enumerate(path_list):
name = file.split('/IDRiD/')
patch = name[1][:-4].split('_')
file_name = name[0] + '/IDRiD_hog_patch/'+patch[0]+'_'+patch[1]
num = patch[-1]
if '/train/normal' in file:
dict = {}
dict['image_path'] = file
dict['image_hog_path'] = file_name +'_8_'+num+'.png'
dict['image_normal_or_abormal_label'] = 0
dict['image_ID'] = str(i)
train_json.append(dict)
dict = {}
dict['image_path'] = file
dict['image_hog_path'] = file_name +'_4_'+num+'.png'
dict['image_normal_or_abormal_label'] = 0
dict['image_ID'] = str(i)
train_json.append(dict)
dict = {}
dict['image_path'] = file
dict['image_hog_path'] = file_name +'_16_'+num+'.png'
dict['image_normal_or_abormal_label'] = 0
dict['image_ID'] = str(i)
train_json.append(dict)
if '/test/abnormal/' in file:
file_name_ = name[1].split('test/abnormal/')
file_name_roi = name[0] + '/IDRiD/label/' + file_name_[1][:-4]
dict = {}
dict['image_path'] = file
dict['image_ID'] = str(i)
dict['image_hog_path'] = file_name +'_4_'+num+'.png'
dict['image_ROI'] = file_name_roi + '.png'
test_json.append(dict)
print ('train: ', len(train_json))
print ('test: ', len(test_json))
json.dump(train_json, file_train, indent=4)
file_train.close()
json.dump(test_json, file_test, indent=4)
file_test.close()
def ADAM_hog_generate(data_dir):
path_list = os.listdir(data_dir+'/all/')
list_ = ['drusen/','exudate/','hemorrhage/','others/','scar/']
abnormal_list = []
for name in list_:
path_list2 = os.listdir(data_dir+'/Training400-Lesion/Lesion_Masks/'+name)
for p in path_list2:
if p not in abnormal_list:
abnormal_list.append(p[:-4]+'.jpg')
for file in path_list:
if file not in abnormal_list:
file_name = data_dir+'/ADAM_hog/train/' + file[:-4]
if file in abnormal_list:
file_name = data_dir+'/ADAM_hog/test/' + file[:-4]
image = Image.open(data_dir+'/all/'+file).convert('L')
image = image.resize((768,768))
fd4, hog_image4 = hog(image, orientations=9, pixels_per_cell=(4, 4), cells_per_block=(1, 1), block_norm='L2',
visualize=True, feature_vector=False)
fd8, hog_image8 = hog(image, orientations=9, pixels_per_cell=(8, 8), cells_per_block=(1, 1), block_norm='L2',
visualize=True, feature_vector=False)
fd16, hog_image16 = hog(image, orientations=9, pixels_per_cell=(16, 16), cells_per_block=(1, 1), block_norm='L2',
visualize=True, feature_vector=False)
io.imsave(file_name+'_4.png', hog_image4)
io.imsave(file_name+'_8.png', hog_image8)
io.imsave(file_name+'_16.png', hog_image16)
def hog_split(dir, dataset='IDRiD'):
path_list = glob.glob(dir+'*/*/*.png')
for file in path_list:
name = file.split('/'+str(dataset)+'/')
file_ = name[0] + '/'+str(dataset)+'_hog/' + name[1]
image = Image.open(file_).convert('L')
size = image.size
w = int(size[0] // 3)
h = int(size[1] // 3)
file_name = name[0]+'/'+str(dataset)+'_hog_patch/'+name[1][:-4]
flag = 1
for j in range(3):
for i in range (3):
box = (w*i, h*j, w*(i+1), h*(j+1))
region = image.crop(box)
region.save(file_name+'_'+str(flag)+'.png')
flag = flag+1
def ADAM_hog_process(data_dir, label_save_dir):
if not os.path.exists(label_save_dir):
os.makedirs(label_save_dir)
train_json = []
test_json = []
file_train = open(label_save_dir+'train_label.json', 'w')
file_test = open(label_save_dir+'test_label.json', 'w')
path_list = glob.glob(data_dir+'*/*.png')
path_list.sort()
print (len(path_list))
for i, file in enumerate(path_list):
name = file.split('/ADAM/')
patch = name[1][:-4].split('_')
file_name = name[0] + '/ADAM_hog/'+patch[0]
num = patch[-1]
if '/train/' in file:
dict = {}
dict['image_path'] = file
dict['image_hog_path'] = file_name +'_8_'+num+'.png'
dict['image_normal_or_abormal_label'] = 0
dict['image_ID'] = str(i)
train_json.append(dict)
dict = {}
dict['image_path'] = file
dict['image_hog_path'] = file_name +'_4_'+num+'.png'
dict['image_normal_or_abormal_label'] = 0
dict['image_ID'] = str(i)
train_json.append(dict)
dict = {}
dict['image_path'] = file
dict['image_hog_path'] = file_name +'_16_'+num+'.png'
dict['image_normal_or_abormal_label'] = 0
dict['image_ID'] = str(i)
train_json.append(dict)
if '/test/' in file:
file_name_ = name[1].split('/')
file_name_roi = name[0] + '/ADAM/label/' + file_name_[1][:-4]
dict = {}
dict['image_path'] = file
dict['image_ID'] = str(i)
dict['image_hog_path'] = file_name +'_4_'+num+'.png'
dict['image_ROI'] = file_name_roi + '.png'
test_json.append(dict)
print ('train: ', len(train_json))
print ('test: ', len(test_json))
json.dump(train_json, file_train, indent=4)
file_train.close()
json.dump(test_json, file_test, indent=4)
file_test.close()
if __name__=="__main__":
args = parser.parse_args()
data_dir = os.path.join(args.path,args.dataset)
if 'IDRiD' in data_dir:
IDRiD_hog_generate(data_dir)
hog_split(data_dir, 'IDRiD')
IDRiD_hog_process(data_dir, './labels/IDRiD/')
if 'ADAM' in data_dir:
ADAM_hog_generate(data_dir)
hog_split(data_dir,'ADAM')
ADAM_hog_process(data_dir, './labels/ADAM/')