|
a |
|
b/SegmentationCropping.py |
|
|
1 |
# %% importing packages |
|
|
2 |
|
|
|
3 |
import argparse |
|
|
4 |
from typing import NamedTuple |
|
|
5 |
import numpy as np |
|
|
6 |
import cv2 as cv |
|
|
7 |
import os |
|
|
8 |
import matplotlib.pyplot as plt |
|
|
9 |
import tqdm |
|
|
10 |
from natsort import natsorted |
|
|
11 |
from glob import glob |
|
|
12 |
import magic |
|
|
13 |
import re |
|
|
14 |
from PIL import Image |
|
|
15 |
|
|
|
16 |
plt.rcParams['figure.figsize'] = [50, 150] |
|
|
17 |
|
|
|
18 |
|
|
|
19 |
|
|
|
20 |
# %% Citations |
|
|
21 |
############################################################# |
|
|
22 |
############################################################# |
|
|
23 |
|
|
|
24 |
# You should try and make this a "select this directory and pad/process all the |
|
|
25 |
# files in it this way" script. You currently have WAY too much repetition |
|
|
26 |
# %% |
|
|
27 |
TissueChainID = '26/01/' |
|
|
28 |
cropping = 0 |
|
|
29 |
|
|
|
30 |
old_files = 0 |
|
|
31 |
|
|
|
32 |
fiduciary_files = 1 |
|
|
33 |
nodal_files = 1 |
|
|
34 |
seg_files = 1 |
|
|
35 |
high_res_files = 1 |
|
|
36 |
|
|
|
37 |
nodal_white = 1 |
|
|
38 |
fiduciary_white = 1 |
|
|
39 |
|
|
|
40 |
reduction_size = 4 |
|
|
41 |
|
|
|
42 |
if reduction_size == 4: |
|
|
43 |
reduction_name = 'QuarterScale' |
|
|
44 |
|
|
|
45 |
if reduction_size == 8: |
|
|
46 |
reduction_name = 'EighthScale' |
|
|
47 |
|
|
|
48 |
|
|
|
49 |
|
|
|
50 |
base_directory = '/var/confocaldata/HumanNodal/HeartData/'+ TissueChainID |
|
|
51 |
JPG_directory = '/var/confocaldata/HumanNodal/HeartData/'+ TissueChainID +'JPG/' |
|
|
52 |
jpg_file_names = glob(JPG_directory + '*.jpg') |
|
|
53 |
|
|
|
54 |
|
|
|
55 |
if old_files: |
|
|
56 |
ML_directory = '/var/confocaldata/HumanNodal/HeartData/'+ TissueChainID +'uNet_Segmentations/' |
|
|
57 |
Nodal_Seg_directory = '/var/confocaldata/HumanNodal/HeartData/'+ TissueChainID +'Segmentations/Nodal Segmentation FullScale_NoPad/' |
|
|
58 |
if fiduciary_files: |
|
|
59 |
Fiduciary_Seg_directory = '/var/confocaldata/HumanNodal/HeartData/'+ TissueChainID +'Segmentations/Fiduciary Segmentation FullScale_NoPad/' |
|
|
60 |
else: |
|
|
61 |
ML_directory = '/var/confocaldata/HumanNodal/HeartData/'+ TissueChainID +'uNet_Segmentations/' |
|
|
62 |
Nodal_Seg_directory = '/var/confocaldata/HumanNodal/HeartData/'+ TissueChainID +'Segmentations/Nodal Segmentation/' |
|
|
63 |
if fiduciary_files: |
|
|
64 |
Fiduciary_Seg_directory = '/var/confocaldata/HumanNodal/HeartData/'+ TissueChainID +'Segmentations/Fiduciary Segmentation/' |
|
|
65 |
|
|
|
66 |
high_res_directory = '/var/confocaldata/HumanNodal/HeartData/'+ TissueChainID +'HighResSeg/' |
|
|
67 |
|
|
|
68 |
os.chdir(ML_directory) |
|
|
69 |
jpg_file_names = natsorted(jpg_file_names) |
|
|
70 |
padding_size = 4000 # + 1536 |
|
|
71 |
|
|
|
72 |
# %% USE THIS SECTION FOR CROPPING THE SEGMENTATIONS AFTER THE UNET HAS AT IT |
|
|
73 |
if cropping: |
|
|
74 |
for idx in tqdm.tqdm(range(len(jpg_file_names))): |
|
|
75 |
|
|
|
76 |
out_directory = './../Cropped_uNet_Segmentations/' |
|
|
77 |
|
|
|
78 |
# create the directory for saving if it doesn't already exist |
|
|
79 |
if not os.path.isdir(out_directory): |
|
|
80 |
os.mkdir(out_directory) |
|
|
81 |
|
|
|
82 |
os.chdir(out_directory) |
|
|
83 |
|
|
|
84 |
jpg_file = jpg_file_names[idx] |
|
|
85 |
id = jpg_file.split('/')[-1].split('.')[0] |
|
|
86 |
id = id.split('_')[0] + '_' + id.split('_')[1] + '_' + id.split('_')[2] |
|
|
87 |
ml_file = glob(ML_directory + f'{id}_*.png')[0] |
|
|
88 |
|
|
|
89 |
jpg_image1 = cv.imread(jpg_file) |
|
|
90 |
ml_image1 = cv.imread(ml_file)[:,:,0] |
|
|
91 |
[x,y,z] = jpg_image1.shape |
|
|
92 |
|
|
|
93 |
cropped_ml1 = ml_image1[padding_size:padding_size+x, |
|
|
94 |
padding_size:padding_size+y] |
|
|
95 |
|
|
|
96 |
cv.imwrite( |
|
|
97 |
id + |
|
|
98 |
f'_CroppedSeg.png', |
|
|
99 |
cropped_ml1 |
|
|
100 |
) |
|
|
101 |
|
|
|
102 |
# %% |
|
|
103 |
ML_directory = '/var/confocaldata/HumanNodal/HeartData/'+ TissueChainID +'Cropped_uNet_Segmentations/' |
|
|
104 |
ml_file_names = glob(ML_directory + '*.png') |
|
|
105 |
all_image_sizes = [] |
|
|
106 |
for file_name in ml_file_names: |
|
|
107 |
header = magic.from_file(file_name) |
|
|
108 |
size = re.search('(\d+) x (\d+)',header).groups() |
|
|
109 |
sizes = [int(a) for a in size] |
|
|
110 |
all_image_sizes.append(sizes) |
|
|
111 |
|
|
|
112 |
max_width = np.max(np.asarray(all_image_sizes)[:,0]) |
|
|
113 |
max_height = np.max(np.asarray(all_image_sizes)[:,1]) |
|
|
114 |
|
|
|
115 |
idx = 200 |
|
|
116 |
additional_padding = 4000 |
|
|
117 |
os.chdir(JPG_directory) |
|
|
118 |
out_big_directory = base_directory + 'Padded_Images/' |
|
|
119 |
out_small_directory = base_directory + 'Padded_Images_' + reduction_name + '/' |
|
|
120 |
out_parent_list = [out_big_directory,out_small_directory] |
|
|
121 |
out_list = [] |
|
|
122 |
if not os.path.isdir(out_big_directory): |
|
|
123 |
os.mkdir(out_big_directory) |
|
|
124 |
|
|
|
125 |
if not os.path.isdir(out_small_directory): |
|
|
126 |
os.mkdir(out_small_directory) |
|
|
127 |
|
|
|
128 |
for idx, out_directory in enumerate(out_parent_list): |
|
|
129 |
jpg_out = out_directory + 'JPG' |
|
|
130 |
seg_out = out_directory + 'Seg' |
|
|
131 |
nodal_out = out_directory + 'Nodal' |
|
|
132 |
fiduciary_out = out_directory + 'Fiduciary' |
|
|
133 |
high_res_out = out_directory + 'HighRes' |
|
|
134 |
out_list.append([jpg_out,seg_out,nodal_out,high_res_out,fiduciary_out]) |
|
|
135 |
|
|
|
136 |
for directory in out_list[idx]: |
|
|
137 |
if not os.path.isdir(directory): |
|
|
138 |
os.mkdir(directory) |
|
|
139 |
|
|
|
140 |
|
|
|
141 |
|
|
|
142 |
for idx in tqdm.tqdm(range(len(jpg_file_names))): |
|
|
143 |
|
|
|
144 |
jpg_file = jpg_file_names[idx] |
|
|
145 |
id = jpg_file.split('/')[-1].split('.')[0] |
|
|
146 |
id = id.split('_')[0] + '_' + id.split('_')[1] + '_' + id.split('_')[2] |
|
|
147 |
|
|
|
148 |
|
|
|
149 |
|
|
|
150 |
# Create a separate section for the nodal tissue stuff, as it looks like |
|
|
151 |
# nodal segmentation will actually happen after the registration using the |
|
|
152 |
# segmentations |
|
|
153 |
# change this to _*.png if you are not using the FullScale_NoPad segmentations |
|
|
154 |
# will need to scale the segmentations for newer segmentations that haven't been |
|
|
155 |
# performed using previously padded images. This section is below, starting with |
|
|
156 |
|
|
|
157 |
|
|
|
158 |
jpg_image = cv.imread(jpg_file) |
|
|
159 |
[height,width,z] = jpg_image.shape |
|
|
160 |
|
|
|
161 |
height_diff = max_height - height |
|
|
162 |
width_diff = max_width - width |
|
|
163 |
|
|
|
164 |
if height_diff%2 == 1: |
|
|
165 |
pad_top = np.floor(height_diff/2) + additional_padding |
|
|
166 |
pad_bottom = np.floor(height_diff/2) + additional_padding |
|
|
167 |
pad_bottom += 1 |
|
|
168 |
else: |
|
|
169 |
pad_top = np.floor(height_diff/2) + additional_padding |
|
|
170 |
pad_bottom = np.floor(height_diff/2) + additional_padding |
|
|
171 |
|
|
|
172 |
if width_diff%2 == 1: |
|
|
173 |
pad_left = np.floor(width_diff/2) + additional_padding |
|
|
174 |
pad_right = np.floor(width_diff/2) + additional_padding |
|
|
175 |
pad_right += 1 |
|
|
176 |
else: |
|
|
177 |
pad_left = np.floor(width_diff/2) + additional_padding |
|
|
178 |
pad_right = np.floor(width_diff/2) + additional_padding |
|
|
179 |
|
|
|
180 |
padded_jpg = cv.copyMakeBorder(jpg_image, |
|
|
181 |
int(pad_top), |
|
|
182 |
int(pad_bottom), |
|
|
183 |
int(pad_left), |
|
|
184 |
int(pad_right), |
|
|
185 |
borderType=cv.cv2.BORDER_CONSTANT, |
|
|
186 |
value=[255,255,255]) |
|
|
187 |
|
|
|
188 |
os.chdir(out_list[0][0]) |
|
|
189 |
cv.imwrite( |
|
|
190 |
id + |
|
|
191 |
f'_Padded.png', |
|
|
192 |
padded_jpg |
|
|
193 |
) |
|
|
194 |
|
|
|
195 |
[pad_height,pad_width,z] = padded_jpg.shape |
|
|
196 |
|
|
|
197 |
|
|
|
198 |
width_small = int(pad_width/reduction_size) |
|
|
199 |
height_small = int(pad_height/reduction_size) |
|
|
200 |
jpg_small = cv.resize(padded_jpg,[width_small,height_small],cv.INTER_AREA) |
|
|
201 |
|
|
|
202 |
os.chdir(out_list[1][0]) |
|
|
203 |
cv.imwrite( |
|
|
204 |
id + |
|
|
205 |
f'_Padded_' + reduction_name + '.png', |
|
|
206 |
jpg_small |
|
|
207 |
) |
|
|
208 |
|
|
|
209 |
|
|
|
210 |
if seg_files: |
|
|
211 |
ml_file = glob(ML_directory + f'{id}_*.png')[0] |
|
|
212 |
ml_image = cv.imread(ml_file)[:,:,0] |
|
|
213 |
padded_seg = cv.copyMakeBorder(ml_image, |
|
|
214 |
int(pad_top), |
|
|
215 |
int(pad_bottom), |
|
|
216 |
int(pad_left), |
|
|
217 |
int(pad_right), |
|
|
218 |
borderType=cv.cv2.BORDER_CONSTANT, |
|
|
219 |
value=[0,0,0]) |
|
|
220 |
os.chdir(out_list[0][1]) |
|
|
221 |
cv.imwrite( |
|
|
222 |
id + |
|
|
223 |
f'_Padded_Seg.png', |
|
|
224 |
padded_seg |
|
|
225 |
) |
|
|
226 |
|
|
|
227 |
seg_small = np.array(Image.fromarray(padded_seg).resize((width_small,height_small), Image.NEAREST)) |
|
|
228 |
os.chdir(out_list[1][1]) |
|
|
229 |
cv.imwrite( |
|
|
230 |
id + |
|
|
231 |
f'_Padded_Seg_' + reduction_name + '.png', |
|
|
232 |
seg_small |
|
|
233 |
) |
|
|
234 |
|
|
|
235 |
|
|
|
236 |
if nodal_files: |
|
|
237 |
|
|
|
238 |
if old_files: |
|
|
239 |
nodal_file = glob(Nodal_Seg_directory + f'{id}-*.png')[0] |
|
|
240 |
else: |
|
|
241 |
nodal_file = glob(Nodal_Seg_directory + f'{id}_*.png')[0] |
|
|
242 |
|
|
|
243 |
nodal_image = cv.imread(nodal_file)[:,:,0] |
|
|
244 |
# be warry of this, you may need to use this later, though I'm not sure what |
|
|
245 |
# it was originally used for. |
|
|
246 |
if nodal_white: |
|
|
247 |
if sum(sum(nodal_image)) > 0: |
|
|
248 |
nodal_image = ~nodal_image |
|
|
249 |
# This accounts for the nodal segmentation images being a quarter the |
|
|
250 |
# original size, but you should make sure that you haven't already done the |
|
|
251 |
# fullscale noPad stuff yet |
|
|
252 |
if ~old_files: |
|
|
253 |
nodal_image = np.array(Image.fromarray(nodal_image).resize((width,height), Image.NEAREST)) |
|
|
254 |
|
|
|
255 |
padded_nodal = cv.copyMakeBorder(nodal_image, |
|
|
256 |
int(pad_top), |
|
|
257 |
int(pad_bottom), |
|
|
258 |
int(pad_left), |
|
|
259 |
int(pad_right), |
|
|
260 |
borderType=cv.cv2.BORDER_CONSTANT, |
|
|
261 |
value=[0,0,0]) |
|
|
262 |
|
|
|
263 |
os.chdir(out_list[0][2]) |
|
|
264 |
cv.imwrite( |
|
|
265 |
id + |
|
|
266 |
f'_Padded_Nodal.png', |
|
|
267 |
padded_nodal |
|
|
268 |
) |
|
|
269 |
|
|
|
270 |
nodal_small = np.array(Image.fromarray(padded_nodal).resize((width_small,height_small), Image.NEAREST)) |
|
|
271 |
os.chdir(out_list[1][2]) |
|
|
272 |
cv.imwrite( |
|
|
273 |
id + |
|
|
274 |
f'_Padded_Nodal_' + reduction_name + '.png', |
|
|
275 |
nodal_small |
|
|
276 |
) |
|
|
277 |
|
|
|
278 |
|
|
|
279 |
|
|
|
280 |
|
|
|
281 |
|
|
|
282 |
if high_res_files: |
|
|
283 |
high_res_file = glob(high_res_directory + f'{id}_*.png')[0] |
|
|
284 |
high_res_image = cv.imread(high_res_file)[:,:,0] |
|
|
285 |
|
|
|
286 |
padded_high_res = cv.copyMakeBorder(high_res_image, |
|
|
287 |
int(pad_top), |
|
|
288 |
int(pad_bottom), |
|
|
289 |
int(pad_left), |
|
|
290 |
int(pad_right), |
|
|
291 |
borderType=cv.cv2.BORDER_CONSTANT, |
|
|
292 |
value=[0,0,0]) |
|
|
293 |
|
|
|
294 |
|
|
|
295 |
os.chdir(out_list[0][3]) |
|
|
296 |
cv.imwrite( |
|
|
297 |
id + |
|
|
298 |
f'_Padded_HighRes.png', |
|
|
299 |
padded_high_res |
|
|
300 |
) |
|
|
301 |
|
|
|
302 |
high_res_small = np.array(Image.fromarray(padded_high_res).resize((width_small,height_small), Image.NEAREST)) |
|
|
303 |
os.chdir(out_list[1][3]) |
|
|
304 |
cv.imwrite( |
|
|
305 |
id + |
|
|
306 |
f'_Padded_HighRes_' + reduction_name + '.png', |
|
|
307 |
high_res_small |
|
|
308 |
) |
|
|
309 |
|
|
|
310 |
|
|
|
311 |
|
|
|
312 |
|
|
|
313 |
|
|
|
314 |
if fiduciary_files: |
|
|
315 |
if old_files: |
|
|
316 |
fiduciary_file = glob(Fiduciary_Seg_directory + f'{id}-*.png')[0] |
|
|
317 |
else: |
|
|
318 |
fiduciary_file = glob(Fiduciary_Seg_directory + f'{id}_*.png')[0] |
|
|
319 |
|
|
|
320 |
fiduciary_image = cv.imread(fiduciary_file)[:,:,0] |
|
|
321 |
|
|
|
322 |
if fiduciary_white: |
|
|
323 |
if sum(sum(fiduciary_image)) > 0: |
|
|
324 |
fiduciary_image = ~fiduciary_image |
|
|
325 |
|
|
|
326 |
if ~old_files: |
|
|
327 |
fiduciary_image = np.array(Image.fromarray(fiduciary_image).resize((width,height), Image.NEAREST)) |
|
|
328 |
|
|
|
329 |
padded_fiduciary = cv.copyMakeBorder(fiduciary_image, |
|
|
330 |
int(pad_top), |
|
|
331 |
int(pad_bottom), |
|
|
332 |
int(pad_left), |
|
|
333 |
int(pad_right), |
|
|
334 |
borderType=cv.cv2.BORDER_CONSTANT, |
|
|
335 |
value=[0,0,0]) |
|
|
336 |
os.chdir(out_list[0][4]) |
|
|
337 |
cv.imwrite( |
|
|
338 |
id + |
|
|
339 |
f'_Padded_Fiduciary.png', |
|
|
340 |
padded_fiduciary |
|
|
341 |
) |
|
|
342 |
|
|
|
343 |
fiduciary_small = np.array(Image.fromarray(padded_fiduciary).resize((width_small,height_small), Image.NEAREST)) |
|
|
344 |
os.chdir(out_list[1][4]) |
|
|
345 |
cv.imwrite( |
|
|
346 |
id + |
|
|
347 |
f'_Padded_Fiduciary_' + reduction_name + '.png', |
|
|
348 |
fiduciary_small |
|
|
349 |
) |
|
|
350 |
|
|
|
351 |
# %% |