Previous IDL Reference Guide: Procedures and Functions Next


Syntax | Return Value | Arguments | Keywords | Examples | Version History | See Also

The MORPH_CLOSE function applies the closing operator to a binary or grayscale image. MORPH_CLOSE is simply a dilation operation followed by an erosion operation.The closing operation is an idempotent operator—applying it more than once produces no further effect.

Both the opening and the closing operators have the effect of smoothing the image, with the opening operation removing pixels, and the closing operation adding pixels.

This routine is written in the IDL language. Its source code can be found in the file in the lib subdirectory of the IDL distribution.


Result = MORPH_CLOSE (Image, Structure [, /GRAY] [, PRESERVE_TYPE=bytearray | /UINT | /ULONG] [, VALUES=array] )

Return Value

The result of a closing operation is that small holes and gaps within the image (smaller than the size of Structure) are filled, yet the original sizes of the primary foreground features are maintained.



A one-, two-, or three-dimensional array upon which the closing operation is to be performed. If neither of the keywords GRAY or VALUES is present, the image is treated as a binary image with all nonzero pixels considered as 1.


A one-, two-, or three-dimensional array to be used as the structuring element. The elements are interpreted as binary values - either zero or nonzero. The structuring element must have the same number of dimensions as the Image argument.



Set this keyword to perform a grayscale, rather than binary, operation. Nonzero elements of the Structure parameter determine the shape of the structuring element. If the VALUES keyword is not present, all elements of the structuring element are 0.


Set this keyword to return the same type as the input array. The input array must be of type BYTE, UINT, or ULONG. This keyword only applies for grayscale erosion/dilation, and is mutually exclusive of UINT and ULONG.


Set this keyword to return an unsigned integer array. This keyword only applies for grayscale operations, and is mutually exclusive of the ULONG and PRESERVE_TYPE keywords.


Set this keyword to return an unsigned longword integer array. This keyword only applies for grayscale operations, and is mutually exclusive of the UINT and PRESERVE_TYPE keywords.


An array of the same dimensions as Structure providing the values of the structuring element. The presence of this keyword implies a grayscale operation.


Also see Smoothing with MORPH_CLOSE.

The following code reads a data file in the IDL Demo data directory containing a magnified image of grains of pollen. It then applies a threshold and a morphological closing operator with a 3 by 3 square kernel to the original image. Notice that most of the holes in the pollen grains have been filled by the closing operator.

;Handle TrueColor displays:  
;Read the image  
READ_JPEG, path, img  
;Create window:  
WINDOW, 0, XSIZE=700, YSIZE=540  
;Show original image  
XYOUTS, 180, 525, 'Original Image', ALIGNMENT=.5, /DEVICE  
TVSCL, img, 20, 280  
;Apply the threshold creating a binary image  
thresh = img GE 140B  
;Load a simple color table  
;Display Edges  
XYOUTS, 520, 525, 'Edges', ALIGNMENT=.5, /DEVICE  
TV, thresh, 360, 280  
;Apply closing operator  
closing = MORPH_CLOSE(thresh, REPLICATE(1,3,3))  
;Show the result  
XYOUTS, 180, 265, 'Closing Operator', ALIGNMENT=.5, /DEVICE  
TV, closing, 20, 20  
;Show added pixels in white  
XYOUTS, 520, 265, 'Added Pixels in White', ALIGNMENT=.5, /DEVICE  
TV, closing + thresh, 360, 20  

Version History


See Also


  IDL Online Help (March 06, 2007)