skimage2.morphology#

Warning

This module is part of the experimental skimage2 namespace and is subject to change without notice. Do not use it in production code. See the migration guide for more details.

Morphological algorithms, for example, closing, opening, and skeletonization.

black_tophat

Return black top hat of an image.

closing

Return grayscale morphological closing of an image.

dilation

Return grayscale morphological dilation of an image.

erosion

Return grayscale morphological erosion of an image.

mirror_footprint

Mirror each dimension in the (decomposed) footprint.

opening

Return grayscale morphological opening of an image.

pad_footprint

Pad the (decomposed) footprint to an odd size along each dimension.

white_tophat

Return white top hat of an image.


skimage2.morphology.black_tophat(image, footprint=None, *, out=None, mode='ignore', cval=0.0)[source]#

Return black top hat of an image.

The black top hat of an image is defined as its morphological closing minus the original image. This operation returns the dark spots of the image that are smaller than the footprint. Note that dark spots in the original image are bright spots after the black top hat.

Parameters:
imagendarray

Input image.

footprintndarray or tuple, optional

The neighborhood expressed as a 2-D array of 1’s and 0’s. If None, use a cross-shaped footprint (so-called 1-connectivity). The footprint can also be provided as a sequence of smaller footprints as described in the notes below. See _Notes_ for more.

outndarray, optional

The array to store the result of the morphology. If None, a new array is allocated.

modestr, optional

The mode parameter determines how the array borders are handled. Valid modes are: ‘reflect’, ‘constant’, ‘nearest’, ‘mirror’, ‘wrap’, ‘max’, ‘min’, or ‘ignore’. See skimage2.morphology.closing(). Default is ‘ignore’.

cvalscalar, optional

Value to fill past edges of input if mode is ‘constant’. Default is 0.0.

Returns:
outndarray, same shape and dtype as image

The result of the morphological black top hat.

Notes

The footprint can also be a provided as a sequence of 2-tuples where the first element of each 2-tuple is a footprint ndarray and the second element is an integer describing the number of times it should be iterated. For example footprint=[(np.ones((9, 1)), 1), (np.ones((1, 9)), 1)] would apply a 9x1 footprint followed by a 1x9 footprint resulting in a net effect that is the same as footprint=np.ones((9, 9)), but with lower computational cost. Most of the built-in footprints such as skimage2.morphology.disk() provide an option to automatically generate a footprint sequence of this type. Refer to the example Decompose flat footprints (structuring elements) for more insights.

If footprint contains even-sized dimensions, they are padded with zeros to an odd size at the front (at index 0) with pad_footprint().

References

Examples

>>> # Change dark peak to bright peak and subtract background
>>> import numpy as np
>>> from skimage.morphology import footprint_rectangle
>>> dark_on_gray = np.array([[7, 6, 6, 6, 7],
...                          [6, 5, 4, 5, 6],
...                          [6, 4, 0, 4, 6],
...                          [6, 5, 4, 5, 6],
...                          [7, 6, 6, 6, 7]], dtype=np.uint8)
>>> black_tophat(dark_on_gray, footprint_rectangle((3, 3)))
array([[0, 0, 0, 0, 0],
       [0, 0, 1, 0, 0],
       [0, 1, 5, 1, 0],
       [0, 0, 1, 0, 0],
       [0, 0, 0, 0, 0]], dtype=uint8)

skimage2.morphology.closing(image, footprint=None, *, out=None, mode='ignore', cval=0.0)[source]#

Return grayscale morphological closing of an image.

The morphological closing of an image is defined as a dilation followed by an erosion. Closing can remove small dark spots (i.e., “pepper”) and connect small bright cracks. This tends to “close” up (dark) gaps between (bright) features.

Parameters:
imagendarray

Input image.

footprintndarray or tuple, optional

The neighborhood expressed as a 2-D array of 1’s and 0’s. If None, use a cross-shaped footprint (so-called 1-connectivity). The footprint can also be provided as a sequence of smaller footprints as described in the notes below. See _Notes_ for more.

outndarray, optional

The array to store the result of the morphology. If None, a new array is allocated.

modestr, optional

The mode parameter determines how the array borders are handled. Valid modes are: ‘reflect’, ‘constant’, ‘nearest’, ‘mirror’, ‘wrap’, ‘max’, ‘min’, or ‘ignore’. If ‘ignore’, pixels outside the image domain are assumed to be the maximum for the image’s dtype in the erosion, and minimum in the dilation, which causes them to not influence the result. Default is ‘ignore’.

cvalscalar, optional

Value to fill past edges of input if mode is ‘constant’. Default is 0.0.

Returns:
outndarray, same shape and dtype as image

