If the intensity values in a gray-level image are thought of as elevations, then a scene is composed of mountain tops (brightest points) and valley lows (darkest regions). Uneven contrast in a photo will often degrade threshold isolation of adjacent mountain tops between a valley. To remedy this problem, an open top-hat filter is used to enhance bright (i.e., high) points and a closing top-hat filter is used to enhance the valleys.
Figure 12.5 illustrates how a top-hat filter preserves sharp peaks and eliminates other features, including the poor contrast features. The initial erosion will off the sharp peaks of the mountains if the structuring element honors the "Goldilocks" rule: a bit wider than the widths of the sharp peaks but much smaller than the mountains. The next operation of dilation reconstructs the mountains without the sharp peaks. And subtraction of the original image from the opening gives the top-hat filtered image, the sharp peaks only.
In a similar manner, Figure 12.6 illustrates how a closing top-hat filter highlights sharp valley bottoms. For convenience I will refer to open top-hat filters as top-hat filters and closing top-hat filters as bottom-hat filters.
To illustrate these properties, Figure 12.7a depicts an image of small pellets that have moderate contrast between the bright (middle of pellets) and dark parts (boundaries between pellets). We would like to increase this contrast to enhance segmentation between different pellets. The image was opened with a structuring element and then was subtracted from to give the top-hat result in Figure 12.7b. The rapidly varying bright points in the pellets are enhanced while the gently varying intensities are suppressed. Opening will spatially enlarge the peaks by several pixels, so subtraction of the opening from the original image highlights elevation differences between neighboring pixels at the peaks of rugged mountains. A top-hat filter can be thought of as a conditional gradient that mostly highlights sharp gradients at mountain peaks.
In a similar manner, closing the pellets image flattens the valley bottoms. Subtracting the closed image with the original image highlights the bottom parts of steep valleys. The bottom-hat filter applied to the pellets image is shown in Figure 12.7c, where the valley lows (boundaries between pellets) are highlighted. Again, the bottom-hat filter is similar to a conditional gradient filter, where gradients at the bottom of steep valleys are highlighted. Adding the top-hat and subtracting the bottom-hat transforms to the image results in Figure 12.7d.
A larger version of the pellet image is shown in Figure 12.8. It is obvious the contrast is enhanced by top-hat and bottom-hat filtered image in 12.8d. The histogram of the filtered image in 12.7f shows better contrast than the histogram of the original image in 12.7e.
Thresholding the top-hat and bottom-hat transforms can enhance their contrast properties. In Figure 12.9, top-hat pixels were zeroed out if their brightness value did not exceed the threshold of 15, otherwise their brightness value was amplified by a factor of 80. The threshold for the bottom-hat filter to darken valleys was set to 10. The final image in Figure 12.9d after thresholding of the top-bottom filter shows an improvement over the 12.8d image.