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.
- Prepare the display device and load a grayscale color table:
- Select the file, read the data and get the image dimensions:
- Using the dimensions of the image add a border for display purposes:
- Get the padded image size, create a window and display the original image:
- Using DIST, define a small square structuring element in order to retain the detail and angles of the image features:
- Apply MORPH_CLOSE to the image and display the resulting image:
- Determine a threshold value, using an intensity histogram as a guide:
- Threshold the original image and display the resulting binary image:
- Now display a binary version of the closed image:
file = FILEPATH('mineral.png', $
SUBDIRECTORY = ['examples', 'data'])
img = READ_PNG(file)
dims = SIZE(img, /DIMENSIONS)
dims = SIZE(padImg, /DIMENSIONS) WINDOW, 0, XSIZE=2*dims[0], YSIZE=2*dims[1], $ TITLE='Defining Shapes with the Closing Operator' TVSCL, padImg, 0
side = 3 strucElem = DIST(side) LE sideTip
EnterPRINT, strucElemto view the structure created by the previous statement.
closeImg = MORPH_CLOSE(padImg, strucElem, /GRAY) TVSCL, closeImg, 1The 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.
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.
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.

