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.

import image

# Two overlapping circles -- one connected blob to bwlabel.

w = 21
h = 13
mask_m = zeros(h, w)
for y in 0 to h - 1
  for x in 0 to w - 1
    in_a = (x - 6) ^ 2 + (y - 6) ^ 2 <= 25
    in_b = (x - 14) ^ 2 + (y - 6) ^ 2 <= 25
    if in_a or in_b
      mask_m[y, x] = 255
    end if
  end for
end for
mask = image_from_matrix(mask_m)

n_blobs = bwlabel(mask).count
print(n_blobs)   # 1 -- the two circles are one connected blob


dt = image.distance_transform(mask)
surface = -dt                 # each circle's own center floods first

markers = zeros(h, w)
markers[6, 6] = 1              # circle A's own center

markers[6, 14] = 2             # circle B's own center

labels = image.watershed(surface, markers=markers)

print(labels[6, 2])    # 1 -- deep inside circle A only

print(labels[6, 18])   # 2 -- deep inside circle B only

output
1
1
2

from images

# 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.