The result of the morphological closing.

Notes

The footprint can also be a provided as a sequence of 2-tuples where the first element of each 2-tuple is a footprint ndarray and the second element is an integer describing the number of times it should be iterated. For example footprint=[(np.ones((9, 1)), 1), (np.ones((1, 9)), 1)] would apply a 9x1 footprint followed by a 1x9 footprint resulting in a net effect that is the same as footprint=np.ones((9, 9)), but with lower computational cost. Most of the built-in footprints such as skimage2.morphology.disk() provide an option to automatically generate a footprint sequence of this type. Refer to the example Decompose flat footprints (structuring elements) for more insights.

If footprint contains even-sized dimensions, they are padded with zeros to an odd size at the front (at index 0) with pad_footprint().

Examples

>>> # Close a gap between two bright lines
>>> import numpy as np
>>> from skimage.morphology import footprint_rectangle
>>> broken_line = np.array([[0, 0, 0, 0, 0],
...                         [0, 0, 0, 0, 0],
...                         [1, 1, 0, 1, 1],
...                         [0, 0, 0, 0, 0],
...                         [0, 0, 0, 0, 0]], dtype=np.uint8)
>>> closing(broken_line, footprint_rectangle((3, 3)))
array([[0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0],
       [1, 1, 1, 1, 1],
       [0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0]], dtype=uint8)

skimage2.morphology.dilation(image, footprint=None, *, out=None, mode='ignore', cval=0.0)[source]#

Return grayscale morphological dilation of an image.

Morphological dilation enlarges bright regions and shrinks dark regions. It assigns each pixel the maximum of the active neighborhood of that pixel. The values where footprint is 1 define this active neighborhood.

Parameters:
imagendarray

Input image.

footprintndarray or tuple, optional

The neighborhood expressed as a 2-D array of 1’s and 0’s. If None, use a cross-shaped footprint (so-called 1-connectivity). The footprint can also be provided as a sequence of smaller footprints as described in the notes below. See _Notes_ for more.

outndarray, optional

The array to store the result of the morphology. If None, a new array is allocated.

modestr, optional

The mode parameter determines how the array borders are handled. Valid modes are: ‘reflect’, ‘constant’, ‘nearest’, ‘mirror’, ‘wrap’, ‘max’, ‘min’, or ‘ignore’. If ‘min’ or ‘ignore’, pixels outside the image domain are assumed to be the maximum for the image’s dtype, which causes them to not influence the result. Default is ‘ignore’.

cvalscalar, optional

Value to fill past edges of input if mode is ‘constant’. Default is 0.0.

Returns:
outndarray, same shape and dtype as image

The result of the morphological dilation.

Notes

For uint8 (and uint16 up to a certain bit-depth) data, the lower algorithm complexity makes the skimage2.filters.rank.maximum() function more efficient for larger images and footprints.

The footprint can also be a provided as a sequence of 2-tuples where the first element of each 2-tuple is a footprint ndarray and the second element is an integer describing the number of times it should be iterated. For example footprint=[(np.ones((9, 1)), 1), (np.ones((1, 9)), 1)] would apply a 9x1 footprint followed by a 1x9 footprint resulting in a net effect that is the same as footprint=np.ones((9, 9)), but with lower computational cost. Most of the built-in footprints such as skimage2.morphology.disk() provide an option to automatically generate a footprint sequence of this type. Refer to the example Decompose flat footprints (structuring elements) for more insights.

If footprint contains even-sized dimensions, they are padded with zeros to an odd size at the front (at index 0) with pad_footprint().

Examples

>>> # Dilation enlarges bright regions
>>> import numpy as np
>>> from skimage.morphology import footprint_rectangle
>>> bright_pixel = np.array([[0, 0, 0, 0, 0],
...                          [0, 0, 0, 0, 0],
...                          [0, 0, 1, 0, 0],
...                          [0, 0, 0, 0, 0],
...                          [0, 0, 0, 0, 0]], dtype=np.uint8)
>>> dilation(bright_pixel, footprint_rectangle((3, 3)))
array([[0, 0, 0, 0, 0],
       [0, 1, 1, 1, 0],
       [0, 1, 1, 1, 0],
       [0, 1, 1, 1, 0],
       [0, 0, 0, 0, 0]], dtype=uint8)

skimage2.morphology.erosion(image, footprint=None, *, out=None, mode='ignore', cval=0.0)[source]#

Return grayscale morphological erosion of an image.

Morphological erosion shrinks bright regions and enlarges dark regions. It assigns each pixel the minimum of the active neighborhood of that pixel. The values where footprint is 1 define this active neighborhood.

Parameters:
imagendarray

Input image.

