Main Content

bwvolumefilt

R2026b

Extract objects from 3-D binary image by volume

Since R2026b

Description

BW3 = bwvolumefilt(BW,range) extracts all connected components (objects) from the volumetric binary image, BW, where the volume of the objects is in the specified range, producing another volumetric binary image BW3. The function bwvolumefilt returns a volumetric binary image BW3 containing only those objects that meet the criteria.

BW3 = bwvolumefilt(BW,n) keeps the n largest objects. In the event of a tie for nth place, only the first n objects are included in BW3.

example

BW3 = bwvolumefilt(BW,n,keep) specifies whether the n objects to keep are the largest objects or the smallest objects.

BW3 = bwvolumefilt(___,conn) also specifies the pixel connectivity that defines the objects, in addition to any combination of input arguments from previous syntaxes.

Examples

collapse all

Define dimensions for a Volume object.

imSize = [40 40 40];

Create a logical, volumetric image that contains seven spherical objects of radius r. Display the image.

numSeeds = 8;
rng(0);
seedIdx = randi(prod(imSize),numSeeds,1);
BWseeds = zeros(imSize,"logical");
BWseeds(seedIdx) = 1;
r = 7;
BW = imdilate(BWseeds,strel("sphere",r));
volshow(BW)

Find the three largest objects by volume, and filter the image. Display filtered image.

BW3 = bwvolumefilt(BW,3);
volshow(BW3)

Input Arguments

collapse all

Volumetric binary image to be filtered, specified as a logical array.

Data Types: logical

Minimum and maximum values of the volume, 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 positive integer.

Data Types: double

Objects to retain in the output image, specified as "largest" or "smallest". In the event of a tie for nth place, bwvolumefilt includes only the first n objects.

Data Types: char | string

Pixel connectivity, specified as one of these values.

Value

Meaning

6

Pixels are connected if their faces touch. Two adjoining pixels are part of the same object if they are both on and are connected in:

  • One of these directions: in, out, left, right, up, and down

Center pixel connected to the faces of 6 pixels

Current pixel is center of cube.

18

Pixels are connected if their faces or edges touch. Two adjoining pixels are part of the same object if they are both on and are connected in:

  • One of these directions: in, out, left, right, up, and down

  • A combination of two directions, such as right-down or in-up

Center pixel connected to the faces of 6 pixels and the edges of 12 pixels

Current pixel is center of cube.

26

Pixels are connected if their faces, edges, or corners touch. Two adjoining pixels are part of the same object if they are both on and are connected in:

  • One of these directions: in, out, left, right, up, and down

  • A combination of two directions, such as right-down or in-up

  • A combination of three directions, such as in-right-up or in-left-down

Center pixel connected to the faces of 6 pixels, the edges of 12 pixels, and the corners of 8 pixels

Current pixel is center of cube.

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

Data Types: double | logical

Output Arguments

collapse all

Filtered volumetric binary image, returned as a logical array of the same size as BW.

Tips

  • bwvolumefilt filters objects by volume using the bwpropfilt3 function.

Extended Capabilities

expand all

Version History

Introduced in R2026b