[c9a8c4]: / skull-stripping / data.py

Download this file

86 lines (62 with data), 2.6 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
from __future__ import print_function
import numpy as np
import os
from skimage.io import imread
from skimage.transform import rescale
from skimage.transform import rotate
image_rows = 256
image_cols = 256
channels = 3 # refers to neighboring slices; if set to 3, takes previous and next slice as additional channels
modalities = 1 # refers to pre, flair and post modalities; if set to 3, uses all and if set to 1, only flair
def load_data(path):
"""
Assumes filenames in given path to be in the following format as defined in `preprocessing3D.m`:
for images: <case_id>_<slice_number>.tif
for masks: <case_id>_<slice_number>_mask.tif
Args:
path: string to the folder with images
Returns:
np.ndarray: array of images
np.ndarray: array of masks
np.chararray: array of corresponding images' filenames without extensions
"""
images_list = os.listdir(path)
total_count = len(images_list) / 2
images = np.ndarray((total_count, image_rows, image_cols,
channels * modalities), dtype=np.uint8)
masks = np.ndarray((total_count, image_rows, image_cols), dtype=np.uint8)
names = np.chararray(total_count, itemsize=64)
i = 0
for image_name in images_list:
if 'mask' in image_name:
continue
names[i] = image_name.split('.')[0]
slice_number = int(names[i].split('_')[-1])
patient_id = '_'.join(names[i].split('_')[:-1])
image_mask_name = image_name.split('.')[0] + '_mask.tif'
img = imread(os.path.join(path, image_name), as_grey=(modalities == 1))
img_mask = imread(os.path.join(path, image_mask_name), as_grey=True)
if channels > 1:
img_prev = read_slice(path, patient_id, slice_number - 1)
img_next = read_slice(path, patient_id, slice_number + 1)
img = np.dstack((img_prev, img[..., np.newaxis], img_next))
elif modalities == 1:
img = np.array([img])
img_mask = np.array([img_mask])
images[i] = img
masks[i] = img_mask
i += 1
images = images.astype('float32')
masks = masks[..., np.newaxis]
masks = masks.astype('float32')
masks /= 255.
return images, masks, names
def read_slice(path, patient_id, slice):
img = np.zeros((image_rows, image_cols))
img_name = patient_id + '_' + str(slice) + '.tif'
img_path = os.path.join(path, img_name)
try:
img = imread(img_path, as_grey=(modalities == 1))
except Exception:
pass
return img[..., np.newaxis]