[7c5f70]: / Utilities / show_segmentations.py

Download this file

417 lines (331 with data), 13.1 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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
import os
import nibabel as nib
import numpy as np
from overlay_ims import overlay_image, save_overlay_im
from PIL import Image
def make_montage(sname_ims, sname_mont='montage.png', crop=None):
"""
Make images
Args:
sname_ims (list): list of image names
sname_mont (str): name of output file
crop (None or list of length 4): Crop coordinates (x, y, x_width, y_width)
Returns:
"""
rows = len(sname_ims)
y = 0
for i in range(rows):
x = 0
# Load first row of images
im = Image.open(sname_ims[i])
msk = Image.open(sname_msks[i])
seg = Image.open(sname_segs[i])
diff = Image.open(sname_diffs[i])
# Apply crops
if crop:
bbox = (crop[0], crop[1], im.width + crop[2], im.height + crop[3])
im = im.crop(bbox)
msk = msk.crop(bbox)
seg = seg.crop(bbox)
diff = diff.crop(bbox)
print(im.height)
if i == 0:
mont = Image.new('RGB', size=(im.width * 4, im.height * rows))
mont.paste(im, (x, y))
x += im.width
mont.paste(msk, (x, y))
x += im.width
mont.paste(seg, (x, y))
x += im.width
mont.paste(diff, (x, y))
# Increment row
y += im.height
# Save montage image
sname_mont = os.path.join(save_path, sname_mont)
mont.save(sname_mont)
def make_montage_init(sname_ims, sname_mont='montage.png'):
"""
Make a montage with one row
Args:
sname_ims (str): montage save name
sname_mont (list): lits of image files
Returns:
"""
# Make montage
cols = len(sname_ims)
x = 0
y = 0
for i in range(cols):
# Load first row of images
im = Image.open(sname_ims[i])
if i == 0:
mont = Image.new('RGB', size=(im.width * cols, im.height))
mont.paste(im, (x, y))
x += im.width
# Save montage image
sname_mont = os.path.join(save_path, sname_mont)
mont.save(sname_mont)
def load_label_show(files, sz):
"""
Load and prep label file
Args:
files (str): label filename
sz (list of length 2): size of MR image (may not always match label)
Returns:
"""
from skimage.transform import resize
from scipy.ndimage.morphology import binary_closing
# Load image
label = nib.load(files).get_data().astype('float32').squeeze()
# Resize image
label /= label.max()
label = resize(label, (sz[0], sz[1]))
# Make binary
label[label < 0.5] = 0
label[label > 0.5] = 1
# Close the mask
# label = binary_closing(label, structure=np.ones(shape=(3, 3, 3)))
return 255 * label.astype(np.uint8)
"""
Goal: remake segmentation images to show a control and a PD1 animal before and after segmentation.
"""
# Set up save paths
save_path = '/media/matt/Seagate Expansion Drive/b7TData_19/b7TData/Results/Analysis/Segmentation_images'
snames = [
['control_1',
'control_2'],
['pd1_1',
'pd1_2']
]
# Data paths
image_files = [
['/media/matt/Seagate Expansion Drive/b7TData_19/b7TData/Results/K520552/20180510/T2_cor.nii.gz',
'/media/matt/Seagate Expansion Drive/b7TData_19/b7TData/Results/K520552/20180517/T2_cor.nii.gz'], # Control
['/media/matt/Seagate Expansion Drive/b7TData_19/b7TData/Results/K520457/20180503/T2_cor.nii.gz',
'/media/matt/Seagate Expansion Drive/b7TData_19/b7TData/Results/K520457/20180510/T2_cor.nii.gz']
]
seg_files = [
['/media/matt/Seagate Expansion Drive/b7TData_19/b7TData/Results/K520552/20180510/tumor_seg.nii.gz',
'/media/matt/Seagate Expansion Drive/b7TData_19/b7TData/Results/K520552/20180517/tumor_seg.nii.gz'],
['/media/matt/Seagate Expansion Drive/b7TData_19/b7TData/Results/K520457/20180503/tumor_seg.nii.gz',
'/media/matt/Seagate Expansion Drive/b7TData_19/b7TData/Results/K520457/20180510/tumor_seg.nii.gz']
]
mask_files = [
['/media/matt/Seagate Expansion Drive/MR Data/MR_Images_Sarcoma/520552/Seg_520552-1-label.nii',
'/media/matt/Seagate Expansion Drive/MR Data/MR_Images_Sarcoma/520552/Seg_520552-2-label.nii'],
['/media/matt/Seagate Expansion Drive/MR Data/MR_Images_Sarcoma/520457/Seg_520457-1-label.nii',
'/media/matt/Seagate Expansion Drive/MR Data/MR_Images_Sarcoma/520457/Seg_520457-2-label.nii']
]
slices = [[27, 19],
[27, 19]
]
# Change paths if in windows
if 'nt' in os.name:
save_path = save_path.replace('/media/matt/Seagate Expansion Drive/', 'E://')
for i in range(len(image_files)):
image_files[i] = [i.replace('/media/matt/Seagate Expansion Drive/', 'E://') for i in image_files[i]]
seg_files[i] = [i.replace('/media/matt/Seagate Expansion Drive/', 'E://') for i in seg_files[i]]
mask_files[i] = [i.replace('/media/matt/Seagate Expansion Drive/', 'E://') for i in mask_files[i]]
if not os.path.exists(save_path):
os.mkdir(save_path)
# Colormap
colormap_mask = ['black', 'red']
colormap_seg = ['black', 'green']
colormap_comb = ['black', 'red', 'green']
# Histogram clipping
top_pct = 0.05 / 100
bot_pct = 15 / 100
sname_ims, sname_msks, sname_segs, sname_diffs = [], [], [], []
for i in range(len(image_files)):
for ii in range(len(image_files[i])):
# Read image
im = nib.load(image_files[i][ii]).get_data().astype(np.float)
msk = nib.load(mask_files[i][ii]).get_data().astype(np.float)
seg = nib.load(seg_files[i][ii]).get_data().astype(np.float)
# Scale the image
im_s = sorted(im.reshape(-1))
top_thresh = im_s[-round(len(im_s) * top_pct)]
bot_thresh = im_s[round(len(im_s) * bot_pct)]
im[im > top_thresh] = top_thresh
im[im < bot_thresh] = bot_thresh
# Select slice
s = slices[i][ii]
im = im[:, :, s].T
msk = msk[:, :, s].T
seg = seg[:, :, s].T
# Normalize
# Set up save name
save_name = os.path.join(save_path, snames[i][ii])
# Create overlay image - no masks
sname_im = save_name + 'im.png'
over_im = overlay_image(base_image=im,
overlay_image=np.zeros_like(msk),
colormap=colormap_mask,
sname=sname_im
)
# Create overlay image - mask
sname_msk = save_name + 'msk.png'
over_im = overlay_image(base_image=im,
overlay_image=msk,
colormap=colormap_mask,
sname=sname_msk
)
# Create overlay image - segmentation
sname_seg = save_name + 'seg.png'
over_im = overlay_image(base_image=im,
overlay_image=seg,
colormap=colormap_seg,
sname=sname_seg
)
# Create overlay image - combined
# Green = false positive, 1.0
# Red = false negative, 0.5
# False positives
fp = seg - msk
fp[fp < 0] = 0
# False negatives
fn = msk - seg
fn[fn < 0] = 0
# Combined
comb = fp + 0.5 * fn
sname_diff = save_name + 'diff.png'
over_im = overlay_image(base_image=im,
overlay_image=comb,
colormap=colormap_comb,
sname=sname_diff
)
sname_ims.append(sname_im)
sname_msks.append(sname_msk)
sname_segs.append(sname_seg)
sname_diffs.append(sname_diff)
make_montage(sname_ims, crop=(35, 10, -35, -70))
""" Initial images """
# Show modal images and segmentation
files = ['/media/matt/Seagate Expansion Drive/MR Data/MR_Images_Sarcoma/3/180420-3b-2_T1.nii',
'/media/matt/Seagate Expansion Drive/MR Data/MR_Images_Sarcoma/3/180420-3b-2_T1wC.nii',
'/media/matt/Seagate Expansion Drive/MR Data/MR_Images_Sarcoma/3/180420-3B-2_T2.nii']
ims = []
seg_file = '/media/matt/Seagate Expansion Drive/MR Data/MR_Images_Sarcoma/3/180420-3B-2-label.nii'
snames = ['T1', 'T1C', 'T2', 'seg']
snames = [os.path.join(save_path, i + '_2.png') for i in snames]
slice = 33
# Change paths if in windows
if 'nt' in os.name:
seg_file = seg_file.replace('/media/matt/Seagate Expansion Drive/', 'E://')
files = [i.replace('/media/matt/Seagate Expansion Drive/', 'E://') for i in files]
for file, sname in zip(files, snames[:-1]):
# Load the file and select slice
im = nib.load(file).get_data().astype(np.float)
im = im[:, :, slice].T
# Remove outliers
im_s = sorted(im.reshape(-1))
top_thresh = im_s[-round(len(im_s) * top_pct)]
bot_thresh = im_s[round(len(im_s) * bot_pct)]
im[im > top_thresh] = top_thresh
im[im < bot_thresh] = bot_thresh
# Convert to 8 bit
im -= im.min()
im /= im.max()
im *= 255
im = im.astype(np.uint8)
# Save image
save_overlay_im(im, sname)
# Save the segmenation image
seg = nib.load(seg_file).get_data().astype(np.uint8)
seg = seg[:, :, slice].T
seg -= seg.min()
seg = seg / seg.max()
seg *= 255
seg = seg.astype(np.uint8)
# Apply colormap
seg = np.repeat(seg[:, :, np.newaxis], 3, 2)
seg[:, :, 1:] = 0
# Save
save_overlay_im(seg, snames[-1])
# Save montage
make_montage_init(snames, sname_mont='mont_init1.png')
# Save second sample images
files = ['/media/matt/Seagate Expansion Drive/MR Data/SarcomaSegmentations/Mouse IV/Mouse IV T1 FLASH.nii',
'/media/matt/Seagate Expansion Drive/MR Data/SarcomaSegmentations/Mouse IV/Mouse IV T1FLASH with Contrast.nii',
'/media/matt/Seagate Expansion Drive/MR Data/SarcomaSegmentations/Mouse IV/Bias Corrected Images/Mouse IV T2TurboRARE New Bias Correction.nii']
seg_file = '/media/matt/Seagate Expansion Drive/MR Data/SarcomaSegmentations/Mouse IV/Mouse IV ROI Black and White Volume.nii'
snames = ['T1', 'T1C', 'T2', 'seg']
snames = [os.path.join(save_path, i + '.png') for i in snames]
slice = 25
# Change paths if in windows
if 'nt' in os.name:
seg_file = seg_file.replace('/media/matt/Seagate Expansion Drive/', 'E://')
files = [i.replace('/media/matt/Seagate Expansion Drive/', 'E://') for i in files]
for file, sname in zip(files, snames[:-1]):
# Load the file and select slice
im = nib.load(file).get_data().astype(np.float)
sz = im.shape
im = im[:, :, slice].T
# Remove outliers
im_s = sorted(im.reshape(-1))
top_thresh = im_s[-round(len(im_s) * top_pct)]
bot_thresh = im_s[round(len(im_s) * bot_pct)]
im[im > top_thresh] = top_thresh
im[im < bot_thresh] = bot_thresh
# Convert to 8 bit
im -= im.min()
im /= im.max()
im *= 255
im = im.astype(np.uint8)
# Save image
save_overlay_im(im, sname)
# Save the segmenation image
seg = load_label_show(seg_file, sz)
seg = seg[:, :, slice].T
# Apply colormap
seg = np.repeat(seg[:, :, np.newaxis], 3, 2)
seg[:, :, 1:] = 0
# Save
save_overlay_im(seg, snames[-1])
# Save montage
make_montage_init(snames, sname_mont='mont_init2.png')
""" Show tumor, edge, and bed segmentations """
from Radiomics.radiomic_maps_190501 import dilate_masks
mask_tumor = '/media/matt/Seagate Expansion Drive/b7TData_19/b7TData/Results/K520457/20180510/tumor_seg.nii.gz'
image_name = '/media/matt/Seagate Expansion Drive/b7TData_19/b7TData/Results/K520457/20180510/T2_cor.nii.gz'
snames = ['area_tumor.png', 'area_edge.png', 'area_bed.png']
snames = [os.path.join(save_path, i) for i in snames]
descriptor = 'area'
slices = [29]
top_pct = 1 / 100
bot_pct = 15 / 100
# Compute masks
mask_bed_file = dilate_masks(mask_tumor, image_name, save_path, diff=False, name=descriptor)
mask_edge_file = dilate_masks(mask_tumor, image_name, save_path, diff=True, name=descriptor)
# Load image data
im = nib.load(image_name).get_data().astype(np.float32)
mask_tumor = nib.load(mask_tumor).get_data().astype(np.uint8)
mask_bed = nib.load(mask_bed_file).get_data().astype(np.uint8)
mask_edge = nib.load(mask_edge_file).get_data().astype(np.uint8)
# Delete dilated masks
# os.remove(mask_bed_file)
# os.remove(mask_edge_file)
# Preprocess image
# Remove outliers
# Remove by pct
im_s = sorted(im.reshape(-1))
top_thresh = im_s[-round(len(im_s) * top_pct)]
bot_thresh = im_s[round(len(im_s) * bot_pct)]
im[im > top_thresh] = top_thresh
im[im < bot_thresh] = bot_thresh
# Convert to 8 bit
# im -= im.min()
# im /= im.max()
# im *= 255
# im = im.astype(np.uint8)
colormap_seg2 = ['black', 'green']
rows = range(20, im.shape[0] - 20)
cols = range(10, im.shape[1] - 10)
# Make image overlays
for i, mask in enumerate([mask_tumor, mask_edge, mask_bed]):
overlay_image(base_image=im[rows, :, slices[0]].T,
overlay_image=mask[rows, :, slices[0]].T,
colormap=colormap_seg2,
sname=snames[i])
# Combine images into a montage
make_montage_init(snames, sname_mont='area_montage.png')