forked from Rimehdaoudi/Tumour-Segmentation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPreprocess.py
More file actions
167 lines (157 loc) · 5.69 KB
/
Preprocess.py
File metadata and controls
167 lines (157 loc) · 5.69 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
# Import the required libraries.
import cv2
import numpy as np
from skimage.transform import resize
from nilearn.image import smooth_img, resample_to_img
# Pre-process class.
class Process(object):
# Expand the dimensions of the image.
def expandDims(self, image):
expDim = np.expand_dims(image, axis=-1)
print("Original Image Shape: ", image.shape)
print("New Image Shape: ", expDim.shape)
return expDim
# Resample an image.
def resampleImage(self, image, target):
resampledImage = resize(
image,
(image.shape[0],
target, target),
mode='constant',
anti_aliasing=True
)
print("Original Image Shape: ", image.shape)
print("New Image Shape: ", resampledImage.shape)
return resampledImage
# Function to either upsample or downsample a list of images
def resampleImages(self, images, target):
sampledImages = []
for i in range(len(images)):
print("Original Shape: ", images[i].shape)
sample = resize(
images[i],
(images[i].shape[0],
target, target),
mode='constant',
anti_aliasing=True
)
sampledImages.append(sample)
print("New Image Shape: ", sampledImages[i].shape)
print("\n")
return sampledImages
# Smooth a list of images.
def smoothImage(self, image, threshold):
smoothedImage = smooth_img(image, threshold)
return smoothedImage
# Smooth a list of images.
def smoothImages(self, images, threshold):
smoothedImages = []
for i in range(len(images)):
smoothedImage = smooth_img(images[i], threshold)
smoothedImages.append(smoothedImage)
print("Image Smoothened")
return smoothedImages
# Resample the mask to the size of the MRI image.
def resampleToImage(self, mask, image):
resampledImage = resample_to_img(mask, image)
return resampledImage
# Resample the masks to the MRI images.
def resampleToImages(self, images, masks):
resampledImages = []
for i in range(len(images)):
resampledImage = resample_to_img(masks, images)
resampledImages.append(resampledImage)
print("Mask Resampled")
return resampledImages
# Extract each slice from an MRI image
def sliceExtractor(
self,
imageData,
numSlices,
OUTDIR=' ',
Image=False,
Mask=False,
coronal=False,
sagittal=False,
transversal=False):
for i in range(numSlices):
# If extracting an image use this.
if Image:
# Coronal
if coronal:
slices = imageData[:, i, :]
flipVertical = cv2.flip(slices, 0)
cv2.imwrite(
OUTDIR +
"Coronal_" +
str(i) +
".png",
flipVertical
)
# Sagittal
elif sagittal:
slices = imageData[:, :, i]
flipVertical = cv2.flip(slices, 0)
cv2.imwrite(
OUTDIR +
"Sagittal_" +
str(i) +
".png",
flipVertical
)
# Transversal
elif transversal:
slices = imageData[i, :, :]
flipVertical = cv2.flip(slices, 0)
cv2.imwrite(
OUTDIR +
"Transversal_" +
str(i) +
".png",
flipVertical
)
else:
print("Extraction failed")
break
# If extracting a mask use this.
if Mask:
# Coronal
if coronal:
slices = imageData[:, i, :]
flipVertical = cv2.flip(slices, 0)
formatted = (flipVertical * 255).astype('uint8')
cv2.imwrite(
OUTDIR +
"Coronal_" +
str(i) +
".png",
formatted
)
# Sagittal
elif sagittal:
slices = imageData[:, :, i]
flipVertical = cv2.flip(slices, 0)
formatted = (flipVertical * 255).astype('uint8')
cv2.imwrite(
OUTDIR +
"Sagittal_" +
str(i) +
".png",
formatted
)
# Transversal
elif transversal:
slices = imageData[i, :, :]
flipVertical = cv2.flip(slices, 0)
formatted = (flipVertical * 255).astype('uint8')
cv2.imwrite(
OUTDIR +
"Transversal_" +
str(i) +
".png",
formatted
)
else:
print("Extraction failed")
break
print("Extraction complete")