|
a |
|
b/ants/ops/mask_image.py |
|
|
1 |
__all__ = ['mask_image'] |
|
|
2 |
|
|
|
3 |
import numpy as np |
|
|
4 |
|
|
|
5 |
import ants |
|
|
6 |
from ants.decorators import image_method |
|
|
7 |
|
|
|
8 |
@image_method |
|
|
9 |
def mask_image(image, mask, level=1, binarize=False): |
|
|
10 |
""" |
|
|
11 |
Mask an input image by a mask image. If the mask image has multiple labels, |
|
|
12 |
it is possible to specify which label(s) to mask at. |
|
|
13 |
|
|
|
14 |
ANTsR function: `maskImage` |
|
|
15 |
|
|
|
16 |
Arguments |
|
|
17 |
--------- |
|
|
18 |
image : ANTsImage |
|
|
19 |
Input image. |
|
|
20 |
|
|
|
21 |
mask : ANTsImage |
|
|
22 |
Mask or label image. |
|
|
23 |
|
|
|
24 |
level : scalar or tuple of scalars |
|
|
25 |
Level(s) at which to mask image. If vector or list of values, output image is non-zero at all locations where label image matches any of the levels specified. |
|
|
26 |
|
|
|
27 |
binarize : boolean |
|
|
28 |
whether binarize the output image |
|
|
29 |
|
|
|
30 |
Returns |
|
|
31 |
------- |
|
|
32 |
ANTsImage |
|
|
33 |
|
|
|
34 |
Example |
|
|
35 |
------- |
|
|
36 |
>>> import ants |
|
|
37 |
>>> myimage = ants.image_read(ants.get_ants_data('r16')) |
|
|
38 |
>>> mask = ants.get_mask(myimage) |
|
|
39 |
>>> myimage_mask = ants.mask_image(myimage, mask, 3) |
|
|
40 |
>>> seg = ants.kmeans_segmentation(myimage, 3) |
|
|
41 |
>>> myimage_mask = ants.mask_image(myimage, seg['segmentation'], (1,3)) |
|
|
42 |
""" |
|
|
43 |
leveluse = level |
|
|
44 |
if type(leveluse) is np.ndarray: |
|
|
45 |
leveluse = level.tolist() |
|
|
46 |
if type(leveluse) is int or type(leveluse) is float: |
|
|
47 |
leveluse = [level] |
|
|
48 |
image_out = image.clone() * 0 |
|
|
49 |
for mylevel in leveluse: |
|
|
50 |
temp = ants.threshold_image(mask, mylevel, mylevel) |
|
|
51 |
if binarize: |
|
|
52 |
image_out = image_out + temp |
|
|
53 |
else: |
|
|
54 |
image_out = image_out + temp * image |
|
|
55 |
return image_out |