Smoothing with MORPH_CLOSE

The morphological closing operation performs dilation followed by erosion, the opposite of the opening operation. The MORPH_CLOSE function smooths contours, links neighboring features, and fills small gaps or holes. The operation effectively brightens small objects in binary and grayscale images. Like the opening operation, primary objects retain their original shape.

The following example uses the closing operation and a square structuring element to extract the shapes of mineral crystals. Complete the following steps for a detailed description of the process.

Example Code
See morphcloseexample.pro in the examples/doc/image subdirectory of the IDL installation directory for code that duplicates this example. Run the example procedure by entering morphcloseexample at the IDL command prompt or view the file in an IDL Editor window by entering .EDIT morphcloseexample.pro.

  1. Prepare the display device and load a grayscale color table:
  2. DEVICE, DECOMPOSED = 0, RETAIN = 2 
    LOADCT, 0 
    
  3. Select the file, read the data and get the image dimensions:
  4. file = FILEPATH('mineral.png', $ 
       SUBDIRECTORY = ['examples', 'data']) 
    img = READ_PNG(file) 
    dims = SIZE(img, /DIMENSIONS) 
    
  5. Using the dimensions of the image add a border for display purposes:
  6. padImg = REPLICATE(0B, dims[0]+10, dims[1]+10) 
    padImg [5,5] = img 
    
  7. Get the padded image size, create a window and display the original image:
  8. dims = SIZE(padImg, /DIMENSIONS) 
    WINDOW, 0, XSIZE=2*dims[0], YSIZE=2*dims[1], $ 
       TITLE='Defining Shapes with the Closing Operator' 
    TVSCL, padImg, 0 
    
  9. Using DIST, define a small square structuring element in order to retain the detail and angles of the image features:
  10. side = 3 
    strucElem = DIST(side) LE side 
    

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

  11. Apply MORPH_CLOSE to the image and display the resulting image:
  12. closeImg = MORPH_CLOSE(padImg, strucElem, /GRAY) 
    TVSCL, closeImg, 1 
     

    The following figure shows the original image (left) and the results of applying the closing operator (right). Notice that the closing operation has removed much of the small, dark noise from the background of the image, while maintaining the characteristics of the foreground features.

    Figure 9-8: Original (left) and Closed Image (right)

    imgmorph08.gif

  13. Determine a threshold value, using an intensity histogram as a guide:
  14. WINDOW, 2, XSIZE = 400, YSIZE = 300 
    PLOT, HISTOGRAM(closeImg) 
     

    Note
    Using an intensity histogram as a guide for determining threshold values is described in the section, Determining Intensity Values for Threshold and Stretch.

  15. Threshold the original image and display the resulting binary image:
  16. binaryImg = padImg LE 160 
    WSET, 0 
    TVSCL, binaryImg, 2 
    
  17. Now display a binary version of the closed image:
  18. binaryClose = closeImg LE 160 
    TVSCL, binaryClose, 3 
    

The results of thresholding the original and closed image using the same intensity value clearly display the actions of the closing operator. The dark background noise has been removed, much as if a dilation operation had been applied, yet the sizes of the foreground features have been maintained.

Figure 9-9: Threshold of Original Image (left) and Closed Image (right)

imgmorph09.gif