Eroding and Dilating Image Objects
The basic morphological operations, erosion and dilation, produce contrasting results when applied to either grayscale or binary images. Erosion shrinks image objects while dilation expands them. The specific actions of each operation are covered in the following sections.
Characteristics of Erosion
- Erosion generally decreases the sizes of objects and removes small anomalies by subtracting objects with a radius smaller than the structuring element.
- With grayscale images, erosion reduces the brightness (and therefore the size) of bright objects on a dark background by taking the neighborhood minimum when passing the structuring element over the image.
- With binary images, erosion completely removes objects smaller than the structuring element and removes perimeter pixels from larger image objects.
Characteristics of Dilation
- Dilation generally increases the sizes of objects, filling in holes and broken areas, and connecting areas that are separated by spaces smaller than the size of the structuring element.
- With grayscale images, dilation increases the brightness of objects by taking the neighborhood maximum when passing the structuring element over the image.
- With binary images, dilation connects areas that are separated by spaces smaller than the structuring element and adds pixels to the perimeter of each image object.
Applying Erosion and Dilation
The following example applies erosion and dilation to grayscale and binary images. When using erosion or dilation, avoid the generation of indeterminate values for objects occurring along the edges of the image by padding the image, as shown in the following example. Complete the following steps for a detailed description of the process.
Example Code
See morpherodedilate.pro in the examples/doc/image subdirectory of the IDL installation directory for code that duplicates this example. Run the example procedure by entering morpherodedilate at the IDL command prompt or view the file in an IDL Editor window by entering .EDIT morpherodedilate.pro.
Note
This example uses a file from the examples/demo/demodata directory of your installation. If you have not already done so, you will need to install "IDL Demos" from your product CD-ROM to install the demo data file needed for this example.
- Prepare the display device:
- Load a grayscale color table:
- Select and read in the image file. Use the GRAYSCALE keyword to READ_JPEG to open the grayscale image:
- Get the size of the image:
- Define the structuring element. A radius of 2 results in a structuring element near the size of the specks of background noise. This radius also affects only the edges of the larger objects (whereas a larger radius would cause significant distortion of all image features):
- Create a disk-shaped structuring element that corresponds to the shapes occurring within the image:
- Add a border to the image to avoid generating indeterminate values when passing the structuring element over objects along the edges of an image. If the starting origin of the structuring element is not specified in the call to ERODE, the origin defaults to one half the width of the structuring element. Therefore, creating a border equal to one half of the structuring element width (equal to the radius) is sufficient to avoid indeterminate values. Create padded images for both the erode operation (using the maximum array value for the border), and the dilate operation (using the minimum array value for the border) as follows:
- Get the size of either of the padded images, create a window and display the original image:
- Apply the ERODE function to the grayscale image using the GRAY keyword and display the image:
- For comparison, apply DILATE to the same image and display it:
- Create a window and use HISTOGRAM in conjunction with PLOT, displaying an intensity histogram to help determine the threshold intensity value:
- To compare the effects of erosion and dilation on binary images, create a binary image, retaining pixels with values greater than or equal to 120:
- Create padded binary images for the erode and dilation operations, using 1 as the maximum array value for the erosion image and 0 as the minimum value for the dilation image:
- Get the dimensions of either image, create a second window and display the binary image:
- Using the structuring element defined previously, apply the erosion and dilation operations to the binary images and display the results by entering the following lines:
file = FILEPATH('pollens.jpg', $
SUBDIRECTORY = ['examples', 'demo', 'demodata'])
READ_JPEG, file, img, /GRAYSCALE
strucElem = SHIFT(DIST(2*radius+1), radius, radius) LE radiusTip
EnterPRINT, strucElemto view the structure created by the previous statement.
erodeImg = REPLICATE(MAX(img), dims[0]+2, dims[1]+2) erodeImg [1,1] = img dilateImg = REPLICATE(MIN(img), dims[0]+2, dims[1]+2) dilateImg [1,1] = imgNote
Padding is only necessary when accurate edge values are important. Adding a pad equal to more that one half the width of the structuring element does not negatively effect the morphological operation, but does minutely add to the processing time. The padding can be removed from the image after applying the morphological operation and before displaying the image if desired.
padDims = SIZE(erodeImg, /DIMENSIONS) WINDOW, 0, XSIZE = 3*padDims[0], YSIZE = padDims[1], $ TITLE = "Original, Eroded and Dilated Grayscale Images" TVSCL, img, 0
dilateImg = DILATE(dilateImg, strucElem, /GRAY) TVSCL, dilateImg, 2The following image displays the effects of erosion (middle) and dilation (right). Erosion removes pixels from perimeters of objects, decreases the overall brightness of the grayscale image and removes objects smaller than the structuring element. Dilation adds pixels to perimeters of objects, brightens the image, and fills in holes smaller than the structuring element as shown in the following figure.
WINDOW, 1, XSIZE = 400, YSIZE = 300 PLOT, HISTOGRAM(img)Note
Using an intensity histogram as a guide for determining threshold values is described in the section, Determining Intensity Values for Threshold and Stretch.
erodeImg = REPLICATE(1B, dims[0]+2, dims[1]+2) erodeImg [1,1] = img dilateImg = REPLICATE(0B, dims[0]+2, dims[1]+2) dilateImg [1,1] = img
dims = SIZE(erodeImg, /DIMENSIONS) WINDOW, 2, XSIZE = 3*dims[0], YSIZE = dims[1], $ TITLE = "Original, Eroded and Dilated Binary Images" TVSCL, img, 0
The results are shown in the following figure.

