{ } Raw JSON

bundles / skimage latest / skimage / morphology / misc / remove_objects_by_distance

function

skimage.morphology.misc:remove_objects_by_distance

source: /dev/scikit-image/src/skimage/morphology/misc.py :271

Signature

def   remove_objects_by_distance ( label_image min_distance * priority = None p_norm = 2 spacing = None out = None )

Summary

Remove objects, in specified order, until remaining are a minimum distance apart.

Extended Summary

Remove labeled objects from an image until the remaining ones are spaced more than a given distance from one another. By default, smaller objects are removed first.

Parameters

label_image : ndarray of integers

An n-dimensional array containing object labels, e.g. as returned by ~.label. A value of zero is considered background, all other object IDs must be positive integers.

min_distance : int or float

Remove objects whose distance to other objects is not greater than this positive value. Objects with a lower priority are removed first.

priority : ndarray, optional

Defines the priority with which objects are removed. Expects a 1-dimensional array of length np.amax(label_image) + 1 <numpy.amax> that contains the priority for each object's label at the respective index. Objects with a lower value are removed first until all remaining objects fulfill the distance requirement. If not given, priority is given to objects with a higher number of samples and their label value second.

p_norm : int or float, optional

The Minkowski distance of order p, used to calculate the distance between objects. The default 2 corresponds to the Euclidean distance, 1 to the "Manhattan" distance, and np.inf to the Chebyshev distance.

spacing : sequence of float, optional

The pixel spacing along each axis of label_image. If not specified, a grid spacing of unity (1) is implied.

out : ndarray, optional

Array of the same shape and dtype as image, into which the output is placed. By default, a new array is created.

Returns

out : ndarray

Array of the same shape as label_image, for which objects that violate the min_distance condition were removed.

Notes

The basic steps of this algorithm work as follows:

  • Find the indices for of all given objects and separate them depending on if they point to an object's border or not.

  • Sort indices by their label value, ensuring that indices which point to the same object are next to each other. This optimization allows finding all parts of an object, simply by stepping to the neighboring indices.

  • Sort boundary indices by priority. Use a stable-sort to preserve the ordering from the previous sorting step. If priority is not given, use numpy.bincount as a fallback.

  • Construct a scipy.spatial.cKDTree from the boundary indices.

  • Iterate across boundary indices in priority-sorted order, and query the kd-tree for objects that are too close. Remove ones that are and don't take them into account when evaluating other objects later on.

The performance of this algorithm depends on the number of samples in label_image that belong to an object's border.

Examples

import skimage as ski
ski.morphology.remove_objects_by_distance(np.array([2, 0, 1, 1]), 2)
ski.morphology.remove_objects_by_distance(
    np.array([2, 0, 1, 1]), 2, priority=np.array([0, 1, 9])
)
label_image = np.array(
    [[8, 0, 0, 0, 0, 0, 0, 0, 0, 9, 9],
     [8, 8, 8, 0, 0, 0, 0, 0, 0, 9, 9],
     [0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 3, 0, 0, 0, 1, 0, 0, 0, 0],
     [2, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 7]]
)
ski.morphology.remove_objects_by_distance(
    label_image, min_distance=3
)

See also

skimage.morphology.remove_small_holes

Remove holes smaller than the specified size.

skimage.morphology.remove_small_objects

Remove objects smaller than the specified size.

Aliases

  • skimage.morphology.remove_objects_by_distance