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

Characteristics of Dilation

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.

  1. Prepare the display device:
  2. DEVICE, DECOMPOSED = 0, RETAIN = 2 
    
  3. Load a grayscale color table:
  4. LOADCT, 0 
    
  5. Select and read in the image file. Use the GRAYSCALE keyword to READ_JPEG to open the grayscale image:
  6. file = FILEPATH('pollens.jpg', $ 
       SUBDIRECTORY = ['examples', 'demo', 'demodata']) 
    READ_JPEG, file, img, /GRAYSCALE 
    
  7. Get the size of the image:
  8. dims = SIZE(img, /DIMENSION) 
    
  9. 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):
  10. radius = 2 
    
  11. Create a disk-shaped structuring element that corresponds to the shapes occurring within the image:
  12. strucElem = SHIFT(DIST(2*radius+1), radius, radius) LE radius 
     

    Tip
    Enter PRINT, strucElem to view the structure created by the previous statement.

  13. 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:
  14. 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] = img 
     

    Note
    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.

  15. Get the size of either of the padded images, create a window and display the original image:
  16. padDims = SIZE(erodeImg, /DIMENSIONS) 
    WINDOW, 0, XSIZE = 3*padDims[0], YSIZE = padDims[1], $ 
       TITLE = "Original, Eroded and Dilated Grayscale Images" 
    TVSCL, img, 0 
    
  17. Apply the ERODE function to the grayscale image using the GRAY keyword and display the image:
  18. erodeImg = ERODE(erodeImg, strucElem, /GRAY) 
    TVSCL, erodeImg, 1 
    
  19. For comparison, apply DILATE to the same image and display it:
  20. dilateImg = DILATE(dilateImg, strucElem, /GRAY) 
    TVSCL, dilateImg, 2 
     

    The 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.

    Figure 9-4: Original (left), Eroded (center) and Dilated (right) Grayscale Images

    imgmorph04.jpg

  21. Create a window and use HISTOGRAM in conjunction with PLOT, displaying an intensity histogram to help determine the threshold intensity value:
  22. 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.

  23. 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:
  24. img = img GE 120 
    
  25. 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:
  26. 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 
    
  27. Get the dimensions of either image, create a second window and display the binary image:
  28. dims = SIZE(erodeImg, /DIMENSIONS) 
    WINDOW, 2, XSIZE = 3*dims[0], YSIZE = dims[1], $ 
       TITLE = "Original, Eroded and Dilated Binary Images" 
    TVSCL, img, 0 
    
  29. 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:
  30. erodeImg = ERODE(erodeImg, strucElem) 
    TVSCL, erodeImg, 1 
    dilateImg = DILATE(dilateImg, strucElem) 
    TVSCL, dilateImg, 2 
    

The results are shown in the following figure.

Figure 9-5: Original, Eroded and Dilated Binary Images

imgmorph05.jpg