skimage2.morphology.remove_objects_by_distance#
- skimage2.morphology.remove_objects_by_distance(label_image, min_distance, *, priority=None, p_norm=2, spacing=None, out=None)[source]#
Remove objects, in specified order, until remaining are a minimum distance apart.
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_imagendarray of integers
An n-dimensional array containing object labels, e.g. as returned by
skimage2.measure.label(). A value of zero is considered background, all other object IDs must be positive integers.- min_distanceint or float
Remove objects whose distance to other objects is not greater than this positive value. Objects with a lower
priorityare removed first.- priorityndarray, optional
Defines the priority with which objects are removed. Expects a 1-dimensional array of length
np.amax(label_image) + 1that 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_normint or float, optional
The Minkowski distance of order p, used to calculate the distance between objects. The default
2corresponds to the Euclidean distance,1to the “Manhattan” distance, andnp.infto the Chebyshev distance.- spacingsequence of float, optional
The pixel spacing along each axis of
label_image. If not specified, a grid spacing of unity (1) is implied.- outndarray, optional
Array of the same shape and dtype as
image, into which the output is placed. By default, a new array is created.
- Returns:
- outndarray
Array of the same shape as
label_image, for which objects that violate themin_distancecondition were removed.
See also
skimage.morphology.remove_small_objectsRemove objects smaller than the specified size.
skimage.morphology.remove_small_holesRemove holes smaller than the specified size.
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. Ifpriorityis not given, usenumpy.bincount()as a fallback.Construct a
scipy.spatial.cKDTreefrom 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_imagethat belong to an object’s border.Examples
>>> import _skimage2 as ski2 >>> ski2.morphology.remove_objects_by_distance(np.array([2, 0, 1, 1]), 2) array([0, 0, 1, 1]) >>> ski2.morphology.remove_objects_by_distance( ... np.array([2, 0, 1, 1]), 2, priority=np.array([0, 1, 9]) ... ) array([2, 0, 0, 0]) >>> 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]] ... ) >>> ski2.morphology.remove_objects_by_distance( ... label_image, min_distance=3 ... ) 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, 0, 0, 0, 0, 0, 0, 0, 0, 0], [2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 7]])