[5d12a0]: / ants / ops / get_mask.py

Download this file

76 lines (59 with data), 2.5 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
__all__ = ['get_mask']
import ants
from ants.decorators import image_method
@image_method
def get_mask(image, low_thresh=None, high_thresh=None, cleanup=2):
"""
Get a binary mask image from the given image after thresholding
ANTsR function: `getMask`
Arguments
---------
image : ANTsImage
image from which mask will be computed. Can be an antsImage of 2, 3 or 4 dimensions.
low_thresh : scalar (optional)
An inclusive lower threshold for voxels to be included in the mask.
If not given, defaults to image mean.
high_thresh : scalar (optional)
An inclusive upper threshold for voxels to be included in the mask.
If not given, defaults to image max
cleanup : integer
If > 0, morphological operations will be applied to clean up the mask by eroding away small or weakly-connected areas, and closing holes.
If cleanup is >0, the following steps are applied
1. Erosion with radius 2 voxels
2. Retain largest component
3. Dilation with radius 1 voxel
4. Morphological closing
Returns
-------
ANTsImage
Example
-------
>>> import ants
>>> image = ants.image_read( ants.get_ants_data('r16') )
>>> mask = ants.get_mask(image)
"""
cleanup = int(cleanup)
if ants.is_image(image):
if image.pixeltype != 'float':
image = image.clone('float')
if low_thresh is None:
low_thresh = image.mean()
if high_thresh is None:
high_thresh = image.max()
mask_image = ants.threshold_image(image, low_thresh, high_thresh)
if cleanup > 0:
mask_image = ants.iMath(mask_image, 'ME', cleanup)
mask_image = ants.iMath(mask_image, 'GetLargestComponent')
mask_image = ants.iMath(mask_image, 'MD', cleanup)
mask_image = ants.iMath(mask_image, 'FillHoles').threshold_image( 1, 2 )
while ((mask_image.min() == mask_image.max()) and (cleanup > 0)):
cleanup = cleanup - 1
mask_image = ants.threshold_image(image, low_thresh, high_thresh)
if cleanup > 0:
mask_image = ants.iMath(mask_image, 'ME', cleanup)
mask_image = ants.iMath(mask_image, 'MD', cleanup)
mask_image = ants.iMath(mask_image, 'FillHoles').threshold_image( 1, 2 )
#if cleanup == 0:
# clustlab = ants.label_clusters(mask_image, 1)
# mask_image = ants.threshold_image(clustlab, 1, 1)
return mask_image