footprintndarray or tuple, optional

The neighborhood expressed as a 2-D array of 1’s and 0’s. If None, use a cross-shaped footprint (so-called 1-connectivity). The footprint can also be provided as a sequence of smaller footprints as described in the notes below. See _Notes_ for more.

outndarray, optional

The array to store the result of the morphology. If None, a new array is allocated.

modestr, optional

The mode parameter determines how the array borders are handled. Valid modes are: ‘reflect’, ‘constant’, ‘nearest’, ‘mirror’, ‘wrap’, ‘max’, ‘min’, or ‘ignore’. If ‘max’ or ‘ignore’, pixels outside the image domain are assumed to be the maximum for the image’s dtype, which causes them to not influence the result. Default is ‘ignore’.

cvalscalar, optional

Value to fill past edges of input if mode is ‘constant’. Default is 0.0.

Returns:
outndarray, same shape and dtype as image

The result of the morphological erosion.

Notes

For uint8 (and uint16 up to a certain bit-depth) data, the lower algorithm complexity makes the skimage2.filters.rank.minimum() function more efficient for larger images and footprints.

The footprint can also be provided as a sequence of 2-tuples where the first element of each 2-tuple is a footprint ndarray and the second element is an integer describing the number of times it should be iterated. For example, footprint=[(np.ones((9, 1)), 1), (np.ones((1, 9)), 1)] would apply a 9x1 footprint followed by a 1x9 footprint resulting in a net effect that is the same as footprint=np.ones((9, 9)), but with lower computational cost. Most of the built-in footprints such as skimage2.morphology.disk() provide an option to automatically generate a footprint sequence of this type. Refer to the example Decompose flat footprints (structuring elements) for more insights.

If footprint contains even-sized dimensions, they are padded with zeros to an odd size at the front (at index 0) with pad_footprint().

Examples

>>> # Erosion shrinks bright regions
>>> import numpy as np
>>> from skimage.morphology import footprint_rectangle
>>> bright_square = np.array([[0, 0, 0, 0, 0],
...                           [0, 1, 1, 1, 0],
...                           [0, 1, 1, 1, 0],
...                           [0, 1, 1, 1, 0],
...                           [0, 0, 0, 0, 0]], dtype=np.uint8)
>>> erosion(bright_square, footprint_rectangle((3, 3)))
array([[0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0],
       [0, 0, 1, 0, 0],
       [0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0]], dtype=uint8)

skimage2.morphology.mirror_footprint(footprint)[source]#

Mirror each dimension in the (decomposed) footprint.

Can mirror decomposed footprints which consist of a sequence of footprints and their repetitions.

Parameters:
footprintndarray or tuple

The input footprint or a sequence of footprints.

Returns:
invertedndarray or tuple

The footprint, mirrored along each dimension.

See also

pad_footprint

Pad the (decomposed) footprint to an odd size along each dimension.

Examples

>>> footprint = np.array([[0, 0, 0],
...                       [0, 1, 1],
...                       [0, 1, 1]], np.uint8)
>>> mirror_footprint(footprint)
array([[1, 1, 0],
       [1, 1, 0],
       [0, 0, 0]], dtype=uint8)

skimage2.morphology.opening(image, footprint=None, *, out=None, mode='ignore', cval=0.0)[source]#

Return grayscale morphological opening of an image.

The morphological opening of an image is defined as an erosion followed by a dilation. Opening can remove small bright spots (i.e., “salt”) and connect small dark cracks. This tends to “open” up (dark) gaps between (bright) features.

Parameters:
imagendarray

Input image.

footprintndarray or tuple, optional

The neighborhood expressed as a 2-D array of 1’s and 0’s. If None, use a cross-shaped footprint (so-called 1-connectivity). The footprint can also be provided as a sequence of smaller footprints as described in the notes below. See _Notes_ for more.

outndarray, optional

The array to store the result of the morphology. If None, a new array is allocated.

modestr, optional

The mode parameter determines how the array borders are handled. Valid modes are: ‘reflect’, ‘constant’, ‘nearest’, ‘mirror’, ‘wrap’, ‘max’, ‘min’, or ‘ignore’. If ‘ignore’, pixels outside the image domain are assumed to be the maximum for the image’s dtype in the erosion, and minimum in the dilation, which causes them to not influence the result. Default is ‘ignore’.

cvalscalar, optional

Value to fill past edges of input if mode is ‘constant’. Default is 0.0.

Returns:
outndarray, same shape and dtype as image

The result of the morphological opening.

Notes

