-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsegmentation_solution.py
232 lines (210 loc) · 9.29 KB
/
segmentation_solution.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
import numpy as np
import scipy.ndimage as ndimage
from skimage import measure, morphology, segmentation
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, threshold_otsu
from skimage.segmentation import clear_border, mark_boundaries
import matplotlib.pyplot as plt
import cv2
def generate_markers(image):
# Creation of the internal Marker
marker_internal = image < -400
marker_internal = segmentation.clear_border(marker_internal)
marker_internal_labels = measure.label(marker_internal)
areas = [r.area for r in measure.regionprops(marker_internal_labels)]
areas.sort()
if len(areas) > 2:
for region in measure.regionprops(marker_internal_labels):
if region.area < areas[-2]:
for coordinates in region.coords:
marker_internal_labels[coordinates[0], coordinates[1]] = 0
marker_internal = marker_internal_labels > 0
# Creation of the external Marker
external_a = ndimage.binary_dilation(marker_internal, iterations=10)
external_b = ndimage.binary_dilation(marker_internal, iterations=55)
marker_external = external_b ^ external_a
# Creation of the Watershed Marker matrix
marker_watershed = np.zeros((512, 512), dtype=np.int)
marker_watershed += marker_internal * 255
marker_watershed += marker_external * 128
return marker_internal, marker_external, marker_watershed
def seperate_lungs(image):
# Creation of the markers as shown above:
marker_internal, marker_external, marker_watershed = generate_markers(image)
# Creation of the Sobel-Gradient
sobel_filtered_dx = ndimage.sobel(image, 1)
sobel_filtered_dy = ndimage.sobel(image, 0)
sobel_gradient = np.hypot(sobel_filtered_dx, sobel_filtered_dy)
sobel_gradient *= 255.0 / np.max(sobel_gradient)
# Watershed algorithm
watershed = morphology.watershed(sobel_gradient, marker_watershed)
# Reducing the image created by the Watershed algorithm to its outline
outline = ndimage.morphological_gradient(watershed, size=(3, 3))
outline = outline.astype(bool)
# Performing Black-Tophat Morphology for reinclusion
# Creation of the disk-kernel and increasing its size a bit
blackhat_struct = [[0, 0, 1, 1, 1, 0, 0],
[0, 1, 1, 1, 1, 1, 0],
[1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1],
[0, 1, 1, 1, 1, 1, 0],
[0, 0, 1, 1, 1, 0, 0]]
blackhat_struct = ndimage.iterate_structure(blackhat_struct, 8)
# Perform the Black-Hat
outline += ndimage.black_tophat(outline, structure=blackhat_struct)
# Use the internal marker and the Outline that was just created to generate the lungfilter
lungfilter = np.bitwise_or(marker_internal, outline)
# Close holes in the lungfilter
# fill_holes is not used here, since in some slices the heart would be reincluded by accident
lungfilter = ndimage.morphology.binary_closing(lungfilter, structure=np.ones((5, 5)), iterations=3)
# Apply the lungfilter (note the filtered areas being assigned -2000 HU)
segmented = np.where(lungfilter == 1, image, -2000 * np.ones((512, 512)))
return segmented, lungfilter, outline, watershed, sobel_gradient, marker_internal, marker_external, marker_watershed
def get_filtered_lung(image):
# Creation of the internal Marker
marker_internal = image < -500
marker_internal = segmentation.clear_border(marker_internal)
marker_internal_labels = measure.label(marker_internal)
areas = [r.area for r in measure.regionprops(marker_internal_labels)]
areas.sort()
if len(areas) > 2:
for region in measure.regionprops(marker_internal_labels):
if region.area < areas[-2]:
for coordinates in region.coords:
marker_internal_labels[coordinates[0], coordinates[1]] = 0
marker_internal = marker_internal_labels > 0
# Creation of the external Marker
external_a = ndimage.binary_dilation(marker_internal, iterations=10)
external_b = ndimage.binary_dilation(marker_internal, iterations=55)
marker_external = external_b ^ external_a
# Creation of the Watershed Marker matrix
marker_watershed = np.zeros((512, 512), dtype=np.int)
marker_watershed += marker_internal * 255
marker_watershed += marker_external * 128
# Creation of the Sobel-Gradient
sobel_filtered_dx = ndimage.sobel(image, 1)
sobel_filtered_dy = ndimage.sobel(image, 0)
sobel_gradient = np.hypot(sobel_filtered_dx, sobel_filtered_dy)
sobel_gradient *= 255.0 / np.max(sobel_gradient)
# Watershed algorithm
watershed = morphology.watershed(sobel_gradient, marker_watershed)
# Reducing the image created by the Watershed algorithm to its outline
outline = ndimage.morphological_gradient(watershed, size=(3, 3))
outline = outline.astype(bool)
# Performing Black-Tophat Morphology for reinclusion
# Creation of the disk-kernel and increasing its size a bit
blackhat_struct = [[0, 0, 1, 1, 1, 0, 0],
[0, 1, 1, 1, 1, 1, 0],
[1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1],
[0, 1, 1, 1, 1, 1, 0],
[0, 0, 1, 1, 1, 0, 0]]
blackhat_struct = ndimage.iterate_structure(blackhat_struct, 8)
# Perform the Black-Hat
outline += ndimage.black_tophat(outline, structure=blackhat_struct)
# Use the internal marker and the Outline that was just created to generate the lungfilter
lungfilter = np.bitwise_or(marker_internal, outline)
# Close holes in the lungfilter
# fill_holes is not used here, since in some slices the heart would be reincluded by accident
lungfilter = ndimage.morphology.binary_closing(lungfilter, structure=np.ones((5, 5)), iterations=3)
return lungfilter
# key solution
def get_segmented_lungs(raw_im):
'''
对肺部CT图像实现实质
This funtion segments the lungs from the given 2D slice.
:param raw_im: 输入原始图像
:return: binaty 二值图掩码 ,im 原始图像叠加二值图掩码后结果
'''
im = raw_im.copy()
'''
将2D Slice转为二值图
Step 1: Convert into a binary image.
'''
binary = im < -567.5
# binary = im < -500
# thresh = threshold_otsu(binary)
# binary = binary > thresh
'''
删除连接到图像边界的噪点。
Step 2: Remove the blobs connected to the border of the image.
'''
cleared = clear_border(binary)
'''
标记图像
Step 3: Label the image.
'''
label_image = label(cleared)
'''
保持标记有两个最大的区域
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
'''
半径为2 pixels,腐蚀操作。
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)
'''
半径为10 pixels,闭合操作。
Step 6: Closure operation with a disk of radius 10. This operation is
to keep nodules attached to the lung wall.
'''
selem = disk(7)
binary = binary_closing(binary, selem)
'''
将二值图中的部分噪点填充。
Step 7: Fill in the small holes inside the binary mask of lungs.
'''
edges = roberts(binary)
binary = ndimage.binary_fill_holes(edges)
'''
在输入图像上叠加二进制掩码。
Step 8: Superimpose the binary mask on the input image.
'''
get_high_vals = binary == 0
im[get_high_vals] = 0
return binary, im
def get_segmented_lungs_with_opencv_api(raw_im, ground_truth, plot=False):
'''
对肺部CT图像实现实质
This funtion segments the lungs from the given 2D slice.
:param raw_im: 输入原始图像
:return: binaty 二值图掩码 ,im 原始图像叠加二值图掩码后结果
'''
im = raw_im.copy()
if plot == True:
f, plots = plt.subplots(8, 1, figsize=(5, 40))
'''
将2D Slice转为二值图
Step 1: Convert into a binary image.
'''
binary = im < -567.5
if plot == True:
plots[0].axis('off')
plots[0].imshow(binary, cmap=plt.cm.bone)
ostued = cv2.threshold(binary, thresh=0, maxval=255, type=cv2.THRESH_OTSU)
if plot == True:
plots[1].axis('off')
plots[1].imshow(ostued, cmap=plt.cm.bone)
expanded = cv2.copyMakeBorder(binary, 3, 3, 3, 3, cv2.BORDER_CONSTANT, value=0)
if plot == True:
plots[2].axis('off')
plots[2].imshow(expanded, cmap=plt.cm.bone)
filled = cv2.floodFill(expanded, mask=ground_truth, seedPoint=(0, 0), newVal=255)
if plot == True:
plots[3].axis('off')
plots[4].imshow(filled, cmap=plt.cm.bone)