-
Notifications
You must be signed in to change notification settings - Fork 0
/
dipBestTrial.py
286 lines (241 loc) · 8.34 KB
/
dipBestTrial.py
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
# common packages
import numpy as np
import os
import copy
from math import *
import matplotlib.pyplot as plt
from functools import reduce
# reading in dicom files
import pydicom
# skimage image processing packages
from skimage import measure, morphology
from skimage.morphology import ball, binary_closing
from skimage.measure import label, regionprops
# scipy linear algebra functions
from scipy.linalg import norm
import scipy.ndimage
# ipywidgets for some interactive plots
from ipywidgets.widgets import *
import ipywidgets as widgets
# plotly 3D interactive graphs
import plotly
from plotly.graph_objs import *
import chart_studio.plotly as py
from scipy import ndimage, misc
import cv2
from skimage import data
from skimage.exposure import histogram
from skimage import feature
from skimage import data
from skimage.exposure import histogram
from skimage.feature import canny
from skimage.filters import sobel
from skimage.segmentation import watershed
from scipy import ndimage as ndi
"""LOADING DICOM DATA"""
def load_scan(path):
slices = [pydicom.dcmread(path+'/' + s) for s in
os.listdir(path)]
slices = [s for s in slices if 'SliceLocation' in s]
slices.sort(key = lambda x: int(x.InstanceNumber))
try:
slice_thickness = np.abs(slices[0].ImagePositionPatient[2]-slices[1].ImagePositionPatient[2])
except:
slice_thickness = np.abs(slices[0].SliceLocation-slices[1].SliceLocation)
for s in slices:
s.SliceThickness = slice_thickness
return slices
def get_pixels_hu(scans):
image = np.stack([s.pixel_array for s in scans])
image = image.astype(np.int16)
# Set outside-of-scan pixels to 0
# The intercept is usually -1024, so air is approximately 0
image[image == -2000] = 0
# Convert to Hounsfield units (HU)
intercept = scans[0].RescaleIntercept
slope = scans[0].RescaleSlope
if slope != 1:
image = slope * image.astype(np.float64)
image = image.astype(np.int16)
image += np.int16(intercept)
return np.array(image, dtype=np.int16)
# set path and load files
path = r"C:\Users\user\OneDrive\Desktop\data"
patient_dicom = load_scan(path)
patient_pixels = get_pixels_hu(patient_dicom)
#sanity check
plt.imshow(patient_pixels[90], cmap=plt.cm.bone)
"""IMAGE PROCESSING"""
def largest_label_volume(im, bg=-1):
vals, counts = np.unique(im, return_counts=True)
counts = counts[vals != bg]
vals = vals[vals != bg]
if len(counts) > 0:
return vals[np.argmax(counts)]
else:
return None
def segment_lung_mask(image, fill_lung_structures=True):
# not actually binary, but 1 and 2.
# 0 is treated as background, which we do not want
binary_image = np.array(image >= -700, dtype=np.int8)+1
labels = measure.label(binary_image)
# Pick the pixel in the very corner to determine which label is air.
# Improvement: Pick multiple background labels from around the patient
# More resistant to “trays” on which the patient lays cutting the air around the person in half
background_label = labels[0,0,0]
# Fill the air around the person
binary_image[background_label == labels] = 2
# Method of filling the lung structures (that is superior to
# something like morphological closing)
if fill_lung_structures:
# For every slice we determine the largest solid structure
for i, axial_slice in enumerate(binary_image):
axial_slice = axial_slice-1
labeling = measure.label(axial_slice)
l_max = largest_label_volume(labeling, bg=0)
if l_max is not None: #This slice contains some lung
binary_image[i][labeling != l_max] = 1
binary_image -= 1 #Make the image actual binary
binary_image = 1-binary_image # Invert it, lungs are now 1
# Remove other air pockets inside body
labels = measure.label(binary_image, background=0)
l_max = largest_label_volume(labels, bg=0)
if l_max is not None: # There are air pockets
binary_image[labels != l_max] = 0
return binary_image
# get masks
segmented_lungs = segment_lung_mask(patient_pixels,
fill_lung_structures=False)
segmented_lungs_fill = segment_lung_mask(patient_pixels,
fill_lung_structures=True)
internal_structures = segmented_lungs_fill - segmented_lungs
# isolate lung from chest
copied_pixels = copy.deepcopy(patient_pixels)
for i, mask in enumerate(segmented_lungs_fill):
get_high_vals = mask == 0
copied_pixels[i][get_high_vals] = 0
seg_lung_pixels = copied_pixels
# sanity check
plt.imshow(seg_lung_pixels[7], cmap=plt.cm.bone)
slice_id = 90
plt.figure(1)
plt.title('Original Dicom')
plt.imshow(patient_pixels[slice_id], cmap=plt.cm.bone)
plt.figure(2)
plt.title('Lung Mask')
plt.imshow(segmented_lungs_fill[slice_id], cmap=plt.cm.bone)
plt.figure(3)
plt.title('Parenchyma Mask on Segmented Lung')
plt.imshow(seg_lung_pixels[slice_id], cmap=plt.cm.bone)
plt.imshow(internal_structures[slice_id], cmap='jet', alpha=0.7)
plt.figure(4)
# slide through dicom images using a slide bar
plt.figure(1)
def dicom_animation(x):
plt.imshow(patient_pixels[x])
return x
interact(dicom_animation, x=(90, len(patient_pixels)-1))
"""FILTERING"""
"""MEDIAN"""
fig = plt.figure()
plt.gray() # show the filtered result in grayscale
ax1 = fig.add_subplot(121) # left side
ax2 = fig.add_subplot(122) # right side
ascent = patient_pixels[90]
result = ndimage.median_filter(ascent, size=20)
ax1.imshow(ascent)
ax2.imshow(result)
plt.show()
"""GAUSSIAN"""
fig = plt.figure()
plt.gray() # show the filtered result in grayscale
ax1 = fig.add_subplot(121) # left side
ax2 = fig.add_subplot(122) # right side
ascent = patient_pixels[90]
result = ndimage.gaussian_filter(ascent,sigma=5)
ax1.imshow(ascent)
ax2.imshow(result)
plt.show()
"""minimum"""
fig = plt.figure()
plt.gray() # show the filtered result in grayscale
ax1 = fig.add_subplot(121) # left side
ax2 = fig.add_subplot(122) # right side
ascent = patient_pixels[90]
result = ndimage.minimum_filter(ascent,size=20)
ax1.imshow(ascent)
ax2.imshow(result)
plt.show()
"""MAXIMUM"""
fig = plt.figure()
plt.gray() # show the filtered result in grayscale
ax1 = fig.add_subplot(121) # left side
ax2 = fig.add_subplot(122) # right side
ascent = patient_pixels[90]
result = ndimage.maximum_filter(ascent,size=20)
ax1.imshow(ascent)
ax2.imshow(result)
plt.show()
"""SOBEL"""
fig = plt.figure()
plt.gray() # show the filtered result in grayscale
ax1 = fig.add_subplot(121) # left side
ax2 = fig.add_subplot(122) # right side
ascent = patient_pixels[90]
result = ndimage.sobel(ascent)
ax1.imshow(ascent)
ax2.imshow(result)
plt.show()
"""PREWITT"""
fig = plt.figure()
plt.gray() # show the filtered result in grayscale
ax1 = fig.add_subplot(121) # left side
ax2 = fig.add_subplot(122) # right side
ascent = patient_pixels[90]
result = ndimage.prewitt(ascent)
ax1.imshow(ascent)
ax2.imshow(result)
plt.show()
"""HISTOGRAM"""
histogram, bin_edges = np.histogram(patient_pixels[53], bins=512, range=(0,121))
plt.figure()
plt.title("Grayscale Histogram")
plt.xlabel("grayscale value")
plt.ylabel("pixels")
plt.plot(bin_edges[0:-1], histogram) # <- or here
plt.show()
histogram, bin_edges = np.histogram(patient_pixels[18], bins=512, range=(0,121))
plt.figure()
plt.title("Grayscale Histogram")
plt.xlabel("grayscale value")
plt.ylabel("pixels")
plt.plot(bin_edges[0:-1], histogram) # <- or here
plt.show()
"""EDGE BASED SEGMENTATION"""
lung= patient_pixels[90]
hist, hist_centers = histogram(lung)
edges = canny(lung/512.)
plt.imshow(edges,cmap=plt.cm.bone)
fill_lung = ndi.binary_fill_holes(edges)
label_objects, nb_labels = ndi.label(fill_lung)
sizes = np.bincount(label_objects.ravel())
mask_sizes = sizes > 20
mask_sizes[0] = 0
lung_cleaned = mask_sizes[label_objects]
plt.imshow(lung_cleaned,cmap=plt.cm.bone)
"""Region-based segmentation"""
markers = np.zeros_like(lung)
markers[lung < 50] = 1
markers[lung> 500] = 2
elevation_map = sobel(lung)
plt.imshow(elevation_map,cmap=plt.cm.bone)
markers = np.zeros_like(lung)
markers[lung < 30] = 1
markers[lung > 150] = 2
plt.imshow(markers,cmap=plt.cm.bone)
"""WATERSHED TRANSFORM"""
segmentation = watershed(elevation_map, markers)
plt.imshow(segmentation,cmap=plt.cm.bone)
segmentation = ndi.binary_fill_holes(segmentation - 1)
labeled_lung, _ = ndi.label(segmentation)
plt.imshow(labeled_lung,cmap=plt.cm.bone)