[c6d664]: / utils.py

Download this file

210 lines (162 with data), 7.2 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
import numpy as np
from scipy.spatial import distance
from scipy import ndimage
from skimage import measure
#####################################################
# Pre-define parameters
#####################################################
def define_parameter():
params = {}
#----------------------------------------------------
# Parameters for intensity (fixed)
#----------------------------------------------------
params['lungMinValue'] = -1024
params['lungMaxValue'] = -400
params['lungThreshold'] = -900
#----------------------------------------------------
# Parameters for lung segmentation (fixed)
#----------------------------------------------------
params['xRangeRatio1'] = 0.4
params['xRangeRatio2'] = 0.75
params['zRangeRatio1'] = 0.5
params['zRangeRatio2'] = 0.75
#----------------------------------------------------
# Parameters for airway segmentation
# NEED TO ADAPT for image resolution and orientation
# [current values work for demo image with resolution = 1mm^3]
#----------------------------------------------------
params['airwayRadiusMask'] = 15 # increase the value if you have high resolution image
params['airwayRadiusX'] = 8 # ditto
params['airwayRadiusZ'] = 15 # ditto
params['super2infer'] = 0 # value = 1 if slice no. increases from superior to inferior, else value = 0
return params
#####################################################
# Generate binary structure to mimic trachea
#####################################################
def generate_structure_trachea(Radius, RadiusX, RadiusZ):
struct_trachea = np.zeros([2*Radius+1,2*Radius+1,RadiusZ])
for i in range(0,2*Radius+1):
for j in range(0,2*Radius+1):
if distance.euclidean([Radius+1,Radius+1],[i,j]) < RadiusX:
struct_trachea[i,j,:] = 1
else:
struct_trachea[i,j,:] = 0
return struct_trachea
#####################################################
# Generate bounding box
#####################################################
def bbox2_3D(img,label,margin,limit):
imgtmp = np.zeros(img.shape)
imgtmp[img == label] = 1
x = np.any(imgtmp, axis=(1, 2))
y = np.any(imgtmp, axis=(0, 2))
z = np.any(imgtmp, axis=(0, 1))
xmin, xmax = np.where(x)[0][[0, -1]]
ymin, ymax = np.where(y)[0][[0, -1]]
zmin, zmax = np.where(z)[0][[0, -1]]
xmin = xmin - margin - 1
xmin = max(0,xmin)
ymin = ymin - margin - 1
ymin = max(0,ymin)
zmin = zmin - margin - 1
zmin = max(0,zmin)
xmax = xmax + margin + 1
xmax = min(xmax,limit[0])
ymax = ymax + margin + 1
ymax = min(ymax,limit[1])
zmax = zmax + margin + 1
zmax = min(zmax,limit[2])
return xmin, xmax, ymin, ymax, zmin, zmax
#####################################################
# Generate inital point in trachea
#####################################################
def generate_initLoc(params, I, Mlung, Radius, RadiusZ, struct_trachea):
mind = np.argwhere(Mlung == 1)
initLoc = [0,0,0]
minDiff = float('inf')
if params['super2infer']:
slice_no = np.min(mind[:,2])
Itmp = I[:,:,slice_no:slice_no+RadiusZ]
else:
slice_no = np.max(mind[:,2])
Itmp = I[:,:,slice_no-RadiusZ:slice_no]
Mtmp = np.ones(Itmp.shape);
Mtmp[Itmp < params['lungMinValue']] = 0
Mtmp[Itmp > params['lungMaxValue']] = 0
Itmp = Mtmp;
Mtmp = np.sum(Mtmp, axis = 2)
for i in range(Radius, Itmp.shape[0]-Radius):
for j in range(Radius, Itmp.shape[1]-Radius):
if Mtmp[i,j] > 0:
struct_Itmp = Itmp[i-Radius:i+Radius+1,j-Radius:j+Radius+1,:]
currVal = struct_Itmp - struct_trachea
currVal = np.sum(np.square(currVal))
if currVal < minDiff:
initLoc = [i,j,slice_no]
minDiff = currVal
print 'initial location = '+str(initLoc)
return slice_no, initLoc
#####################################################
# Closed space dialation to segment airway
#####################################################
def close_space_dilation(params, I, Mlung, Radius, RadiusX, RadiusZ, struct_s, slice_no, initLoc):
iterNoPerSlice = RadiusX
maxFactor = RadiusX/2
maxChange = RadiusX*RadiusX*RadiusX*50
totalChange = 1
tempCheck = 0
[m,n,p] = I.shape
Mtmp = np.zeros([m,n,p])
struct_m = ndimage.iterate_structure(struct_s, 2)
if params['super2infer']:
Mtmp[initLoc[0]-Radius:initLoc[0]+Radius+1,
initLoc[1]-Radius:initLoc[1]+Radius+1,
0:slice_no+RadiusZ] = 1
else:
Mtmp[initLoc[0]-Radius:initLoc[0]+Radius+1,
initLoc[1]-Radius:initLoc[1]+Radius+1,
slice_no-RadiusZ:p-1] = 1
Mtmp = np.multiply(Mtmp, Mlung)
Minit = ndimage.binary_closing(Mtmp, structure = struct_s, iterations = 1)
Minit = np.int8(Minit)
Minit[Minit > 0] = 2
while totalChange > 0:
maxSegmentChange = 0;
tempCheck = tempCheck + 1
L = measure.label(np.floor(Minit/2))
Minit[Minit > 1] = 1
for label in np.unique(L[:]):
if label != 0 and np.sum(L[:] == label) > 10:
# Process each component in local FOV
xmin, xmax, ymin, ymax, zmin, zmax = bbox2_3D(L,label,iterNoPerSlice,[m,n,p])
Mtmp = Minit[xmin:xmax,ymin:ymax,zmin:zmax]
Itmp = I[xmin:xmax,ymin:ymax,zmin:zmax]
Ltmp = L[xmin:xmax,ymin:ymax,zmin:zmax]
Ltmp[Ltmp != label] = 0
Ltmp[Ltmp > 0] = 1;
for iterCount in range(0, iterNoPerSlice):
Ltmp = ndimage.binary_dilation(Ltmp, structure = struct_s, iterations = 1)
Ltmp = np.int8(Ltmp)
Ltmp[Itmp > params['lungThreshold']] = 0
Ltmp = ndimage.binary_closing(Ltmp, structure = struct_s, iterations = 1)
Ltmp = np.int8(Ltmp)
Ltmp[Mtmp > 0] = 0
Ltmp[Ltmp > 0] = 2
Ltmp = Ltmp + Mtmp
segmentChange = np.sum(Ltmp[:]>1)
if segmentChange < maxChange or tempCheck < 10:
Minit[xmin:xmax,ymin:ymax,zmin:zmax] = Ltmp
if segmentChange > maxSegmentChange:
maxSegmentChange = segmentChange
if tempCheck < 10:
maxChange = max(maxFactor*maxSegmentChange,maxChange)
else:
maxChange = min(maxFactor*maxSegmentChange,maxChange)
totalChange = np.sum(Minit[:]>1)
print 'iter = '+str(tempCheck)+' airway sum = '+str(np.sum(Minit[:]>0))\
+' airway change = '+str(totalChange)
Minit[Minit > 0] = 1
Minit = ndimage.binary_opening(Minit, structure = struct_s, iterations = 1)
Minit = ndimage.binary_dilation(Minit, structure = struct_m, iterations = 1)
Maw = np.int8(Minit)
return Maw