Top-hat¶
| Fact | Value |
|---|---|
| Also called | White top-hat |
| Status | Implemented and tested |
| Family | Morphological filters |
| Engine | Browser |
| Type | Nonlinear, spatial, image minus opening |
| Output | Image |
| Operator shown as | structuring-element (exact) |
| Support radius | 2 x iterations x the reach of the element |
| Code | web/src/core/filters/morphology/gray.ts, tophatFilter |
| Operator code | web/src/core/filters/morphology/gray.ts, morphology |
| Channels | RGB, Brightness only |
Overview¶
The top-hat, or white top-hat, is the image minus its opening. The opening keeps everything the element fits into, so the top-hat keeps only the bright details that are smaller than the element. It also removes a slowly changing background.
How it works¶
- Open the image with the element.
- Subtract the opening from the image.
- Only the small bright details remain.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(f\): The image
- \(B\): The structuring element
The opening keeps everything the element fits into, so the difference keeps only the bright details that are smaller than the element. Erosion takes the smallest value of f(p + z) for z in B. Dilation takes the largest value of f(p - z), which uses the element turned by 180 degrees. Both match the grayscale erosion and dilation of SciPy. On an image of 0 and 255 they are binary erosion and dilation.
The structuring element B is a small set of offsets around the anchor, its center. The editor offers a disk, rectangle, ellipse, cross, diamond, line with angle and length, and a custom grid of 0 and 1. A flat element only selects pixels, it has no weights. Choose an element larger than the details you want and smaller than the background changes. Then the opening follows the background, and the top-hat holds the details on a flat, dark background, ready for a threshold.
Kernel or operator¶
The Inspector shows the structuring element as a grid, with the anchor outlined, and the formula. The Morphology stepper shows the values under the element at the probe.
Algorithm¶
- Extend the image by the reach of all steps with the border mode.
- Open the image with the element.
- Subtract the opening from the image, and cut the extension off.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Structuring element element |
disk 1 | Disk, rectangle, ellipse, cross, diamond, line or custom, with sizes | The shape and size of the neighborhood. Disk 1 is the 3 x 3 cross. |
Iterations iterations |
1 | 1 to 10 | How many times each erosion or dilation runs. |
Border mode border |
Reflect 101 (default) | Reflect 101 (default), Reflect, Replicate, Constant (zero), Wrap | How pixels outside the image are filled when the window reaches past the edge. |
Channels channelScope |
RGB | RGB, Brightness only | Which part of the color image the filter changes. |
Implementation¶
morphology in web/src/core/filters/morphology/gray.ts builds every filter from erodePlane and dilatePlane, which take the smallest or the largest value under the element. The element comes from parseStructuringElement in web/src/core/filters/morphology/structuring.ts. The filter extends the plane once by its full reach with the border mode, runs its steps and cuts the extension off, so strips and whole images agree. The filter runs in the browser engine, in Web Workers, through the shared filter framework in web/src/core/filters/run.ts, which handles the border mode, the channel scope and alpha.
Worked example¶
| x | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Input | 20 | 20 | 200 | 20 | 20 | 20 | 180 | 180 | 180 | 60 | 180 | 180 | 180 | 20 | 20 |
| Top-hat | 0 | 0 | 180 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
One row of an image whose rows are all the same, with the element line 3 0: the pixel and its left and right neighbors. The top-hat is the row minus its opening. Only the narrow bright peak at x = 2 is left. At the ends, the reflect 101 border mirrors the row.
Complexity¶
- Time: \(O(N K)\)
- Memory: \(O(N)\)
N is the number of pixels per channel and K the number of pixels in the element, times the number of steps.
Artifacts and limitations¶
Limitations
- Bright objects larger than the element vanish.
Artifacts
- Halos where the background changes faster than the element.
Tests¶
The reference runs scipy.ndimage.grey_erosion and scipy.ndimage.grey_dilation on the extended plane. Before the cases are written, the script checks that single steps equal SciPy with the border mode applied directly. It does so for a disk, a line and an element that is not symmetric. It also checks that the disk, diamond and ellipse elements equal those of skimage.morphology. The cases cover three elements, one and two iterations, and all five border modes.
References¶
- Gonzalez, R. C. and Woods, R. E. Digital Image Processing, 4th edition. Pearson, 2018. Chapter 9, morphological image processing.
- Soille, P. Morphological Image Analysis, 2nd edition. Springer, 2004. DOI 10.1007/978-3-662-05088-0.
- SciPy documentation, scipy.ndimage.grey_erosion and grey_dilation. https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.grey_erosion.html