Main Content

bwareafilt

Extract objects from binary image by size

Description

BW2 = bwareafilt(BW,range) extracts all connected components (objects) from the binary image BW, where the area of the objects is in the specified range, producing another binary image BW2. bwareafilt returns a binary image BW2 containing only those objects that meet the criteria.

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.

example

BW2 = bwareafilt(BW,n,keep) specifies whether to keep the n largest objects or the n smallest objects.

BW2 = bwareafilt(___,conn) specifies the pixel connectivity that defines the objects.

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')

Figure contains an axes object. The hidden axes object contains an object of type image.

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')

Figure contains an axes object. The hidden axes object contains an object of type image.

Input Arguments

collapse all

Image to be filtered, specified as a binary image.

Data Types: logical

Minimum and maximum values of the area, specified as a 2-element 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 numeric scalar.

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 | string

Pixel connectivity, specified as one of these values.

Value

Meaning

Two-Dimensional Connectivities

4

Pixels are connected if their edges touch. Two adjoining pixels are part of the same object if they are both on and are connected along the horizontal or vertical direction.

Center pixel connected to four pixels

Current pixel is shown in gray.

8

Pixels are connected if their edges or corners touch. Two adjoining pixels are part of the same object if they are both on and are connected along the horizontal, vertical, or diagonal direction.

Center pixel connected to eight pixels

Current pixel is shown in gray.

Connectivity can also be defined in a more general way by specifying a 3-by-3 matrix of 0s and 1s. The 1-valued elements define neighborhood locations relative to the center element of conn. The matrix must be symmetric about its center element.

Data Types: double | logical

Output Arguments

collapse all

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

Extended Capabilities

Version History

Introduced in R2014b

expand all