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.
- Prepare the display device and load a grayscale color table:
- Select and open the image file:
- Pad the image so that objects at the edges of the image are not discounted:
- Get the image dimensions, create a window and display the padded image:
- Define the radius of the structuring element and create a large, disk-shaped element to extract the large, circular image objects:
- Apply MORPH_OPEN for a smoothing effect and display the image:
- Since the hit-or-miss operation requires a binary image, display an intensity histogram as a guide for determining a threshold value:
- Create a binary image by retaining only those image elements with pixel values greater than or equal to 150 (the bright foreground objects):
- Create the structuring elements for the hit-or-miss operation:
- Apply the MORPH_HITORMISS function to the binary image. Image regions matching the hit and miss conditions are designated at matches:
- Display the elements matching the hit and miss conditions, dilating the elements to the radius of a hit:
- Display the original image overlaid with the matching elements:
file = FILEPATH('r_seeberi.jpg', $
SUBDIRECTORY = ['examples','data'])
READ_JPEG, file, img, /GRAYSCALE
dims = SIZE(img, /DIMENSIONS) padImg = REPLICATE(0B, dims[0]+10, dims[1]+10) padImg [5,5] = imgFailing to pad an image causes all objects occurring at the edges of the image to fail the hit and miss conditions.
dims = SIZE(padImg, /DIMENSIONS) WINDOW, 0, XSIZE = 3*dims[0], YSIZE = 2*dims[1], $ TITLE='Displaying Hit-or-Miss Matches' TVSCL, padImg, 0
radstr = 7 strucElem = SHIFT(DIST(2*radstr+1), radstr, radstr) LE radstrTip
EnterPRINT, strucElemto view the structure created by the previous statement.
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.
threshImg = openImg GE 150 WSET, 0 TVSCL, threshImg, 2The results of opening (left) and thresholding (right) are shown in the following figure.
radhit = 7 radmiss = 23 hit = SHIFT(DIST(2*radhit+1), radhit, radhit) LE radhit miss = SHIFT(DIST(2*radmiss+1), radmiss, radmiss) GE radmissWhile the shapes of the structuring elements are purposefully circular, the sizes were chosen after empirically testing, seeking elements suitable for this example.
Tip
EnterPRINT, hitorPRINT, missto 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?"
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).
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.
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.



