imerode
result =imerode(img, radius)<br>imerode(img, se=)
Input: img, radius · Output: result
What it is
img is a binary Image; radius is a positive integer, the half-width of a square structuring element of side 2*radius+1. Binary morphological erosion — shrinks foreground regions and removes specks smaller than the element.
Overloads
One call shape — this function is not overloaded.
| Code | Input | Output | Description |
result = imerode(img, radius) <br> imerode(img, se=) | img Imageimg is a binary Image.radius —radius is a positive integer, the half-width of a square structuring element of side 2*radius+1. | imdilate —see "Structuring elements" above | Returns a new Image, same dimensions as img. Same additive se= kwarg as imdilate (see "Structuring elements" above), 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
m = [0, 0, 0, 0, 0, 0; 0, 255, 255, 0, 255, 0; 0, 255, 255, 0, 0, 0; 0, 0, 0, 0, 0, 0; 0, 255, 0, 0, 255, 255; 0, 0, 0, 0, 255, 255]
img = image_from_matrix(m)
imshow(img)
eroded = imerode(img, 1)
dilated = imdilate(img, 1)
opened = imopen(img, 1) # drops the lone speck
closed = imclose(img, 1) # fills small gaps
bright = imtophat(img, 1) # small bright features
dark = imbothat(img, 1) # small dark features
imshow(opened)
print(eroded.width, dilated.width, opened.width, closed.width, bright.width, dark.width)
6 6 6 6 6 6
from images
How it works
and foreground/background separation (§ blob-manipulation pass, 2026-08-24). See image::Image's own "Binary morphology..." doc-comment section for the full binary-mask convention this whole group shares: a Value::Image is treated as "binary" when every pixel is grayscale (R=G=B) with two intended levels, 0 = background / 255 = foreground (is_foreground actually reads >=128, so a slightly antialiased mask still behaves sanely). No threshold/ otsu_threshold builtin existed in this shared working tree when this landed (a sibling agent's own lane) — this is a documented, independent design choice, not a guess at matching code that hadn't landed yet; reconcile if the sibling's own producer ends up using a different convention. imerode(img, radius) / imdilate(img, radius) — a square structuring element of side 2*radius+1 (documented choice over a disk — see image::Image::erode's doc comment). Additive se= kwarg (§ SE/hit-miss/watershed pass, 2026-09-25): an explicit structuring-element mask Image (disk()/line(), or hand-built) in place of the radius-implied square — radius is simply not read at all when se= is given, so the old two-positional-argument form keeps working unchanged.
From the note above this builtin in the interpreter, where the reasoning was written down as it was built.