Qu

foreground_mask

Builtin index · Images

result = foreground_mask(img, level, [radius=1])

Input: img, level, radius · Output: result

What it is

img is an Image, color or grayscale (BT.601 luma either way); level is a required number, the threshold cutoff 0–255; radius is an optional named positive integer (default 1), the structuring-element radius for the cleanup step. Thresholds img at level, then applies imopen(radius) to remove noise specks. level is a required argument by design — pair with otsu_threshold for auto-thresholding: foreground_mask(img, otsu_threshold(img)).

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
result = foreground_mask(img, level, [radius=1])
img Imageimg is an Image, color or grayscale (BT.601 luma either way).
level numberlevel is a required number, the threshold cutoff 0–255.
radius optional (default 1)radius is an optional named positive integer (default 1), the structuring-element radius for the cleanup step.
result Image
Returns a new binary Image, same dimensions as img.

Examples

From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.

m = [10, 20, 30, 220; 15, 25, 200, 230; 120, 130, 210, 225; 125, 135, 205, 215]
img = image_from_matrix(m)

seg = otsu(img)                             # compute Otsu's level and apply it

imshow(seg)

cuts = multi_otsu(img, 3)                   # two cut points -> three bands

banded = multithreshold(img, cuts)
imshow(banded)

k = kapur_threshold(img)                    # entropy-based single threshold

fg = foreground_mask(img, k, radius=1)      # threshold + speck cleanup

print(cuts, k, seg.width, banded.width, fg.width)
output
[30.625, 135.46875] 130.3125 4 4 4
a synthetic field with one bright patch

from images

How it works

foreground_mask(img, level, [radius=1]) — the documented foreground/background separation workflow: threshold img (via BT.601 luma, so a raw color or grayscale image both work, not just an already-grayscaled one) at the explicit, *required* level (0-255), then imopen the result (radius default 1) to remove noise specks, returning the cleaned binary foreground mask. Deliberately takes level as a required argument rather than inventing its own auto- threshold heuristic — that would duplicate/conflict with the sibling threshold/otsu_threshold builtins landing separately in this same working tree. The intended full pipeline once those land is foreground_mask(img, otsu_threshold(img)).

From the note above this builtin in the interpreter, where the reasoning was written down as it was built.