Detecting Edges of Image Objects
The MORPH_GRADIENT function applies the gradient operation to a grayscale image. This operation highlights object edges by subtracting an eroded version of the original image from a dilated version. Repeatedly applying the gradient operator or increasing the size of the structuring element results in wider edges.
The following example extracts image features by applying the morphological gradient operation to an image of the Mars globe. Complete the following steps for a detailed description of the process.
Example Code
See morphgradientex.pro in the examples/doc/image subdirectory of the IDL installation directory for code that duplicates this example. Run the example procedure by entering morphgradientex at the IDL command prompt or view the file in an IDL Editor window by entering .EDIT morphgradientex.pro.
- Prepare the display device and load the grayscale color table:
- Select and read in the file:
- Get the image size, create a window and display the smoothed image:
- Preserve the greatest amount of detail within the image by defining a structuring element with a radius of 1, avoiding excessively thick edge lines:
- Apply the MORPH_GRADIENT function to the image and display the result:
- To more easily distinguish features within the dark image, prepare to stretch the image by displaying an intensity histogram:
- Stretch the image and display its inverse:
file = FILEPATH('marsglobe.jpg', $
SUBDIRECTORY=['examples', 'data'])
READ_JPEG, file, image, /GRAYSCALE
dims = SIZE(image, /DIMENSIONS) WINDOW, 0, XSIZE =2*dims[0], YSIZE = 2*dims[1], $ TITLE = 'Original and MORPH_GRADIENT Images'The original image is shown in the following figure.
radius = 1 strucElem = SHIFT(DIST(2*radius+1), radius, radius) LE radiusTip
EnterPRINT, strucElemto view the structure created by the previous statement.
WINDOW, 2, XSIZE = 400, YSIZE = 300 PLOT, HISTOGRAM(1-image)The previous line returns a histogram of an inverse of the original image since the final display will also be an inverse display for showing the greatest detail.

