This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.


Remove small objects from binary image


  • BW2 = bwareaopen(BW,P)
  • BW2 = bwareaopen(BW,P,conn)



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. The default connectivity is 8 for two dimensions, 26 for three dimensions, and conndef(ndims(BW), 'maximal') for higher dimensions. This operation is known as an area opening.

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

Code Generation support: Yes.

MATLAB® Function Block support: No.


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.


Input Arguments

collapse all

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

Example: BW2 = bwareaopen(BW, 50);

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

Maximum number of pixels in objects, specified as a nonnegative, integer-valued, numeric scalar.

Example: BW2 = bwareaopen(BW, 50);

Data Types: double

Connectivity, specified as one of the following numeric scalars:



Two-dimensional connectivities


4-connected neighborhood


8-connected neighborhood

Three-dimensional connectivities


6-connected neighborhood


18-connected neighborhood


26-connected neighborhood

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 central element of conn. Note that conn must be symmetric about its central element.

Example: BW2 = bwareaopen(BW, 50, 4);

Data Types: double | logical

Output Arguments

collapse all

Image that has been morphologically opened, specified as a nonsparse, logical array the same size as BW.

More About

collapse all

Code Generation

This function supports the generation of C code using MATLAB Coder™. For more information, see Code Generation for Image Processing.

When generating code, note the following:

  • BW must be a 2-D binary image. N-D arrays are not supported.

  • conn can only one of the two-dimensional connectivities (4 or 8) or a 3-by-3 matrix. The 3-D connectivities (6, 18, and 26) are not supported. Matrices of size 3-by-3-by-...-by-3 are not supported.

  • conn must be a compile-time constant.


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

See Also


Introduced before R2006a

Was this topic helpful?