Qu

bwareaopen

Builtin index · Images

result = bwareaopen(binary_img, min_area) (alias of remove_small_blobs)

Input: binary_img, min_area · Output: result

What it is

binary_img is a binary Image; min_area is a number, the minimum pixel count a blob must have to survive. Labels internally (8-connectivity, matching MATLAB's default) and zeroes out every blob whose pixel count is below min_area.

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
result = bwareaopen(binary_img, min_area) (alias of remove_small_blobs)
binary_img Imagebinary_img is a binary Image.
min_area numbermin_area is a number, the minimum pixel count a blob must have to survive.
result Image
Returns a new binary Image, same dimensions as binary_img, with small blobs removed.

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

remove_small_blobs(binary_img, min_area) (alias bwareaopen) — labels internally (no separate label_blobs call needed), zeroes out every blob whose pixel count is below min_area, and returns a cleaned binary image in the same 0/255 mask convention (always 8-connectivity, matching MATLAB's own bwareaopen default).

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