Main Content

bwareaopen

Remove small objects from binary image

Description

example

BW2 = bwareaopen(BW,P) removes all connected components (objects) that have fewer than P pixels from the binary image BW, producing another binary image, BW2. This operation is known as an area opening.

BW2 = bwareaopen(BW,P,conn) removes all connected components, where conn specifies the desired connectivity.

Examples

collapse all

Read binary image.

BW = imread('text.png');

Remove objects containing fewer than 50 pixels using bwareaopen function.

BW2 = bwareaopen(BW, 50);

Display original image next to morphologically opened image.

imshowpair(BW,BW2,'montage')

Input Arguments

collapse all

Binary image, specified as a logical or numeric array of any dimension.

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

Maximum number of pixels in objects, specified as a nonnegative integer.

Example: 50

Data Types: double

Pixel connectivity, specified as one of the values in this table. The default connectivity is 8 for 2-D images, and 26 for 3-D images.

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.

Three-Dimensional Connectivities

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.

For higher dimensions, bwareaopen uses the default value conndef(ndims(BW),'maximal').

Connectivity can also be defined in a more general way for any dimension by specifying a 3-by-3-by- ... -by-3 matrix of 0s and 1s. The 1-valued elements define neighborhood locations relative to the center element of conn. Note that conn must be symmetric about its center element. See Specifying Custom Connectivities for more information.

Data Types: double | logical

Output Arguments

collapse all

Area-opened image, returned as a logical array of the same size as BW.

Algorithms

The basic steps are

  1. Determine the connected components:

    CC = bwconncomp(BW, conn);
  2. Compute the area of each component:

    S = regionprops(CC, 'Area');
  3. Remove small objects:

    L = labelmatrix(CC);
    BW2 = ismember(L, find([S.Area] >= P));
    

Extended Capabilities

Version History

Introduced before R2006a