hit_miss
result = hit_miss(img, se_fg, se_bg)
Input: img, se_fg, se_bg · Output: result
What it is
img is a binary Image; se_fg/se_bg are structuring-element mask Images (from disk()/line(), or hand-built with e.g. image_from_matrix). The morphological hit-or-miss transform: output pixel (x, y) is foreground iff EVERY foreground pixel of se_fg (relative to its own center) lands on a foreground pixel of img at that offset from (x, y), AND EVERY foreground pixel of se_bg lands on a BACKGROUND pixel โ a position neither SE covers is a don't-care. Out-of-range neighbors count as background (same border convention distance_transform/fill_holes use). The standard way to isolate a specific local pattern (corners, endpoints, ...) in a binary image.
Overloads
One call shape — this function is not overloaded.
| Code | Input | Output | Description |
result = hit_miss(img, se_fg, se_bg) | img Imageimg is a binary Image.se_fg Imagese_fg/se_bg are structuring-element mask Images (from disk()/line(), or hand-built with e.g.se_bg Imagese_fg/se_bg are structuring-element mask Images (from disk()/line(), or hand-built with e.g. | result Image | Returns a new binary Image, same dimensions as img. Added in v0.4.0. |
Examples
From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.
# disk(1) is a plus shape, not a 3x3 square: eroding a 1-pixel-wide plus
# with the radius-implied square wipes it out completely, but a disk SE
# of the same radius exactly matches the plus's own footprint and leaves
# its center pixel standing.
plus = image_from_matrix([0,0,0,0,0; 0,0,255,0,0; 0,255,255,255,0; 0,0,255,0,0; 0,0,0,0,0])
square_eroded = imerode(plus, 1)
print(imhist(square_eroded, n_bins=2)) # [25, 0] -- wiped out entirely
se = disk(1)
disk_eroded = imerode(plus, se=se)
print(imhist(disk_eroded, n_bins=2)) # [24, 1] -- only the center pixel survives
# hit_miss isolating the top-left corner of a solid 3x3 square: fg
# requires self/east/south/south-east foreground, bg requires
# west/north/north-west background -- true only at the one corner pixel.
sq = image_from_matrix([0,0,0,0,0; 0,255,255,255,0; 0,255,255,255,0; 0,255,255,255,0; 0,0,0,0,0])
fg = image_from_matrix([0,0,0; 0,255,255; 0,255,255])
bg = image_from_matrix([255,255,0; 255,0,0; 0,0,0])
corner = hit_miss(sq, fg, bg)
print(imhist(corner, n_bins=2)) # [24, 1] -- exactly one corner pixel found
[25, 0] [24, 1] [24, 1]
from images
How it works
hit_miss(img, se_fg, se_bg) โ the morphological hit-or-miss transform (ยง SE/hit-miss/watershed pass, 2026-09-25): se_fg/se_bg are structuring-element mask Images (same shape disk()/line()/se= above use), read via the same image_to_se_offsets conversion. See image::Image::hit_miss's own doc comment for the exact matching rule and the border (out-of-range = background) convention.
From the note above this builtin in the interpreter, where the reasoning was written down as it was built.