Image.erode#

Image.erode(se: ndarray | list, n: int = 1, border: str = 'replicate', bordervalue: float = 0, **kwargs: Any) Self[source]#

Morphological erosion

Parameters:
  • se (ndarray(N,M)) – structuring element

  • n (int, optional) – number of times to apply the erosion, defaults to 1

  • border (str, optional) – option for boundary handling, see convolve, defaults to 'replicate'

  • bordervalue (scalar, optional) – padding value, defaults to 0

  • kwargs – addition options passed to opencv.erode

Returns:

eroded image

Return type:

Image

Returns the image after morphological erosion with the structuring element se applied n times.

The image can be of any type and boolean images where True is treated as 1 and False as 0. The structuring element should be a 2D array of non-negative integers, where non-zero values indicate the shape of the structuring element.

Example:

>>> from machinevisiontoolbox import Image
>>> import numpy as np
>>> img = Image.Squares(1, size=7)
>>> img.print()
   0 0 0 0 0 0 0
   0 1 1 1 1 1 0
   0 1 1 1 1 1 0
   0 1 1 1 1 1 0
   0 1 1 1 1 1 0
   0 1 1 1 1 1 0
   0 0 0 0 0 0 0
>>> img.erode(np.ones((3,3))).print()
   0 0 0 0 0 0 0
   0 0 0 0 0 0 0
   0 0 1 1 1 0 0
   0 0 1 1 1 0 0
   0 0 1 1 1 0 0
   0 0 0 0 0 0 0
   0 0 0 0 0 0 0

Note

  • It is cheaper to apply a smaller structuring element multiple times than one large one, the effective structuring element is the Minkowski sum of the structuring element with itself N times.

  • The structuring element typically has odd side lengths.

  • For a greyscale image erosion is the maximum value over the structuring element.

Warning

treats NaNs in input image as standard IEEE-754 floating-point values, meaning any morphological operation involving a NaN results in NaN. A single NaN within the structuring element’s window propagates to make the output pixel NaN. Use fixbad for handling NaN values.

References:

Important

Uses OpenCV function cv2.erode which accepts multiple-channel, CV_8U, CV_16U, CV_16S, CV_32F or CV_64F images.

Seealso:

dilate open close morph opencv.erode