Hit-or-miss transform¶
| Fact | Value |
|---|---|
| Also called | Hit-and-miss transform |
| Status | Implemented and tested |
| Family | Morphological filters |
| Engine | Browser |
| Type | Nonlinear, spatial, pattern match with two elements |
| Output | Mask |
| Operator shown as | structuring-element (exact) |
| Support radius | half the pattern size |
| Code | web/src/core/filters/morphology/binary.ts, hitOrMissFilter |
| Operator code | web/src/core/filters/morphology/binary.ts, hitOrMissFilter |
| Channels | Brightness only, RGB |
Overview¶
The hit-or-miss transform marks the pixels where a small pattern matches exactly. The pattern says, for each cell, whether the pixel must be white, must be black, or may be either. It finds isolated points, line ends, corners and other exact local shapes.
How it works¶
- Write the pattern with 1 for white, -1 for black and 0 for either.
- Place it on each pixel.
- Mark the pixel if every 1 lies on white and every -1 on black.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(A\): The foreground
- \(B_1, B_2\): The pattern pixels that must be foreground and background
B_1 holds the pattern pixels that must be foreground and B_2 those that must be background. A pixel is marked when B_1 fits inside the foreground and B_2 inside the background, both placed at that pixel. Pixels marked 0 in the pattern may be either. Values other than 1, -1 and 0 make the filter use the default pattern.
The pattern holds two disjoint elements: B_1, the cells that must be white, and B_2, the cells that must be black. The transform is the erosion of the white set by B_1 intersected with the erosion of the black set by B_2. A pixel is marked only when both fit.
Kernel or operator¶
The Inspector shows B_1 as a grid and gives the size of B_2.
Algorithm¶
- Extend the image with the border mode and make it binary.
- At each pixel, compare the pattern with the pixels under it.
- Mark the pixel where every 1 is foreground and every -1 is background, and cut the extension off.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Pattern pattern |
-1 -1 -1; -1 1 -1; -1 -1 -1 | Rows of numbers, odd sides up to 15 | Rows of 1 (must be foreground), -1 (must be background) and 0 (either). Odd sides, up to 15. |
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 |
Brightness only | RGB, Brightness only | Which part of the color image the filter changes. |
Implementation¶
hitOrMissFilter in web/src/core/filters/morphology/binary.ts compares the pattern with the binary image at each pixel. It extends the image once by half the pattern size with the border mode, so strips and whole images agree. The filter runs in the browser engine through the shared filter framework in web/src/core/filters/run.ts, which handles the channel scope and alpha. Values at or above 128 count as white, and the result is 255 or 0.
Worked example¶
| Row | Input | Result |
|---|---|---|
| 0 | 1 0 0 0 0 0 0 0 0 0 |
1 0 0 0 0 0 0 0 0 0 |
| 1 | 0 0 0 0 0 0 0 0 0 0 |
0 0 0 0 0 0 0 0 0 0 |
| 2 | 0 1 1 1 1 1 1 0 0 0 |
0 0 0 0 0 0 0 0 0 0 |
| 3 | 0 1 1 1 1 1 1 0 0 0 |
0 0 0 0 0 0 0 0 0 0 |
| 4 | 0 1 1 1 1 1 1 0 0 0 |
0 0 0 0 0 0 0 0 0 0 |
| 5 | 0 0 0 0 0 0 0 0 0 0 |
0 0 0 0 0 0 0 0 0 0 |
| 6 | 0 0 1 0 0 0 0 0 0 0 |
0 0 0 0 0 0 0 0 0 0 |
| 7 | 0 0 1 0 0 0 0 0 0 0 |
0 0 0 0 0 0 0 0 0 0 |
| 8 | 0 0 1 1 1 0 0 0 0 0 |
0 0 0 0 0 0 0 0 0 0 |
A small binary image, 1 for white and 0 for black. The default pattern needs a white center with eight black neighbors, so only the isolated pixel at the top left is marked.
Complexity¶
- Time: \(O(N K)\)
- Memory: \(O(N)\)
N is the number of pixels per channel and K the size of the pattern.
Artifacts and limitations¶
Limitations
- One pattern finds one orientation. Rotated shapes need rotated patterns.
Artifacts
- Missed matches where noise changes a single pixel.
Tests¶
The reference runs scipy.ndimage.binary_hit_or_miss on the extended binary plane. The cases cover two patterns 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.