Main Content

Dilate an Image to Enlarge a Shape

This example shows how to dilate an image using the imdilate function. The morphological dilation operation expands or thickens foreground objects in an image.

Create a simple sample binary image containing one foreground object: the square region of 1's in the middle of the image.

BW = zeros(9,10);
BW(4:6,4:7) = 1
BW = 9×10

     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
     0     0     0     1     1     1     1     0     0     0
     0     0     0     1     1     1     1     0     0     0
     0     0     0     1     1     1     1     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     0     0     0

imshow(imresize(BW,40,'nearest'))

Create a structuring element to use with imdilate. To dilate a geometric object, you typically create a structuring element that is the same shape as the object.

SE = strel('square',3)
SE = 
strel is a square shaped structuring element with properties:

      Neighborhood: [3x3 logical]
    Dimensionality: 2

Dilate the image, passing the input image and the structuring element to imdilate. Note how dilation adds a rank of 1's to all sides of the foreground object.

BW2 = imdilate(BW,SE)
BW2 = 9×10

     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0
     0     0     1     1     1     1     1     1     0     0
     0     0     1     1     1     1     1     1     0     0
     0     0     1     1     1     1     1     1     0     0
     0     0     1     1     1     1     1     1     0     0
     0     0     1     1     1     1     1     1     0     0
     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0

imshow(imresize(BW2,40,'nearest'))

For comparison, create a structuring element that is a different shape. Dilate the original image using the new structuring element.

SE2 = strel('diamond',1);
BW3 = imdilate(BW,SE2);
imshow(imresize(BW3,40,'nearest'))