Smoothing with MORPH_OPEN

The MORPH_OPEN function applies the opening operation, which is erosion followed by dilation, to a binary or grayscale image. The opening operation removes noise from an image while maintaining the overall sizes of objects in the foreground. Opening is a useful process for smoothing contours, removing pixel noise, eliminating narrow extensions, and breaking thin links between features. After using an opening operation to darken small objects and remove noise, thresholding or other morphological processes can be applied to the image to further refine the display of the primary shapes within the image.

The following example applies the opening operation to an image of microscopic spherical organisms, Rhinosporidium seeberi protozoans. After applying the opening operation and thresholding the image, only the largest elements of the image are retained, the mature R.seeberi organisms. Complete the following steps for a detailed description of the process.

Example Code
See morphopenexample.pro in the examples/doc/image subdirectory of the IDL installation directory for code that duplicates this example. Run the example procedure by entering morphopenexample at the IDL command prompt or view the file in an IDL Editor window by entering .EDIT morphopenexample.pro.

  1. Prepare the display device and load grayscale color table:
  2. DEVICE, DECOMPOSED = 0, RETAIN = 2 
    LOADCT, 0 
    
  3. Select and open the image file:
  4. file = FILEPATH('r_seeberi.jpg', $ 
        SUBDIRECTORY = ['examples', 'data']) 
    READ_JPEG, file, image, /GRAYSCALE 
    
  5. Get the image dimensions, prepare a window and display the image:
  6. dims = SIZE(image, /DIMENSIONS) 
    WINDOW, 0, XSIZE = 2*dims[0], YSIZE = 2*dims[1], $ 
        TITLE = 'Defining Shapes with Opening Operation' 
    TVSCL, image, 0 
    
  7. Define the radius of the structuring element and create a disk-shaped element to extract circular features:
  8. radius = 7 
    strucElem = SHIFT(DIST(2*radius+1), radius, radius) LE radius 
     

    Compared to the previous example, a larger element is used in order to retain only the larger image elements, discarding all of the smaller background features. Further increases in the size of the structuring element would extract even larger image features.

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

  9. Apply the MORPH_OPEN function to the image, specifying the GRAY keyword for the grayscale image:
  10. morphImg = MORPH_OPEN(image, strucElem, /GRAY) 
    
  11. Display the image:
  12. TVSCL, morphImg, 1 
     

    The following figure shows the original image (left) and the application of the opening operation to the original image (right). The opening operation has enhanced and maintained the sizes of the large bright objects within the image while blending the smaller background features.

    Figure 9-6: Application of the Opening Operation to a Grayscale Image

    imgmorph06.gif

    The following steps apply the opening operator to a binary image.

  13. Create a window and use HISTOGRAM in conjunction with PLOT, displaying an intensity histogram to help determine the threshold intensity value:
  14. WINDOW, 1, XSIZE = 400, YSIZE = 300 
    PLOT, HISTOGRAM(img) 
     

    Note
    Using an intensity histogram as a guide for determining threshold values is described in the section, Determining Intensity Values for Threshold and Stretch.

  15. Using the histogram as a guide, create a binary image. To prepare to remove background noise, retain only areas of the image where pixel values are equal to or greater than 160:
  16. threshImg = image GE 160 
    WSET, 0 
    TVSCL, threshImg, 2 
    
  17. Apply the opening operation to the binary image to remove noise and smooth contours, and then display the image:
  18. morphThresh = MORPH_OPEN(threshImg, strucElem) 
    TVSCL, morphThresh, 3 
    

The combination of thresholding and applying the opening operation has successfully extracted the primary foreground features as shown in the following figure.

Figure 9-7: Binary Image (left) and Application of the Opening Operator to the Binary Image (right)

imgmorph07.gif