bwareafilt

Extract objects from binary image by size

Description

example

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.

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

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 area, 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 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-connected

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.

8-connected

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.

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.

Introduced in R2014b