|
Image Processing : Extracting and Analyzing Shapes |
|
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. |
DEVICE, DECOMPOSED = 0, RETAIN = 2 LOADCT, 0
file = FILEPATH('mineral.png', $
SUBDIRECTORY = ['examples', 'data'])
img = READ_PNG(file)
dims = SIZE(img, /DIMENSIONS)
padImg = REPLICATE(0B, dims[0]+10, dims[1]+10) padImg [5,5] = img
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 side
| Tip Enter PRINT, strucElem to view the structure created by the previous statement. |
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.
![]() |
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. |
binaryImg = padImg LE 160 WSET, 0 TVSCL, binaryImg, 2
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.
![]() |
IDL Online Help (March 06, 2007)