Erode image


IM2 = imerode(IM,SE)
IM2 = imerode(IM,NHOOD)
IM2 = imerode(___,PACKOPT,M)
IM2 = imerode(___,SHAPE)
gpuarrayIM2 = imerode(gpuarrayIM,___)


IM2 = imerode(IM,SE) erodes the grayscale, binary, or packed binary image IM, returning the eroded image IM2. The argument SE is a structuring element object or array of structuring element objects returned by the strel function.

If IM is logical and the structuring element is flat, imerode performs binary erosion; otherwise it performs grayscale erosion. If SE is an array of structuring element objects, imerode performs multiple erosions of the input image, using each structuring element in SE in succession.

This function supports code generation (see Tips).

IM2 = imerode(IM,NHOOD) erodes the image IM, where NHOOD is an array of 0's and 1's that specifies the structuring element neighborhood. This is equivalent to the syntax imerode(IM,strel(NHOOD)). The imerode function determines the center element of the neighborhood by floor((size(NHOOD)+1)/2).

IM2 = imerode(___,PACKOPT,M) specifies whether IM is a packed binary image and, if it is, provides the row dimension M of the original unpacked image. PACKOPT can have either of the following values. Default value is enclosed in braces ({}).




IM is treated as a packed binary image as produced by bwpack. IM must be a 2-D uint32 array and SE must be a flat 2-D structuring element.


IM is treated as a normal array.

If PACKOPT is 'ispacked', you must specify a value for M.

IM2 = imerode(___,SHAPE) specifies the size of the output image. SHAPE can have either of the following values. Default value is enclosed in braces ({}).



Make the output image the same size as the input image. If the value of PACKOPT is 'ispacked', SHAPE must be 'same'.


Compute the full erosion.

gpuarrayIM2 = imerode(gpuarrayIM,___) performs the operation on a graphics processing unit (GPU). gpuarrayIM is a gpuArray that contains a grayscale or binary image. gpuarrayIM2 is a gpuArray of the same class as the input image. Note that the PACKOPT syntax is not supported on a GPU. This syntax requires the Parallel Computing Toolbox™.

Class Support

IM can be numeric or logical and it can be of any dimension. If IM is logical and the structuring element is flat, the output image is logical; otherwise the output image has the same class as the input. If the input is packed binary, then the output is also packed binary.

gpuarrayIM must be a gpuArray of type uint8 or logical. When used with a gpuarray, the structuring element must be flat and two-dimensional. The output has the same class as the input.


Erode a binary image with a disk structuring element.

originalBW = imread('circles.png');  
se = strel('disk',11);        
erodedBW = imerode(originalBW,se);
imshow(originalBW), figure, imshow(erodedBW)

Erode a grayscale image with a rolling ball.

originalI = imread('cameraman.tif');
se = strel('ball',5,5);
erodedI = imerode(originalI,se);
figure, imshow(originalI), figure, imshow(erodedI)

Erode the binary image in text.png with a vertical line.

originalBW = imread('text.png');
se = strel('line',11,90);
erodedBW = imerode(originalBW,se);
figure, imshow(originalBW)
figure, imshow(erodedBW)

Erode the binary image on a GPU.

originalBW = imread('text.png');
se = strel('line',11,90);
erodedBW = imerode(gpuArray(originalBW),se);
figure, imshow(originalBW), figure, imshow(erodedBW)

Erode the grayscale image on a GPU.

originalI = imread('cameraman.tif');
se = strel('disk',5);
erodedI = imerode(gpuArray(originalI),se);
figure, imshow(originalI), figure, imshow(erodedI)


The binary erosion of A by B, denoted A B, is defined as the set operation A B = {z|(BzA}. In other words, it is the set of pixel locations z, where the structuring element translated to location z overlaps only with foreground pixels in A.

In the general form of gray-scale erosion, the structuring element has a height. The gray-scale erosion of A(x, y) by B(x, y) is defined as:

(A B)(x, y) = min {A(x + x′, y + y′) − B(x′, y′) | (x′, y′) ∊ DB},

where DB is the domain of the structuring element B and A(x,y) is assumed to be +∞ outside the domain of the image. To create a structuring element with nonzero height values, use the syntax strel(nhood,height), where height gives the height values and nhood corresponds to the structuring element domain, DB.

Most commonly, gray-scale erosion is performed with a flat structuring element (B(x,y) = 0). Gray-scale erosion using such a structuring element is equivalent to a local-minimum operator:

(A B)(x, y) = min {A(x + x′, y + y′) | (x′, y′) ∊ DB}.

All of the strel syntaxes except for strel(nhood,height), strel('arbitrary',nhood,height), and strel('ball', ...) produce flat structuring elements.

For more information on binary erosion, see [1].

More About

collapse all


  • This function supports the generation of C code using MATLAB® Coder™. Note that if you choose the generic MATLAB Host Computer target platform, the function generates code that uses a precompiled, platform-specific shared library. Use of a shared library preserves performance optimizations but limits the target platforms for which code can be generated. For more information, see Understanding Code Generation with Image Processing Toolbox.

    When generating code, the input image, IM, must be 2-D or 3-D. The structuring element argument SE must be a single element—arrays of structuring elements are not supported. To obtain the same result as that obtained using an array of structuring elements, call the function sequentially. When the target is MATLAB Host Computer, the PACKOPT and SHAPE arguments must be compile-time constants. When the target is any other platform, the PACKOPT syntax is not supported.


imerode automatically takes advantage of the decomposition of a structuring element object (if a decomposition exists). Also, when performing binary dilation with a structuring element object that has a decomposition, imerode automatically uses binary image packing to speed up the dilation.

Erosion using bit packing is described in [3].


[1] Gonzalez, R. C., R. E. Woods, and S. L. Eddins, Digital Image Processing Using MATLAB, Gatesmark Publishing, 2009.

[2] Haralick, Robert M., and Linda G. Shapiro, Computer and Robot Vision, Vol. I, Addison-Wesley, 1992, pp. 158-205.

[3] van den Boomgard, R, and R. van Balen, "Methods for Fast Morphological Image Transforms Using Bitmapped Images," Computer Vision, Graphics, and Image Processing: Graphical Models and Image Processing, Vol. 54, Number 3, pp. 254-258, May 1992.

Was this topic helpful?