Selecting Specific Image Objects

The hit-or-miss morphological operation is used primarily for identifying specific shapes within binary images. The MORPH_HITORMISS function uses two structuring elements; a "hit" structure and a "miss" structure. The operation first applies an erosion operation with the hit structure to the original image. The operation then applies an erosion operator with the miss structure to an inverse of the original image. The matching image elements entirely contain the hit structure and are entirely and solely contained by the miss structure.

Note
An image must be padded with a border equal to one half the size of the structuring element if you want the hit-or-miss operation to be applied to image elements occurring along the edges of the image.

The hit-or-miss operation is very sensitive to the shape, size and rotation of the two structuring elements. Hit and miss structuring elements must be specifically designed to extract the desired geometric shapes from each individual image. When dealing with complicated images, extracting specific image regions may require multiple applications of hit and miss structures, using a range of sizes or several rotations of the structuring elements.

The following example uses the image of the Rhinosporidium seeberi parasitic protozoans, containing simple circular shapes. After specifying distinct hit and miss structures, the elements of the image that meet the hit and miss conditions are identified and overlaid on the original image. Complete the following steps for a detailed description of the process.

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

  1. Prepare the display device and load a 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, img, /GRAYSCALE 
    
  5. Pad the image so that objects at the edges of the image are not discounted:
  6. dims = SIZE(img, /DIMENSIONS) 
    padImg = REPLICATE(0B, dims[0]+10, dims[1]+10) 
    padImg [5,5] = img 
     

    Failing to pad an image causes all objects occurring at the edges of the image to fail the hit and miss conditions.

  7. Get the image dimensions, create a window and display the padded image:
  8. dims = SIZE(padImg, /DIMENSIONS) 
    WINDOW, 0, XSIZE = 3*dims[0], YSIZE = 2*dims[1], $ 
        TITLE='Displaying Hit-or-Miss Matches' 
    TVSCL, padImg, 0 
    
  9. Define the radius of the structuring element and create a large, disk-shaped element to extract the large, circular image objects:
  10. radstr = 7 
    strucElem = SHIFT(DIST(2*radstr+1), radstr, radstr) LE radstr 
     

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

  11. Apply MORPH_OPEN for a smoothing effect and display the image:
  12. openImg = MORPH_OPEN(padImg, strucElem, /GRAY) 
    TVSCL, openImg, 1 
    
  13. Since the hit-or-miss operation requires a binary image, display an intensity histogram as a guide for determining a threshold value:
  14. WINDOW, 2, XSIZE = 400, YSIZE = 300 
    PLOT, HISTOGRAM(openImg) 
     

    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. Create a binary image by retaining only those image elements with pixel values greater than or equal to 150 (the bright foreground objects):
  16. threshImg = openImg GE 150 
    WSET, 0 
    TVSCL, threshImg, 2 
     

    The results of opening (left) and thresholding (right) are shown in the following figure.

    Figure 9-15: Results of Opening (left) and Thresholding (right)

    imgmorph15.jpg

  17. Create the structuring elements for the hit-or-miss operation:
  18. radhit = 7 
    radmiss = 23 
    hit = SHIFT(DIST(2*radhit+1), radhit, radhit) LE radhit 
    miss = SHIFT(DIST(2*radmiss+1), radmiss, radmiss) GE radmiss 
     

    While the shapes of the structuring elements are purposefully circular, the sizes were chosen after empirically testing, seeking elements suitable for this example.

    Tip
    Enter PRINT, hit or PRINT, miss to view the structures.

    The following figures shows the hit and miss structuring elements and the binary image. Knowing that the region must enclose the hit structure and be surrounded by a background area at least as large as the miss structure, can you predict which regions will be "matches?"

    Figure 9-16: Applying the Hit and Miss Structuring Elements to a Binary Image

    imgmorph16.jpg

  19. Apply the MORPH_HITORMISS function to the binary image. Image regions matching the hit and miss conditions are designated at matches:
  20. matches = MORPH_HITORMISS(threshImg, hit, miss) 
    
  21. Display the elements matching the hit and miss conditions, dilating the elements to the radius of a hit:
  22. dmatches = DILATE(matches, hit) 
    TVSCL, dmatches, 3 
    
  23. Display the original image overlaid with the matching elements:
  24. padImg [WHERE (dmatches EQ 1)] = 1 
    TVSCL, padImg, 4 
    

The following figure shows the elements of the image which matched the hit and miss conditions, having a radius of at least 7 (the hit structure), yet fitting entirely inside a structure with a radius of 23 (the miss structure).

Figure 9-17: Image Elements Matching Hit and Miss Conditions

imgmorph17.jpg

Initially, it may appear that more regions should have been "matches" since they met the hit condition of having a radius of 7 or more. However, as the following figure shows, many such regions failed the miss condition since neighboring regions impinged upon the miss structure. Such a region appears on the left in the following figure.

Figure 9-18: Example of Hit and Miss Relationship

imgmorph18.jpg

Considering the simplicity of the previous image, it is understandable that selecting hit and miss structures for more complex images can require significant empirical testing. It is to your advantage to keep in mind how sensitive the hit-or-miss operation is to the shapes, sizes and rotations of the hit and miss structures.