-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtile_images.py
315 lines (242 loc) · 9.36 KB
/
tile_images.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
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
314
315
# -*- coding: utf-8 -*-
"""
Created on Wed Sept 19 2018
@author: eric
"""
import os
import glob
import numpy as np
import sys
import cv2
import argparse
def aspect_resize(im, ii=226):
"""
Resizes an image and preserves the aspect according to largest dimension
:param im: input array
:param ii: desired dimension of the output. Assumes square output image
:return out: resized square image array
"""
cen = np.floor(np.array((ii, ii))/2.0).astype('int')
dim = im.shape[0:2]
if dim[0] != dim[1]:
# get the largest dimension
large_dim = max(dim)
# ratio between the large dimension and required dimension
rat = float(ii)/large_dim
# get the smaller dimension that maintains the aspect ratio
small_dim = int(min(dim)*rat)
# get the indicies of the large and small dimensions
large_ind = dim.index(max(dim))
small_ind = dim.index(min(dim))
dim = list(dim)
# the dimension assigment may seem weird cause of how python indexes images
dim[small_ind] = ii
dim[large_ind] = small_dim
dim = tuple(dim)
im = cv2.resize(im, dim)
half = np.floor(np.array(im.shape[0:2])/2.0).astype('int')
# make an empty array, and place the new image in the middle
res = np.zeros((ii, ii, 3), dtype='uint8')
if large_ind == 1:
test = res[cen[0]-half[0]:cen[0]+half[0], cen[1]-half[1]:cen[1]+half[1]+1]
if test.shape != im.shape:
res[cen[0]-half[0]:cen[0]+half[0]+1, cen[1]-half[1]:cen[1]+half[1]+1] = im
else:
res[cen[0]-half[0]:cen[0]+half[0], cen[1]-half[1]:cen[1]+half[1]+1] = im
else:
test = res[cen[0]-half[0]:cen[0]+half[0]+1, cen[1]-half[1]:cen[1]+half[1]]
if test.shape != im.shape:
res[cen[0]-half[0]:cen[0]+half[0]+1, cen[1]-half[1]:cen[1]+half[1]+1] = im
else:
res[cen[0]-half[0]:cen[0]+half[0]+1, cen[1]-half[1]:cen[1]+half[1]] = im
else:
res = cv2.resize(im, (ii, ii))
return res
def get_rand_ims(cls, num=200):
"""
takes a list of classes and randomly selects images from random classes on
the day specified
:param cls: input list of files pointing to classes to consider
:param num: number of images to select
"""
# placeholder and output list
out = []
count = 0
# select random images until the number has been reached
while count < num:
ind = np.random.randint(0, len(cls)) # select random index
in_cls = cls[ind]
imgs = glob.glob(os.path.join(in_cls, '*.jpg'))
np.random.shuffle(imgs)
check = True # set bool to check if the image has been selected
flag = 0
while check:
try:
img = imgs[flag]
if img in out:
flag += 1
else:
out.append(img)
check = False
count += 1
except IndexError:
print('sampled all ' + str(flag) + ' of ' + os.path.basename(in_cls))
cls.remove(in_cls)
check = False
return out
def tile_images(images, tile_dim, resize=128):
"""
takes a list of images and tiles them
:param images: input list of image paths
:param tile_dim: number to tile in each dimension [hh x ww] as int
:param resize: size to resize the input images
:return:
"""
out = np.zeros((resize*tile_dim[0], resize*tile_dim[1], 3))
out = out.astype(np.uint8)
for idx, img in enumerate(images):
ii = idx % tile_dim[1]
jj = idx // tile_dim[1]
im_in = cv2.imread(img)
im_out = aspect_resize(im_in, resize)
out[jj*resize:jj*resize+resize, ii*resize:ii*resize+resize, :] = im_out
return out
if __name__ == "__main__":
# where you run from /home/ptvradmin/machine_learning
cwd = os.getcwd()
# deterimine how to run and switch if necessary
if os.path.exists(sys.argv[2]):
# check if the second argument is a file
# what classifier output to work on
clf = sys.argv[1]
# get the days to work on from file
days = np.genfromtxt(sys.argv[2], dtype=int)
# parse the class from the file name with list of days
lab = os.path.basename(sys.argv[2]).split('_')[0]
# where to save the finished mosaics
out_path = sys.argv[3]
# number of images per days
num_samples = int(sys.argv[4])
# iterate over the days and append paths to list
out_imgs = []
np.random.shuffle(days)
# keep track of the total and generate a list of the days used
date_info = []
for day in days:
# get the file paths
ptf = os.path.join(cwd,
clf,
'Any',
str(day),
'labeled_images',
lab
)
imgs = glob.glob(os.path.join(ptf, '*.jpg'))
# shuffle and select the images
np.random.shuffle(imgs)
if len(imgs) < num_samples:
out_imgs.extend(imgs)
else:
out_imgs.extend(imgs[0:num_samples])
# retain info about the number of images
date_info.append([day, len(imgs)])
# check if there are more than the deisred number
if len(out_imgs) > 200:
break
# generate the mosaic
print(len(out_imgs))
np.random.shuffle(out_imgs)
mos = tile_images(out_imgs[0:200], [num_samples, 20])
# save the image and information about the number of ROIs/day
out_mos = os.path.join(out_path, lab+'_hyb_samp_mosaic.png')
out_info = os.path.join(out_path,lab+'_num_hyb_per_day.txt')
cv2.imwrite(out_mos, mos)
date_info = np.asarray(date_info)
np.savetxt(out_info, date_info, fmt='%i', delimiter=',')
elif sys.argv[2] == 'except':
# what classifier output to work on
clf = sys.argv[1]
# what day to work on
day = sys.argv[3]
# category to ignore
lab = sys.argv[4]
# where to save the finished mosaics
out_path = sys.argv[5]
# path to the file
ptf = os.path.join(cwd,
clf,
'Any',
day,
'labeled_images'
)
# all classes
process = glob.glob(os.path.join(ptf, '*'))
# remove the identified class
consider = [line for line in process if os.path.basename(line) != lab]
# make the mosaic
imgs = get_rand_ims(consider)
mos = tile_images(imgs, [10, 20])
out_str = os.path.join(out_path,
day + '_except_' + lab + '_mosaic.png')
cv2.imwrite(out_str, mos)
else:
# what classifier output to work on
clf = sys.argv[1]
# what day to work on
day = sys.argv[2]
# category to work on ['phyto' produces a mosaic of phyto class,
# 'noise' produces a mosaic of noise. A particular class makes a mosaic of
# just that class]
lab = sys.argv[3]
# where to save the finished mosaics
out_path = sys.argv[4]
if lab != 'phyto' and lab != 'noise':
# get the path to the file
ptf = os.path.join(cwd,
clf,
'Any',
day,
'labeled_images',
lab
)
imgs = glob.glob(os.path.join(ptf, '*.jpg'))
# randomly shuffle and select the number of samples
num_samples = 200
np.random.shuffle(imgs)
imgs = imgs[0:num_samples]
mos = tile_images(imgs, [10, 20])
out_str = os.path.join(out_path,
day + '_' + lab + '_mosaic.png')
cv2.imwrite(out_str, mos)
else:
# make the file path to the images
ptf = os.path.join(cwd,
clf,
'Any',
day,
'labeled_images'
)
process = glob.glob(os.path.join(ptf, '*'))
# phyto and noise classes as of 092018
consid = ['Akashiwo', 'Ceratium furca', 'Ceratium fusus', 'Chain 01', 'Ciliate 01', 'Cochlodinium',
'Lingulodinium', 'Nauplius', 'Polykrikos', 'Prorocentrum', 'Prorocentrum Skinny', 'Protoperidinium sp',
'Spear 01']
if lab == 'phyto':
mos_lab = [line for line in process if os.path.basename(line) in consid]
else:
mos_lab = [line for line in process if os.path.basename(line) not in consid]
# number of samples to draw from each class
num_samples = 20
img_out = np.zeros((128*10, 128*2*len(mos_lab), 3))
img_out = img_out.astype(np.uint8)
flag = 0
for proc in mos_lab:
imgs = glob.glob(os.path.join(proc, '*.jpg'))
np.random.shuffle(imgs)
imgs = imgs[0:num_samples]
temp = tile_images(imgs, [10, 2])
img_out[:, 128*2*flag:128*2*flag+128*2, :] = temp
flag += 1
out_str = os.path.join(out_path,
day + '_' + lab + '_mosaic.png')
cv2.imwrite(out_str, img_out)