|
Image Processing : Extracting and Analyzing Shapes |
|
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. |
DEVICE, DECOMPOSED = 0, RETAIN = 2 LOADCT, 0
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] = img
Failing 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 radstr
| Tip Enter PRINT, strucElem to view the structure created by the previous statement. |
openImg = MORPH_OPEN(padImg, strucElem, /GRAY) TVSCL, openImg, 1
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, 2
The 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 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?"
![]() |
matches = MORPH_HITORMISS(threshImg, hit, miss)
dmatches = DILATE(matches, hit) TVSCL, dmatches, 3
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).
![]() |
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.
IDL Online Help (March 06, 2007)