-
Notifications
You must be signed in to change notification settings - Fork 0
/
segmentation.py
164 lines (99 loc) · 3.94 KB
/
segmentation.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
#!/usr/bin/env python2.7
import tables
import cv2
import numpy
import pandas
from joblib import Parallel, delayed
from tqdm import trange
from dsbTests import tsvFile
print(cv2.__version__)
def segmentSlice(slice):
sliceC = slice.copy()
# threshold HU > -300
sliceC[sliceC > -300] = 255
sliceC[sliceC < -300] = 0
sliceC = numpy.uint8(sliceC)
# find surrounding torso from the threshold and make a mask
im2, contours, _ = cv2.findContours(sliceC, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
#print contours
largest_contour = max(contours, key=cv2.contourArea)
mask = numpy.zeros(sliceC.shape, numpy.uint8)
cv2.fillPoly(mask, [largest_contour], 255)
# apply mask to threshold image to remove outside. this is our new mask
sliceC = ~sliceC
sliceC[(mask == 0)] = 0 # <-- Larger than threshold value
# apply closing to the mask
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (15, 15))
sliceC = cv2.morphologyEx(sliceC, cv2.MORPH_OPEN, kernel) # <- to remove speckles...
sliceC = cv2.morphologyEx(sliceC, cv2.MORPH_DILATE, kernel)
sliceC = cv2.morphologyEx(sliceC, cv2.MORPH_DILATE, kernel)
sliceC = cv2.morphologyEx(sliceC, cv2.MORPH_CLOSE, kernel)
sliceC = cv2.morphologyEx(sliceC, cv2.MORPH_CLOSE, kernel)
sliceC = cv2.morphologyEx(sliceC, cv2.MORPH_ERODE, kernel)
sliceC = cv2.morphologyEx(sliceC, cv2.MORPH_ERODE, kernel)
# apply mask to image
img2 = slice.copy()
img2[(sliceC == 0)] = -2000 # <-- Larger than threshold value
# closing
# sorted_contours = sorted(contours, key=cv2.contourArea, reverse=True)
# largest_contour = max(contours, key=cv2.contourArea)
# rgb = cv2.cvtColor(img,cv2.COLOR_GRAY2RGB)
# aaa = np.concatenate( sorted_contours[1:3] )
# cv2.drawContours(rgb, [cv2.convexHull(aaa)], -1, (0,255,0), 3)
return img2
def segmentVol(vol):
#newVol = []
#for slice in vol:
# segSlice = segmentSlice(slice)
# newVol.append(segSlice)
threads = 5
newVol = Parallel(n_jobs=threads, backend='threading')(delayed(segmentSlice)(slice) for slice in vol)
newVol = numpy.asarray(newVol)
return newVol
#imagesDF = pandas.read_csv(DATADIR + 'resampledImages.tsv', sep='\t')
#DB = tables.open_file(DATADIR + 'resampled.h5', mode='r')
#imageArray = DB.root.resampled
# For testing
def testSegmentation():
DATADIR = '/data/datasets/luna/resampled_order1/'
tsvFile = DATADIR + 'nodules.tsv'
arrayFile = DATADIR + 'resampled.h5'
DB = tables.open_file(arrayFile, mode='r')
array = DB.root.resampled
DF = pandas.read_csv(tsvFile, sep='\t')
row = DF.iloc[200]
#DF = DF[DF.imgNum==2] # we've got a specific nodule in mind
image = array[int(row['imgNum'])]
image = segmentVol(image)
voxelC = row[['voxelZ', 'voxelY', 'voxelX']].as_matrix()
from extractCubes import extractCubeAtLocation
cube = extractCubeAtLocation(image, voxelC, 40)
numpy.save('cube.npy', cube)
def segmentCollection(arrayFile, arrayOut):
DBi = tables.open_file(arrayFile, mode='r')
array = DBi.root.resampled
DFi = pandas.read_csv(tsvFile, sep='\t')
IMG_SHAPE = array[0].shape
nVols = len(array)
DBo = tables.open_file(arrayOut, mode='w')
filters = tables.Filters(complevel=1, complib='blosc:snappy') # 7.7sec / 1.2 GB (14 sec 1015MB if precision is reduced) 140s 3.7GB
images = DBo.create_carray(DBo.root, 'resampled', atom=tables.Int16Atom(shape=IMG_SHAPE), shape=(nVols,), filters=filters)
print nVols
for index in trange(nVols):
vol = array[index]
seg = segmentVol(vol)
print 'seg: ', seg.min(), seg.mean(), seg.max()
#images.append([seg])
images[index] = seg
if __name__ == '__main__':
#testSegmentation()
#sys.exit(0)
DATADIR = '/data/datasets/lung/resampled_order1/'
arrayFile = DATADIR + 'resampled.h5'
arrayOut = DATADIR + 'segmented.h5'
segmentCollection(arrayFile, arrayOut)
# luna, not lung! nearly the same but different dataset
DATADIR = '/data/datasets/luna/resampled_order1/'
arrayFile = DATADIR + 'resampled.h5'
arrayOut = DATADIR + 'segmented.h5'
segmentCollection(arrayFile, arrayOut)