Main Content

bwconncomp

Find and count connected components in binary image

Description

example

CC= bwconncomp(BW)finds and counts the connected componentsCCin the binary imageBW. TheCCoutput structure contains the total number of connected components, such as regions of interest (ROIs), in the image and the pixel indices assigned to each component.bwconncompuses a default connectivity of 8 for two dimensions and 26 for three dimensions.

example

CC= bwconncomp(BW,conn)specifies the desired connectivityconnfor the connected components.

Examples

collapse all

Create a small sample 3-D array.

BW = cat(3, [1 1 0; 0 0 0; 1 0 0],...[0 1 0; 0 0 0; 0 1 0],...[0 1 1; 0 0 0; 0 0 1]);

Find the connected components in the array.

CC = bwconncomp(BW)
CC =struct with fields:Connectivity: 26 ImageSize: [3 3 3] NumObjects: 2 PixelIdxList: {[5x1 double] [3x1 double]}

Calculate centroids of the objects in the array.

S = regionprops(CC,'Centroid')
S=2×1 struct array with fields:Centroid

Read image into the workspace and display it.

BW = imread('text.png'); imshow(BW)

Figure contains an axes object. The axes object contains an object of type image.

Find the number of connected components in the image.

CC = bwconncomp(BW)
CC =struct with fields:Connectivity: 8 ImageSize: [256 256] NumObjects: 88 PixelIdxList: {1x88 cell}

Determine which is the largest component in the image and erase it (set all the pixels to 0).

numPixels = cellfun(@numel,CC.PixelIdxList); [biggest,idx] = max(numPixels); BW(CC.PixelIdxList{idx}) = 0;

Display the image, noting that the largest component happens to be the two consecutive f's in the word different.

figure imshow(BW)

Figure contains an axes object. The axes object contains an object of type image.

Input Arguments

collapse all

Binary image, specified as a numeric or logical array of any dimension.For numeric input, any nonzero pixels are considered to be1(true).

Example:BW = imread('text.png');

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

Pixel connectivity, specified as one of the values in this table. The default connectivity is8for 2-D images, and26for 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

边缘像素连接如果他们的脸或联系. 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,bwconncompuses the default valueconndef(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 of0s and1s. The1-valued elements define neighborhood locations relative to the center element ofconn. Note thatconnmust be symmetric about its center element. SeeSpecifying Custom Connectivitiesfor more information.

Data Types:double|logical

Output Arguments

collapse all

Connected components, returned as a structure with four fields.

Field Description
Connectivity Connectivity of the connected components (objects)
ImageSize Size ofBW
NumObjects Number of connected components (objects) inBW
PixelIdxList 1-by-NumObjectscell array where thek-th element in the cell array is a vector containing the linear indices of the pixels in thek-th object.

Tips

  • The functionsbwlabel,bwlabeln, andbwconncompall compute connected components for binary images.bwconncompreplaces the use ofbwlabelandbwlabeln. It uses significantly less memory and is sometimes faster than the other functions.

    Function Input Dimension Output Form Memory Use Connectivity
    bwlabel 2-D Label matrix with double-precision High 4 or 8
    bwlabeln N-D Double-precision label matrix High Any
    bwconncomp N-D CCstruct Low Any
  • To extract features from a binary image usingregionpropswith default connectivity, just passBWdirectly intoregionpropsusing the commandregionprops(BW).

  • To compute a label matrix having more memory-efficient data type (for instance,uint8versusdouble), use thelabelmatrixfunction on the output ofbwconncomp.

Extended Capabilities

已经rsion History

Introduced in R2009a