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.

  1. Prepare the display device and load the grayscale color table:
  2. DEVICE, DECOMPOSED = 0, RETAIN = 2 
    LOADCT, 0 
    
  3. Select and read in the file:
  4. file = FILEPATH('marsglobe.jpg', $ 
       SUBDIRECTORY=['examples', 'data']) 
    READ_JPEG, file, image, /GRAYSCALE 
    
  5. Get the image size, create a window and display the smoothed image:
  6. 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.

    Figure 9-19: Image of Mars Globe

    imgmorph19.gif

  7. Preserve the greatest amount of detail within the image by defining a structuring element with a radius of 1, avoiding excessively thick edge lines:
  8. radius = 1 
    strucElem = SHIFT(DIST(2*radius+1), radius, radius) LE radius 
     

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

  9. Apply the MORPH_GRADIENT function to the image and display the result:
  10. morphImg = MORPH_GRADIENT(image, strucElem) 
    TVSCL, morphImg, 2 
    
  11. To more easily distinguish features within the dark image, prepare to stretch the image by displaying an intensity histogram:
  12. 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.

  13. Stretch the image and display its inverse:
  14. WSET, 0 
    TVSCL, 1-(morphImg < 87 ), 3 
     

    The following figure displays the initial and stretched gradient images.

    Figure 9-20: Initial and Stretched Results of the Gradient Operation

    imgmorph20.jpg