-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimaging_utils.py
415 lines (319 loc) · 12.3 KB
/
imaging_utils.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Image preprocessing module
"""
import numpy as np
from scipy import ndimage, misc
from skimage import measure, morphology
from skimage.filters import threshold_otsu, gaussian
import logging
from tqdm import tqdm
import recon_utils as ru
def segment(image, threshold_value):
"""Threshold image.
Parameters
----------
image
Image data.
threshold_value (optional)
Threshold value. If empty an Otsu threshold is calculated.
Returns
-------
BWimage
Binary image after thresholding.
T
Threshold value.
"""
# we do want bone = 1 and background = 0;
if threshold_value is None:
# use Otsu if threshold input not specified
T = threshold_otsu(image)
else:
T = int(threshold_value)
# apply the threshold
return image > T, T
def resample(image, voxelsize, resampling_factor):
"""Resize image.
Parameters
----------
image
Image data.
voxelsize
Voxel size.
resampling_factor
Scaling factor.
Returns
-------
image
Resized image.
voxelsize
Voxel size after rescaling.
"""
# resize the 3D data using spline interpolation of order 2
image = ndimage.zoom(image, 1 / resampling_factor, output=None, order=2)
# correct voxelsize
voxelsize = voxelsize * resampling_factor
return image, voxelsize
def centerofmass(bwimage):
"""Center Of Mass (COM) of binary image.
Parameters
----------
bwimage: bool
Binary image. Can be 2D and 3D.
Returns
-------
cmassx_array
X-coordinate array of the COM. If input is 3D, an array of the slicewise COMs is returned.
cmassy_array
Y-coordinate array of the COM.
"""
if bwimage.ndim == 3:
# output arrays initialization
cmassx_array = np.zeros([bwimage.shape[0]])
cmassy_array = np.zeros([bwimage.shape[0]])
for slice in range(0, bwimage.shape[0]):
y = np.sum(bwimage[slice,:,:], 1)
cmassy = np.inner(y, np.arange(0, y.size))
cmassy_array[slice] = cmassy / np.sum(y)
x = np.sum(bwimage[slice, :, :], 0)
cmassx = np.inner(x, np.arange(0, x.size))
cmassx_array[slice] = cmassx / np.sum(x)
elif bwimage.ndim == 2:
y = np.sum(bwimage, 1)
cmassy = np.inner(y, np.arange(0, y.size))
cmassy_array = cmassy / np.sum(y)
x = np.sum(bwimage, 0)
cmassx = np.inner(x, np.arange(0, x.size))
cmassx_array = cmassx / np.sum(x)
return cmassx_array, cmassy_array
def remove_unconnected(bwimage):
"""Remove all unconnected voxels. Returns a binary of the largest connected cluster.
Parameters
----------
bwimage
Binary image.
Returns
-------
bwcluster
Binary image of the largest connected cluster of voxels.
"""
# label the BW image
# [labels, n_labels] = measure.label(bwimage, None, True)
[labels, n_labels] = measure.label(bwimage, None, True, 1)
# count occurrences of each label
occurrences = np.bincount(labels.reshape(labels.size))
# find largest unconnected label
largest_label_id = occurrences[1:].argmax() + 1
return labels == largest_label_id
def fill_voids(I, fill_val=None, makecopy=False):
"""Fill voids within color image with given value.
Parameters
----------
I
Input color image.
fill_val
Filling value.
makecopy : bool
Make copy of input image.
Returns
-------
I_filled
Filled image.
"""
if fill_val is None:
fill_val = I.max()
# binarize and label inverse of the input image
[labels, n_labels] = measure.label(~(I>0), None, True, 1)
# count occurrences of each label
occurrences = np.bincount(labels.reshape(labels.size))
# find and delete largest label (background)
largest_label_id = occurrences[1:].argmax() + 1
labels[labels == largest_label_id] = 0
if makecopy:
I_filled = I.copy()
I_filled[labels != 0] = fill_val
return I_filled
else:
I[labels != 0] = fill_val
return I
def remove_largest(bwimage):
"""Remove largest cluster of voxels in binary image.
Parameters
----------
bwimage
Binary image.
Returns
-------
bwcluster
Binary image in which the largest cluster of voxels is removed.
"""
# label the BW image
[labels, n_labels] = measure.label(bwimage, None, True, 1)
# count occurrences of each label
occurrences = np.bincount(labels.reshape(labels.size))
# find largest unconnected label
largest_label_id = occurrences[1:].argmax() + 1
labels[labels == largest_label_id] = 0
return labels != 0
def add_cap(I, cap_thickness, cap_val):
"""Add caps to 3D image.
Caps are added on both ends along the Z-direction (first dataset dimension). The thickness and color (Grey Value) of the added caps can be specified.
Parameters
----------
I
3D data. Zeroes as background.
cap_thickness : int
Cap thickness in pixels.
cap_val : float
Cap grey value.
Returns
----------
I_cap
Image with caps added.
"""
I_cap = np.ones([I.shape[0]+2*cap_thickness, I.shape[1], I.shape[2]], I.dtype)*cap_val
I_cap[cap_thickness:-cap_thickness, :, :] = I
return I_cap
def embed(I, embed_depth, embed_dir, embed_val=None, pad=0, makecopy=False):
"""Add embedding to 3D image.
Direction and depth of the embedded region should be given. Zeroes in the input image is considered to be background.
Parameters
----------
I
3D data. Zeroes as background.
embed_depth : int
Embedding depth in pixels.
embed_dir : str
Embedding direction. Can be "-x", "+x", "-y", "+y", "-z", or "+z".
embed_val : float
Embedding grey value.
pad = int
Padding around bounding box of embedded area.
makecopy : bool
Make copy of the input image.
Returns
----------
I
Embedded image. Same size as the input one.
BW_embedding
BW mask of the embedding area.
"""
if embed_val is None:
embed_val = I.max() + 1
# binarize the input image
BW_I = np.zeros(I.shape, dtype='bool')
BW_I[I>0] = True
# init embedding mask
BW_embedding = np.zeros(BW_I.shape, dtype='bool')
if embed_dir == "-z":
dir = -1
# start the embedding at first non-zero voxel
embed_start = np.where(np.max(BW_I.max(1), 1) == True)[-1][-1]
# project embedded area and find size of embedding
bbox_origin, bbox_size = ru.bbox(BW_I[embed_start + (dir * embed_depth):, :, :], pad)
# create embedding mask
BW_embedding[embed_start + (dir * embed_depth):, bbox_origin[0]:bbox_origin[0] + bbox_size[0], bbox_origin[1]:bbox_origin[1] + bbox_size[1]] = True
elif embed_dir == "+z":
dir = 1
# start the embedding at first non-zero voxel
embed_start = np.where(np.max(BW_I.max(1), 1) == True)[0][0]
# project embedded area and find size of embedding
bbox_origin, bbox_size = ru.bbox(BW_I[:embed_start + (dir * embed_depth), :, :], pad)
# create embedding mask
BW_embedding[:embed_start + (dir * embed_depth), bbox_origin[0]:bbox_origin[0] + bbox_size[0], bbox_origin[1]:bbox_origin[1] + bbox_size[1]] = True
elif embed_dir == "-x":
dir = -1
# start the embedding at first non-zero voxel
embed_start = np.where(np.max(BW_I.max(0), 0) == True)[-1][-1]
# project embedded area and find size of embedding
bbox_origin, bbox_size = ru.bbox(BW_I[:, :, embed_start + (dir * embed_depth):], pad)
# create embedding mask
BW_embedding[bbox_origin[2]:bbox_origin[2] + bbox_size[2], bbox_origin[0]:bbox_origin[0] + bbox_size[0], embed_start + (dir * embed_depth):] = True
elif embed_dir == "+x":
dir = +1
# start the embedding at first non-zero voxel
embed_start = np.where(np.max(BW_I.max(0), 0) == True)[0][0]
# project embedded area and find size of embedding
bbox_origin, bbox_size = ru.bbox(BW_I[:, :, :embed_start + (dir * embed_depth)], pad)
# create embedding mask
BW_embedding[bbox_origin[2]:bbox_origin[2] + bbox_size[2], bbox_origin[0]:bbox_origin[0] + bbox_size[0], :embed_start + (dir * embed_depth)] = True
elif embed_dir == "+y":
dir = -1
# start the embedding at first non-zero voxel
embed_start = np.where(np.max(BW_I.max(0), 1) == True)[-1][-1]
# project embedded area and find size of embedding
bbox_origin, bbox_size = ru.bbox(BW_I[:, embed_start + (dir * embed_depth):, :], pad)
# create embedding mask
BW_embedding[bbox_origin[2]:bbox_origin[2] + bbox_size[2], embed_start + (dir * embed_depth):, bbox_origin[1]:bbox_origin[1] + bbox_size[1]] = True
elif embed_dir == "-y":
dir = +1
# start the embedding at first non-zero voxel
embed_start = np.where(np.max(BW_I.max(0), 1) == True)[0][0]
# project embedded area and find size of embedding
bbox_origin, bbox_size = ru.bbox(BW_I[:, :embed_start + (dir * embed_depth), :])
# create embedding mask
BW_embedding[bbox_origin[2]:bbox_origin[2] + bbox_size[2], :embed_start + (dir * embed_depth), bbox_origin[1]:bbox_origin[1] + bbox_size[1]] = True
else:
raise IOError("EMBED_DIR parameter unknown. Valid entries are -x, +x, -y, +y, -z, and +z.")
# emboss embedding mask with the masked input image
BW_embedding = remove_unconnected(BW_embedding & ~BW_I)
# assign embedding val to input image
if makecopy:
I_output = I.copy()
I_output[BW_embedding] = embed_val
return I_output, BW_embedding
else:
I[BW_embedding] = embed_val
return I, BW_embedding
def periosteummask(bwimage, closepixels=10, closevoxels=0, remove_objects_smaller_than=None, removeunconn=True, verbose=False):
"""Binary mask of periosteum (whole bone).
Parameters
----------
bwimage : bool
Binary image. Can be 2D or 3D.
closepixels : int
Radius of DISK structuring element for 2D image closing.
closevoxels : int
Radius of CUBE structuring element for final 3D image closing.
remove_objects_smaller_than : int
Remove objects smaller than given size before periosteum mask calculation.
removeunconn : bool
Remove unconnected clusters of pixels/voxels from the calculated mask.
verbose : bool
Activate verbose output.
Returns
-------
perimask : bool
Binary mask of the whole bone (periosteum mask).
"""
# verbose output
if verbose:
logging.basicConfig(level=logging.INFO)
if remove_objects_smaller_than:
logging.info('Preliminary removal of objects smaller than {} pixels.'.format(remove_objects_smaller_than))
bwimage = morphology.remove_small_objects(bwimage, min_size=remove_objects_smaller_than)
if bwimage.ndim == 3:
# output arrays initialization
perimask = np.zeros(np.shape(bwimage), dtype=bool)
# 2D slice-wise imclose and fill
logging.info('2D slice-wise image closing and filling.\n Structuring element DISK of radius: {}'.format(closepixels))
for slice in tqdm(range(0, bwimage.shape[0])):
perimask[slice,:,:] = ndimage.binary_fill_holes(morphology.binary_closing(bwimage[slice,:,:], morphology.disk(closepixels)))
if removeunconn:
# remove isolated clusters
logging.info("Removing isolated clusters of voxels.")
perimask = remove_unconnected(perimask)
if closevoxels > 0:
# final 3D imclose
logging.info('Final 3D image closing.\n Structuring element CUBE of radius: {}'.format(closepixels))
perimask = morphology.binary_closing(perimask, morphology.cube(closevoxels))
elif bwimage.ndim == 2:
# imclose and fill
perimask = ndimage.binary_fill_holes(morphology.binary_closing(bwimage, morphology.disk(closepixels)))
# remove isolated clusters
if removeunconn:
# remove isolated clusters
logging.info("Removing isolated clusters of voxels.")
perimask = remove_unconnected(perimask)
return perimask