|
a |
|
b/lung_segment.py |
|
|
1 |
""" |
|
|
2 |
This script is to segment the lung out of background as a prepocessing for lobes segementation. |
|
|
3 |
""" |
|
|
4 |
import SimpleITK as sitk |
|
|
5 |
import gui |
|
|
6 |
|
|
|
7 |
class LungSegment: |
|
|
8 |
""" |
|
|
9 |
This class is designed for 3D segmentation of lung, including the methods: |
|
|
10 |
... |
|
|
11 |
""" |
|
|
12 |
def __init__(self, img): |
|
|
13 |
self.img = img |
|
|
14 |
self.temp_img = None |
|
|
15 |
self.img_uint8 = None |
|
|
16 |
|
|
|
17 |
def conv_2_uint8(self, WINDOW_LEVEL=(1050,500)): |
|
|
18 |
""" |
|
|
19 |
Convert original image to 8-bit image |
|
|
20 |
:param WINDOW_LEVEL: Using an external viewer (ITK-SNAP or 3DSlicer) |
|
|
21 |
we identified a visually appealing window-level setting |
|
|
22 |
:return: None |
|
|
23 |
""" |
|
|
24 |
# self.img_uint8 = sitk.Cast(self.img, |
|
|
25 |
# sitk.sitkUInt8) |
|
|
26 |
self.img_uint8 = sitk.Cast(sitk.IntensityWindowing(self.img, |
|
|
27 |
windowMinimum=WINDOW_LEVEL[1] - WINDOW_LEVEL[0] / 2.0, |
|
|
28 |
windowMaximum=WINDOW_LEVEL[1] + WINDOW_LEVEL[0] / 2.0), |
|
|
29 |
sitk.sitkUInt8) |
|
|
30 |
|
|
|
31 |
def regiongrowing(self, seed_pts): |
|
|
32 |
""" |
|
|
33 |
Implement ConfidenceConnected by SimpleITK tools with given seed points |
|
|
34 |
:param seed_pts: seed points for region growing [(z,y,x), ...] |
|
|
35 |
:return: None |
|
|
36 |
""" |
|
|
37 |
self.temp_img = sitk.ConfidenceConnected(self.img, seedList=seed_pts, |
|
|
38 |
numberOfIterations=0, |
|
|
39 |
multiplier=2, |
|
|
40 |
initialNeighborhoodRadius=1, |
|
|
41 |
replaceValue=1) |
|
|
42 |
|
|
|
43 |
def image_showing(self, title=''): |
|
|
44 |
""" |
|
|
45 |
Showing image. |
|
|
46 |
:return: None |
|
|
47 |
""" |
|
|
48 |
gui.MultiImageDisplay(image_list=[sitk.LabelOverlay(self.img_uint8, self.temp_img)], |
|
|
49 |
title_list=[title]) |
|
|
50 |
|
|
|
51 |
def image_closing(self, size=7): |
|
|
52 |
""" |
|
|
53 |
Implement morphological closing to fix the "holes" inside the image. |
|
|
54 |
:param size: the size the closing kernel |
|
|
55 |
:return: None |
|
|
56 |
""" |
|
|
57 |
closing = sitk.BinaryMorphologicalClosingImageFilter() |
|
|
58 |
closing.SetForegroundValue(1) |
|
|
59 |
closing.SetKernelRadius(size) |
|
|
60 |
self.temp_img = closing.Execute(self.temp_img) |