Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

bwareafilt

Extract objects from binary image by size

Syntax

BW2 = bwareafilt(BW,range)
BW2 = bwareafilt(BW,n)
BW2 = bwareafilt(BW,n,keep)

Description

example

BW2 = bwareafilt(BW,range) extracts all connected components (objects) from the binary image BW, where the area of the objects is in range, producing another binary image BW2. range is a 2-by-1 vector of minimum and maximum sizes (inclusive). bwareafilt removes objects that do not meet the criterion. The default connectivity is 8.

example

BW2 = bwareafilt(BW,n) keeps the n largest objects. In the event of a tie for n-th place, only the first n objects are included in BW2.

BW2 = bwareafilt(BW,n,keep) retains the n largest objects if keep is 'largest' (the default) or the n smallest if keep is 'smallest'.

Examples

collapse all

Read image.

BW = imread('text.png');

Filter image, retaining only those objects with areas between 40 and 50.

BW2 = bwareafilt(BW,[40 50]);

Display the original image and filtered image side by side.

imshowpair(BW,BW2,'montage')

Read image.

BW = imread('text.png');

Filter image, retaining only the 5 objects with the largest areas.

BW2 = bwareafilt(BW,5);

Display the original image and the filtered image side by side.

imshowpair(BW,BW2,'montage')

Input Arguments

collapse all

Image to be filtered, specified as a binary image.

Data Types: logical

Minimum and maximum values of the property inclusive, specified as a 2-by-1 numeric vector of the form [low high].

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Number of objects to include when filtering image objects by size, specified as a scalar double.

Data Types: double

Size of objects to include in the output image, specified as ‘largest' or ‘smallest'. In the event of a tie for n-th place, bwareafilt includes only the first n objects.

Data Types: char

Output Arguments

collapse all

Filtered image, returned as a binary image, the same size and class as the input image BW.

Introduced in R2014b

Was this topic helpful?