-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMip_Utility.py
More file actions
297 lines (259 loc) · 10 KB
/
Copy pathMip_Utility.py
File metadata and controls
297 lines (259 loc) · 10 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
import numpy as np
import SimpleITK as sitk
import os
from os import listdir
from os.path import isfile, join
import os
import numpy as np
import SimpleITK as sitk
import matplotlib.pyplot as plt
import pandas as pd
from skimage.morphology import ball, disk, dilation, binary_erosion, remove_small_objects, erosion, closing, reconstruction, binary_closing
from skimage.measure import label,regionprops, perimeter
from skimage.morphology import binary_dilation, binary_opening
from skimage.filters import roberts, sobel
from skimage import measure, feature
from skimage.segmentation import clear_border
from skimage import data
from scipy import ndimage as ndi
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
import scipy.misc
import csv
############################
# Utility Functions for MIP#
############################
id1 = '1.3.6.1.4.1.14519.5.2.1.6279.6001.105756658031515062000744821260'
id2 = '1.3.6.1.4.1.14519.5.2.1.6279.6001.108197895896446896160048741492'
path1 = './subset0/' + id1 + '.mhd'
path2 = './subset0/' + id2 + '.mhd'
'''
This function takes in image as a matrix and returns a new image after applying
Maximum Intensity Projections.
'''
def createMIP(np_img, slices_num = 13):
''' create the mip image from original image, slice_num is the number of
slices for maximum intensity projection'''
img_shape = np_img.shape
np_mip = np.zeros(img_shape)
for i in range(img_shape[0]):
start = max(0, i-slices_num)
np_mip[i,:,:] = np.amax(np_img[start:i+1],0)
return np_mip
'''
This function takes in image as a matrix and returns a new image after applying
Maximum Intensity Projections.
'''
def createMIP_coronal(np_img, slices_num = 15):
''' create the mip image from original image, slice_num is the number of
slices for maximum intensity projection'''
img_shape = np_img.shape
np_mip = np.zeros(img_shape)
for i in range(img_shape[1]):
start = max(0, i-slices_num)
np_mip[:,i,:] = np.amax(np_img[:,start:i+1,:],1)
return np_mip
'''
This funciton reads a '.mhd' file using SimpleITK and return the image array,
origin and spacing of the image.
'''
def load_itk(filename):
# Reads the image using SimpleITK
itkimage = sitk.ReadImage(filename)
# Convert the image to a numpy array first and then shuffle the dimensions to get axis in the order z,y,x
ct_scan = sitk.GetArrayFromImage(itkimage)
# Read the origin of the ct_scan, will be used to convert the coordinates from world to voxel and vice versa.
origin = np.array(list(reversed(itkimage.GetOrigin())))
# Read the spacing along each dimension
spacing = np.array(list(reversed(itkimage.GetSpacing())))
return ct_scan, origin, spacing
'''
This function is used to convert the world coordinates to voxel coordinates using
the origin and spacing of the ct_scan
'''
def world_2_voxel(world_coordinates, origin, spacing):
stretched_voxel_coordinates = np.absolute(world_coordinates - origin)
voxel_coordinates = stretched_voxel_coordinates / spacing
return voxel_coordinates
'''
This function is used to convert the voxel coordinates to world coordinates using
the origin and spacing of the ct_scan.
'''
def voxel_2_world(voxel_coordinates, origin, spacing):
stretched_voxel_coordinates = voxel_coordinates * spacing
world_coordinates = stretched_voxel_coordinates + origin
return world_coordinates
'''
This function plots the axial view of the ct scan read in from load_itk.
'''
def plot_ct_scan(scan):
f, plots = plt.subplots(int(scan.shape[0] / 20) + 1, 4, figsize=(25, 25))
for i in range(0, scan.shape[0]):
plots[int(i / 20), int((i % 20) / 5)].axis('off')
plots[int(i / 20), int((i % 20) / 5)].imshow(scan[i])
'''
This function plots the coronal view of the ct scan read in from load_itkself.
'''
def plot_ct_scan_coronal(scan):
f, plots = plt.subplots(int(scan.shape[1] / 20) + 1, 4, figsize=(20, 30))
for i in range(0, scan.shape[1]):
plots[int(i / 20), int((i % 20) / 5)].axis('off')
plots[int(i / 20), int((i % 20) / 5)].imshow(scan[:, i, :])
def get_segmented_lungs(img, plot=False):
im = np.copy(img)
'''
This funtion segments the lungs from the given 2D slice.
'''
if plot == True:
f, plots = plt.subplots(9, 1, figsize=(5, 40))
plots[0].axis('off')
plots[0].imshow(im, cmap=plt.cm.bone)
'''
Step 1: Convert into a binary image.
'''
binary = im < 604
if plot == True:
plots[1].axis('off')
plots[1].imshow(binary, cmap=plt.cm.bone)
'''
Step 2: Remove the blobs connected to the border of the image.
'''
cleared = clear_border(binary)
if plot == True:
plots[2].axis('off')
plots[2].imshow(cleared, cmap=plt.cm.bone)
'''
Step 3: Label the connected regions of the image.
'''
label_image = label(cleared)
if plot == True:
plots[3].axis('off')
plots[3].imshow(label_image, cmap=plt.cm.bone)
'''
Step 4: Keep the labels with 2 largest areas.
'''
areas = [r.area for r in regionprops(label_image)]
areas.sort()
if len(areas) > 2:
for region in regionprops(label_image):
if region.area < areas[-2]:
for coordinates in region.coords:
label_image[coordinates[0], coordinates[1]] = 0
binary = label_image > 0
if plot == True:
plots[4].axis('off')
plots[4].imshow(binary, cmap=plt.cm.bone)
'''
Step 5: Erosion operation with a disk of radius 2. This operation is
seperate the lung nodules attached to the blood vessels.
'''
selem = disk(2)
binary = binary_erosion(binary, selem)
if plot == True:
plots[5].axis('off')
plots[5].imshow(binary, cmap=plt.cm.bone)
'''
Step 6: Closure operation with a disk of radius 10. This operation is
to keep nodules attached to the lung wall.
'''
selem = disk(10)
binary = binary_closing(binary, selem)
if plot == True:
plots[6].axis('off')
plots[6].imshow(binary, cmap=plt.cm.bone)
'''
Step 7: Fill in the small holes inside the binary mask of lungs.
'''
edges = roberts(binary)
binary = ndi.binary_fill_holes(edges)
if plot == True:
plots[7].axis('off')
plots[7].imshow(binary, cmap=plt.cm.bone)
'''
Step 8: Superimpose the binary mask on the input image.
'''
get_high_vals = binary == 0
im[get_high_vals] = 0
if plot == True:
plots[8].axis('off')
plots[8].imshow(im, cmap=plt.cm.bone)
return im
def segment_lung_from_ct_scan(ct_scan):
return np.asarray([get_segmented_lungs(s) for s in ct_scan])
def resize_image(img, spacing):
RESIZE_SPACING = [1, 1, 1]
resize_factor = spacing / RESIZE_SPACING
new_real_shape = img.shape * resize_factor
new_shape = np.round(new_real_shape)
real_resize = new_shape / img.shape
new_spacing = spacing / real_resize
#resize image
lung_img = scipy.ndimage.interpolation.zoom(img, real_resize)
return lung_img, new_spacing
def seq(start, stop, step=1):
n = int(round((stop - start)/float(step)))
if n > 1:
return([start + step*i for i in range(n+1)])
else:
return([])
'''
This function is used to create spherical regions in binary masks
at the given locations and radius.
'''
def draw_circles(image,cands,origin,spacing):
#make empty matrix, which will be filled with the mask
RESIZE_SPACING = [1, 1, 1]
image_mask = np.zeros(image.shape)
#run over all the nodules in the lungs
for ca in cands.values:
#get middel x-,y-, and z-worldcoordinate of the nodule
radius = np.ceil(ca[4])/2
coord_x = ca[1]
coord_y = ca[2]
coord_z = ca[3]
image_coord = np.array((coord_z,coord_y,coord_x))
#determine voxel coordinate given the worldcoordinate
image_coord = world_2_voxel(image_coord,origin,spacing)
#determine the range of the nodule
noduleRange = seq(-radius, radius, RESIZE_SPACING[0])
#create the mask
for x in noduleRange:
for y in noduleRange:
for z in noduleRange:
coords = world_2_voxel(np.array((coord_z+z,coord_y+y,coord_x+x)),origin,spacing)
if (np.linalg.norm(image_coord-coords) * RESIZE_SPACING[0]) < radius:
image_mask[int(np.round(coords[0])),int(np.round(coords[1])),int(np.round(coords[2]))] = int(1)
return image_mask
def create_nodule_mask(path, cands):
'''
This function takes the path to a '.mhd' file as input and
is used to create the nodule masks and segmented lungs after
rescaling to 1mm size in all directions. It saved them in the .npz
format. It also takes the list of nodule locations in that CT Scan as
input.
'''
# get file
img, origin, spacing = load_itk(path)
#resize image
lung_img, new_spacing = resize_image(img, spacing)
# Segment the lung structure
lung_img = lung_img + 1024
lung_mask = segment_lung_from_ct_scan(lung_img)
lung_img = lung_img - 1024
#create nodule mask
nodule_mask = draw_circles(lung_img,cands,origin,new_spacing)
lung_img_512, lung_mask_512, nodule_mask_512 = np.zeros(
(lung_img.shape[0], 512, 512)), np.zeros(
(lung_mask.shape[0], 512, 512)), np.zeros(
(nodule_mask.shape[0], 512, 512))
original_shape = lung_img.shape
for z in range(lung_img.shape[0]):
offset = int((512 - original_shape[1]))
upper_offset = int(np.round(offset/2))
lower_offset = offset - upper_offset
# not sure what code below is doing exactly
new_origin = voxel_2_world([-upper_offset,-lower_offset,0],origin,new_spacing)
lung_img_512[z, upper_offset:-lower_offset,upper_offset:-lower_offset] = lung_img[z,:,:]
lung_mask_512[z, upper_offset:-lower_offset,upper_offset:-lower_offset] = lung_mask[z,:,:]
nodule_mask_512[z, upper_offset:-lower_offset,upper_offset:-lower_offset] = nodule_mask[z,:,:]
return lung_img_512, lung_mask_512, nodule_mask_512