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.


Extract objects from binary image by size


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



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.


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) keeps the n largest objects, by default. If you want to keep n smallest object, specify the keep parameter with the value 'smallest'.

BW2 = bwareafilt(___,conn) extracts objects from a binary image where conn specifies the connectivity that defines the objects.


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.


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.


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

Connectivity, specified as the value 4 or 8, or as a 3-by-3 matrix of zeros and ones. By default, bwarefilt uses 8-connected neighborhoods. Connectivity can be defined in a more general way by using for conn a 3-by-3 matrix of 0s and 1s. The 1-valued elements of conn define neighborhood locations relative to the center element. conn must be symmetric around its center element.

Data Types: double | logical

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?