| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Image Processing Toolbox |
| Contents | Index |
| Learn more about Image Processing Toolbox |
CC = bwconncomp(BW)
CC = bwconncomp(BW,conn)
CC = bwconncomp(BW) returns the connected components CC found in BW. The binary image BW can have any dimension. CC is a structure with four fields.
| Field | Description |
|---|---|
| Connectivity | Connectivity of the connected components (objects) |
| ImageSize | Size of BW |
| NumObjects | Number of connected components (objects) in BW |
| PixelIdxList | 1-by-NumObjects cell array where the kth element in the cell array is a vector containing the linear indices of the pixels in the kth object. |
bwconncomp uses a default connectivity of 8 for two dimensions, 26 for three dimensions, and conndef(ndims(BW),'maximal') for higher dimensions.
CC = bwconncomp(BW,conn) specifies the desired connectivity for the connected components. conn can have the following scalar values.
Value | Meaning |
|---|---|
Two-dimensional connectivities | |
4-connected neighborhood | |
8-connected neighborhood | |
Three-dimensional connectivities | |
6-connected neighborhood | |
18-connected neighborhood | |
26-connected neighborhood | |
Connectivity can be defined in a more general way for any dimension using a 3-by-3-by- ... -by-3 matrix of 0s and 1s. conn must be symmetric about its center element. The 1-valued elements define neighborhood locations relative to conn.
The functions bwlabel, bwlabeln, and bwconncomp all compute connected components for binary images. bwconncomp replaces the use of bwlabel and bwlabeln. 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 | CC struct | Low | Any |
To extract features from a binary image using regionprops with default connectivity, just pass BW directly into regionprops (i.e., regionprops(BW)).
To compute a label matrix having more memory-efficient data type (e.g., uint8 versus double), use the labelmatrix function on the output of bwconncomp. See the documentation for each function for more information.
BW can be a logical or numeric array of any dimension, and it must be real and nonsparse. CC is a structure.
Calculate the centroids of the 3-D objects.
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]);
CC = bwconncomp(BW);
S = regionprops(CC,'Centroid');
Erase the largest letter from the image.
BW = imread('text.png');
imshow(BW);

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

bwlabel, bwlabeln, labelmatrix, regionprops
![]() | bwboundaries | bwdist | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2010- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |