Qu

bwlabel

Builtin index · Images

result = bwlabel(binary_img, [connectivity=8]) (alias of label_blobs)

Input: binary_img, connectivity · Output: result

What it is

binary_img is a binary (0/255) Image. connectivity is an optional named number, 4 or 8 (default 8), the neighbor rule used for flood-fill labeling. Connected-component labeling.

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
result = bwlabel(binary_img, [connectivity=8]) (alias of label_blobs)
binary_img Imagebinary_img is a binary (0/255) Image.
connectivity number, optional (default 8)connectivity is an optional named number, 4 or 8 (default 8), the neighbor rule used for flood-fill labeling.
Model —kind "blobs"
Returns a Model (kind "blobs") with fields .labels (a Mat of the same height/width as binary_img, integer label ids, background = 0), .count (a number, the blob count), .width, .height (the source image's own dimensions).

Examples

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

# Three bright discs on a dark field, written out and read back. The blob

# count printed below is then checkable against an image the reader can

# see being built, rather than asserted about a file nobody has.

n = 60
field = zeros(n, n)
centres = [15, 15; 42, 20; 30, 45]
for i in 0 to n - 1
    for j in 0 to n - 1
        for k in 0 to 2
            dy = i - centres[k, 0]
            dx = j - centres[k, 1]
            if dy * dy + dx * dx < 64
                field[i, j] = 230
            end if
        end for
    end for
end for
save_image("cells.bmp", image_from_matrix(field))

img = load_image("cells.bmp")
level = otsu_threshold(img)
mask = threshold(img, level)
clean = bwareaopen(mask, 20)          # drop specks smaller than 20 px

imshow(clean)

labeled = bwlabel(clean)
print(labeled.count)                  # number of detected blobs


stats = regionprops(labeled)
for i = 1 to labeled.count
    r = stats[i - 1]
    print(r.label, r.area, r.centroid_x, r.centroid_y)
end for
output
3
1 193 15 15
2 193 45 30
3 193 20 42

from images

How it works

label_blobs(binary_img, [connectivity=8]) (alias bwlabel) — connected-component labeling (see image::Image::label_components's doc comment for the flood-fill algorithm). Returns a Value::Model (kind "blobs", the multi-result-in-one-handle protocol this codebase's model values already use) rather than a tuple: .labels (a Value::Mat of integer label ids, background = 0), .count (blob count), plus .width/.height (the source image's own dimensions).

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