[5d12a0]: / ants / label / image_to_cluster_images.py

Download this file

54 lines (41 with data), 1.4 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
__all__ = ['image_to_cluster_images']
import numpy as np
import ants
from ants.decorators import image_method
@image_method
def image_to_cluster_images(image, min_cluster_size=50, min_thresh=1e-06, max_thresh=1):
"""
Converts an image to several independent images.
Produces a unique image for each connected
component 1 through N of size > min_cluster_size
ANTsR function: `image2ClusterImages`
Arguments
---------
image : ANTsImage
input image
min_cluster_size : integer
throw away clusters smaller than this value
min_thresh : scalar
threshold to a statistical map
max_thresh : scalar
threshold to a statistical map
Returns
-------
list of ANTsImage types
Example
-------
>>> import ants
>>> image = ants.image_read(ants.get_ants_data('r16'))
>>> image = ants.threshold_image(image, 1, 1e15)
>>> image_cluster_list = ants.image_to_cluster_images(image)
"""
if not ants.is_image(image):
raise ValueError('image must be ANTsImage type')
clust = ants.label_clusters(image, min_cluster_size, min_thresh, max_thresh)
labs = np.unique(clust[clust > 0])
clustlist = []
for i in range(len(labs)):
labimage = image.clone()
labimage[clust != labs[i]] = 0
clustlist.append(labimage)
return clustlist