Python scipy.ndimage.center_of_mass() Examples

The following are 20 code examples of scipy.ndimage.center_of_mass(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module scipy.ndimage , or try the search function .
Example #1
Source File: test_measurements.py    From GraphicDesignPatternByPython with MIT License 5 votes vote down vote up
def test_center_of_mass02():
    expected = [1, 0]
    for type in types:
        input = np.array([[0, 0], [1, 0]], type)
        output = ndimage.center_of_mass(input)
        assert_array_almost_equal(output, expected) 
Example #2
Source File: center.py    From PyAbel with MIT License 5 votes vote down vote up
def find_origin_by_center_of_mass(IM, verbose=False, round_output=False,
                                  **kwargs):
    """
    Find image origin by calculating its center of mass.

    Parameters
    ----------
    IM : numpy 2D array
        image data

    round_output : bool
        if ``True``, the coordinates are rounded to integers;
        otherwise they are floats.

    Returns
    -------
    origin : tuple
        (row, column)
    """
    origin = center_of_mass(IM)

    if verbose:
        to_print = "Center of mass at ({0}, {1})".format(origin[0], origin[1])

    if round_output:
        origin = (round(origin[0]), round(origin[1]))
        if verbose:
            to_print += " ... round to ({0}, {1})".format(origin[0], origin[1])

    if verbose:
        print(to_print)

    return origin 
Example #3
Source File: utils.py    From abagen with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def get_centroids(image, labels=None, image_space=False):
    """
    Finds centroids of ``labels`` in ``label_image``

    Parameters
    ----------
    label_image : niimg-like object
        3D image containing integer label at each point
    labels : array_like, optional
        List of values containing labels of which to find centroids. Default:
        all possible labels
    image_space : bool, optional
        Whether to return xyz (image space) coordinates for centroids based
        on transformation in ``label_image.affine``. Default: False

    Returns
    -------
    centroids : (N, 3) np.ndarray
        Coordinates of centroids for ROIs in input data
    """

    image = check_img(image)
    data = np.asarray(image.dataobj)

    # if no labels of interest provided, get all possible labels
    if labels is None:
        labels = np.trim_zeros(np.unique(data))

    # get centroids for all possible labels
    centroids = np.row_stack(center_of_mass(data, labels=data, index=labels))

    # return xyz if desired; otherwise, ijk
    if image_space:
        centroids = ijk_to_xyz(centroids, image.affine)

    return centroids 
Example #4
Source File: psf.py    From prysm with MIT License 5 votes vote down vote up
def centroid(self, unit='spatial'):
        """Calculate the centroid of the PSF.

        Parameters
        ----------
        unit : `str`, {'spatial', 'pixels'}
            unit to return the centroid in.
            If pixels, corner indexed.  If spatial, center indexed.

        Returns
        -------
        `int`, `int`
            if unit == pixels, indices into the array
        `float`, `float`
            if unit == spatial, referenced to the origin

        """
        from scipy.ndimage import center_of_mass
        com = center_of_mass(self.data)
        if unit != 'spatial':
            return com
        else:
            # tuple - cast from generator
            # sample spacing - indices to units
            # x-c -- index shifted from center
            return tuple(self.sample_spacing * (x-c) for x, c in zip(com, (self.center_y, self.center_x))) 
Example #5
Source File: test_measurements.py    From GraphicDesignPatternByPython with MIT License 5 votes vote down vote up
def test_center_of_mass09():
    labels = [1, 2]
    expected = [(0.5, 0.0), (0.5, 1.0)]
    input = np.array([[1, 2], [1, 1]], bool)
    output = ndimage.center_of_mass(input, labels, [1, 2])
    assert_array_almost_equal(output, expected) 
Example #6
Source File: test_measurements.py    From GraphicDesignPatternByPython with MIT License 5 votes vote down vote up
def test_center_of_mass08():
    labels = [1, 2]
    expected = [0.5, 1.0]
    input = np.array([[5, 2], [3, 1]], bool)
    output = ndimage.center_of_mass(input, labels, 2)
    assert_array_almost_equal(output, expected) 
Example #7
Source File: test_measurements.py    From GraphicDesignPatternByPython with MIT License 5 votes vote down vote up
def test_center_of_mass07():
    labels = [1, 0]
    expected = [0.5, 0.0]
    input = np.array([[1, 2], [3, 1]], bool)
    output = ndimage.center_of_mass(input, labels)
    assert_array_almost_equal(output, expected) 
Example #8
Source File: test_measurements.py    From GraphicDesignPatternByPython with MIT License 5 votes vote down vote up
def test_center_of_mass05():
    expected = [0.5, 0.5]
    for type in types:
        input = np.array([[1, 1], [1, 1]], type)
        output = ndimage.center_of_mass(input)
        assert_array_almost_equal(output, expected) 
Example #9
Source File: test_measurements.py    From GraphicDesignPatternByPython with MIT License 5 votes vote down vote up
def test_center_of_mass04():
    expected = [1, 1]
    for type in types:
        input = np.array([[0, 0], [0, 1]], type)
        output = ndimage.center_of_mass(input)
        assert_array_almost_equal(output, expected) 
Example #10
Source File: test_measurements.py    From GraphicDesignPatternByPython with MIT License 5 votes vote down vote up
def test_center_of_mass03():
    expected = [0, 1]
    for type in types:
        input = np.array([[0, 1], [0, 0]], type)
        output = ndimage.center_of_mass(input)
        assert_array_almost_equal(output, expected) 
Example #11
Source File: _get_clusters_table.py    From nistats with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _identify_subpeaks(data):
    # Initial identification of subpeaks with minimal minimum distance
    data_max = ndimage.filters.maximum_filter(data, 3)
    maxima = (data == data_max)
    data_min = ndimage.filters.minimum_filter(data, 3)
    diff = ((data_max - data_min) > 0)
    maxima[diff == 0] = 0

    labeled, n_subpeaks = ndimage.label(maxima)
    labels_index = range(1, n_subpeaks + 1)
    ijk = np.array(ndimage.center_of_mass(data, labeled, labels_index))
    ijk = np.round(ijk).astype(int)
    vals = np.apply_along_axis(arr=ijk, axis=1, func1d=_get_val,
                               input_arr=data)
    return ijk, vals 
Example #12
Source File: test_measurements.py    From GraphicDesignPatternByPython with MIT License 5 votes vote down vote up
def test_center_of_mass01():
    expected = [0.0, 0.0]
    for type in types:
        input = np.array([[1, 0], [0, 0]], type)
        output = ndimage.center_of_mass(input)
        assert_array_almost_equal(output, expected) 
Example #13
Source File: test_measurements.py    From Computable with MIT License 5 votes vote down vote up
def test_center_of_mass09():
    "center of mass 9"
    labels = [1, 2]
    expected = [(0.5, 0.0), (0.5, 1.0)]
    input = np.array([[1, 2], [1, 1]], bool)
    output = ndimage.center_of_mass(input, labels, [1, 2])
    assert_array_almost_equal(output, expected) 
Example #14
Source File: test_measurements.py    From Computable with MIT License 5 votes vote down vote up
def test_center_of_mass08():
    "center of mass 8"
    labels = [1, 2]
    expected = [0.5, 1.0]
    input = np.array([[5, 2], [3, 1]], bool)
    output = ndimage.center_of_mass(input, labels, 2)
    assert_array_almost_equal(output, expected) 
Example #15
Source File: test_measurements.py    From Computable with MIT License 5 votes vote down vote up
def test_center_of_mass07():
    "center of mass 7"
    labels = [1, 0]
    expected = [0.5, 0.0]
    input = np.array([[1, 2], [3, 1]], bool)
    output = ndimage.center_of_mass(input, labels)
    assert_array_almost_equal(output, expected) 
Example #16
Source File: test_measurements.py    From Computable with MIT License 5 votes vote down vote up
def test_center_of_mass05():
    "center of mass 5"
    expected = [0.5, 0.5]
    for type in types:
        input = np.array([[1, 1], [1, 1]], type)
        output = ndimage.center_of_mass(input)
        assert_array_almost_equal(output, expected) 
Example #17
Source File: test_measurements.py    From Computable with MIT License 5 votes vote down vote up
def test_center_of_mass04():
    "center of mass 4"
    expected = [1, 1]
    for type in types:
        input = np.array([[0, 0], [0, 1]], type)
        output = ndimage.center_of_mass(input)
        assert_array_almost_equal(output, expected) 
Example #18
Source File: test_measurements.py    From Computable with MIT License 5 votes vote down vote up
def test_center_of_mass03():
    "center of mass 3"
    expected = [0, 1]
    for type in types:
        input = np.array([[0, 1], [0, 0]], type)
        output = ndimage.center_of_mass(input)
        assert_array_almost_equal(output, expected) 
Example #19
Source File: test_measurements.py    From Computable with MIT License 5 votes vote down vote up
def test_center_of_mass02():
    "center of mass 2"
    expected = [1, 0]
    for type in types:
        input = np.array([[0, 0], [1, 0]], type)
        output = ndimage.center_of_mass(input)
        assert_array_almost_equal(output, expected) 
Example #20
Source File: test_measurements.py    From Computable with MIT License 5 votes vote down vote up
def test_center_of_mass01():
    "center of mass 1"
    expected = [0.0, 0.0]
    for type in types:
        input = np.array([[1, 0], [0, 0]], type)
        output = ndimage.center_of_mass(input)
        assert_array_almost_equal(output, expected)