[c6d664]: / segment_lung.py

Download this file

63 lines (43 with data), 1.8 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
import numpy as np
import nibabel as nib
from scipy import ndimage
from skimage import measure
from scipy import stats
def segment_lung(params, I, I_affine):
#####################################################
# Intensity thresholding & Morphological operations
#####################################################
M = np.zeros(I.shape)
M [I > params['lungMinValue']] = 1
M [I > params['lungMaxValue']] = 0
struct_s = ndimage.generate_binary_structure(3, 1)
struct_m = ndimage.iterate_structure(struct_s, 2)
M = ndimage.binary_closing(M, structure = struct_s, iterations = 1)
M = ndimage.binary_opening(M, structure = struct_m, iterations = 1)
#####################################################
# Estimate lung filed of view
#####################################################
[m, n, p] = I.shape;
medx = int(m/2)
medy = int(n/2)
xrange1 = int(m/2*params['xRangeRatio1'])
xrange2 = int(m/2*params['xRangeRatio2'])
zrange1 = int(p*params['zRangeRatio1'])
zrange2 = int(p*params['zRangeRatio2'])
#####################################################
# Select largest connected components & save nii
#####################################################
M = measure.label(M)
label1 = M[medx - xrange2 : medx - xrange1, medy, zrange1 : zrange2]
label2 = M[medx + xrange1 : medx + xrange2, medy, zrange1 : zrange2]
label1 = stats.mode(label1[label1 > 0])[0][0]
label2 = stats.mode(label2[label2 > 0])[0][0]
M[M == label1] = -1
M[M == label2] = -1
M[M > 0] = 0
M = M*-1
M = ndimage.binary_closing(M, structure = struct_m, iterations = 1)
M = ndimage.binary_fill_holes(M)
Mlung = np.int8(M)
nib.Nifti1Image(Mlung,I_affine).to_filename('./result/sample_lungaw.nii.gz')
return Mlung