The footprint can also be a provided as a sequence of 2-tuples where the first element of each 2-tuple is a footprint ndarray and the second element is an integer describing the number of times it should be iterated. For example footprint=[(np.ones((9, 1)), 1), (np.ones((1, 9)), 1)] would apply a 9x1 footprint followed by a 1x9 footprint resulting in a net effect that is the same as footprint=np.ones((9, 9)), but with lower computational cost. Most of the built-in footprints such as skimage2.morphology.disk() provide an option to automatically generate a footprint sequence of this type. Refer to the example Decompose flat footprints (structuring elements) for more insights.

If footprint contains even-sized dimensions, they are padded with zeros to an odd size at the front (at index 0) with pad_footprint().

Examples

>>> # Open up gap between two bright regions (but also shrink regions)
>>> import numpy as np
>>> from skimage.morphology import footprint_rectangle
>>> bad_connection = np.array([[1, 0, 0, 0, 1],
...                            [1, 1, 0, 1, 1],
...                            [1, 1, 1, 1, 1],
...                            [1, 1, 0, 1, 1],
...                            [1, 0, 0, 0, 1]], dtype=np.uint8)
>>> opening(bad_connection, footprint_rectangle((3, 3)))
array([[0, 0, 0, 0, 0],
       [1, 1, 0, 1, 1],
       [1, 1, 0, 1, 1],
       [1, 1, 0, 1, 1],
       [0, 0, 0, 0, 0]], dtype=uint8)

skimage2.morphology.pad_footprint(footprint, *, pad_end=True)[source]#

Pad the (decomposed) footprint to an odd size along each dimension.

Parameters:
footprintndarray or tuple

The input footprint or sequence of footprints

pad_endbool, optional

If True, pads at the end of each dimension (right side), otherwise pads on the front (left side).

Returns:
paddedndarray or tuple

The footprint, padded to an odd size along each dimension.

See also

mirror_footprint

Mirror each dimension in the (decomposed) footprint.

Examples

>>> footprint = np.array([[0, 0],
...                       [1, 1],
...                       [1, 1]], np.uint8)
>>> pad_footprint(footprint)
array([[0, 0, 0],
       [1, 1, 0],
       [1, 1, 0]], dtype=uint8)

skimage2.morphology.white_tophat(image, footprint=None, *, out=None, mode='ignore', cval=0.0)[source]#

Return white top hat of an image.

The white top hat of an image is defined as the image minus its morphological opening. This operation returns the bright spots of the image that are smaller than the footprint.

Parameters:
imagendarray

Input image.

footprintndarray or tuple, optional

The neighborhood expressed as a 2-D array of 1’s and 0’s. If None, use a cross-shaped footprint (so-called 1-connectivity). The footprint can also be provided as a sequence of smaller footprints as described in the notes below. See _Notes_ for more.

outndarray, optional

The array to store the result of the morphology. If None, a new array is allocated.

modestr, optional

The mode parameter determines how the array borders are handled. Valid modes are: ‘reflect’, ‘constant’, ‘nearest’, ‘mirror’, ‘wrap’, ‘max’, ‘min’, or ‘ignore’. See skimage2.morphology.opening(). Default is ‘ignore’.

cvalscalar, optional

Value to fill past edges of input if mode is ‘constant’. Default is 0.0.

Returns:
outndarray, same shape and dtype as image

The result of the morphological white top hat.

Notes

The footprint can also be a provided as a sequence of 2-tuples where the first element of each 2-tuple is a footprint ndarray and the second element is an integer describing the number of times it should be iterated. For example footprint=[(np.ones((9, 1)), 1), (np.ones((1, 9)), 1)] would apply a 9x1 footprint followed by a 1x9 footprint resulting in a net effect that is the same as footprint=np.ones((9, 9)), but with lower computational cost. Most of the built-in footprints such as skimage2.morphology.disk() provide an option to automatically generate a footprint sequence of this type. Refer to the example Decompose flat footprints (structuring elements) for more insights.

If footprint contains even-sized dimensions, they are padded with zeros to an odd size at the front (at index 0) with pad_footprint().

References

Examples

>>> # Subtract gray background from bright peak
>>> import numpy as np
>>> from skimage.morphology import footprint_rectangle
>>> bright_on_gray = np.array([[2, 3, 3, 3, 2],
...                            [3, 4, 5, 4, 3],
...                            [3, 5, 9, 5, 3],
...                            [3, 4, 5, 4, 3],
...                            [2, 3, 3, 3, 2]], dtype=np.uint8)
>>> white_tophat(bright_on_gray, footprint_rectangle((3, 3)))
array([[0, 0, 0, 0, 0],
       [0, 0, 1, 0, 0],
       [0, 1, 5, 1, 0],
       [0, 0, 1, 0, 0],
       [0, 0, 0, 0, 0]], dtype=uint8)