a b/ants/math/get_centroids.py
1
__all__ = ["get_centroids"]
2
3
import numpy as np
4
import ants
5
from ants.decorators import image_method
6
7
@image_method
8
def get_centroids(image, clustparam=0):
9
    """
10
    Reduces a variate/statistical/network image to a set of centroids
11
    describing the center of each stand-alone non-zero component in the image
12
13
    ANTsR function: `getCentroids`
14
15
    Arguments
16
    ---------
17
    image : ANTsImage
18
        image from which centroids will be calculated
19
20
    clustparam : integer
21
        look at regions greater than or equal to this size
22
23
    Returns
24
    -------
25
    ndarray
26
27
    Example
28
    -------
29
    >>> import ants
30
    >>> image = ants.image_read( ants.get_ants_data( "r16" ) )
31
    >>> image = ants.threshold_image( image, 90, 120 )
32
    >>> image = ants.label_clusters( image, 10 )
33
    >>> cents = ants.get_centroids( image )
34
    """
35
    imagedim = image.dimension
36
    if clustparam > 0:
37
        mypoints = ants.label_clusters(image, clustparam, max_thresh=1e15)
38
    if clustparam == 0:
39
        mypoints = image.clone()
40
    mypoints = ants.label_stats(mypoints, mypoints)
41
    nonzero = mypoints[["LabelValue"]] > 0
42
    mypoints = mypoints[nonzero["LabelValue"]]
43
    mypoints = mypoints.iloc[:, :]
44
    x = mypoints.x
45
    y = mypoints.y
46
47
    if imagedim == 3:
48
        z = mypoints.z
49
    else:
50
        z = np.zeros(mypoints.shape[0])
51
52
    if imagedim == 4:
53
        t = mypoints.t
54
    else:
55
        t = np.zeros(mypoints.shape[0])
56
57
    centroids = np.stack([x, y, z, t]).T
58
    return centroids