bwpropfilt

Extract objects from binary image using properties

Description

example

BW2 = bwpropfilt(BW,attrib,range) extracts all connected components (objects) from a binary image BW whose value of property attrib is in the specified range. bwpropfilt returns a binary image BW2 containing only those objects that meet the criteria.

example

BW2 = bwpropfilt(BW,attrib,n) sorts the objects based on the value of the specified property, attrib, returning a binary image that contains only the top n largest objects. In the event of a tie for n-th place, bwpropfilt keeps only the first n objects in BW2.

BW2 = bwpropfilt(BW,attrib,n,keep) specifies whether to keep the n largest objects or the n smallest objects when sorted by property attrib.

BW2 = bwpropfilt(BW,I,attrib,___) sorts objects based on the intensity values in the grayscale image I and the property attrib.

BW2 = bwpropfilt(BW,___,conn) specifies the pixel connectivity, conn.

Examples

collapse all

Read image and display it.

BW = imread('text.png');
figure
imshow(BW)
title('Original Image')

Use filtering to create a second image that contains only those regions in the original image that do not have holes. For these regions, the Euler number property is equal to 1. Display filtered image.

BW2 = bwpropfilt(BW,'EulerNumber',[1 1]);
figure
imshow(BW2)
title('Regions with Euler Number == 1')

Read image.

BW = imread('text.png');

Find the ten objects in the image with the largest perimeters and display filtered image.

BW2 = bwpropfilt(BW,'perimeter',10);
figure;
imshow(BW2)
title('Objects with the Largest Perimeters')

Input Arguments

collapse all

Image to be filtered, specified as a binary image.

Data Types: logical

Name of attribute on which to filter, specified as one of the following values. For detailed information about these attributes, see regionprops.

AreaEulerNumberMinorAxisLength
ConvexAreaExtentOrientation
EccentricityFilledAreaPerimeter
EquivDiameterMajorAxisLengthSolidity

If you specify a grayscale image, then attrib can have one of these additional values.

MaxIntensityMeanIntensityMinIntensity

Data Types: char | string

Minimum and maximum property values, 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 object to return, specified as a positive integer.

Data Types: double

Objects to retain, specified as 'largest' or 'smallest'.

Data Types: char | string

Marker image, specified as a grayscale image, the same size as the input binary image. Intensity values in the grayscale image define regions in the input binary image.

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

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 the same size as BW.

Introduced in R2014b