You are here : matlabImage Processingbwconncomp

bwconncomp() - Image Processing

CC = bwconncomp(BW) returns
the connected components CC found in the binary
image BW. bwconncomp uses a
default connectivity of 8 for two dimensions, 26 for three dimensions,
and conndef(ndims(BW),'maximal') for higher dimensions.exampleCC = bwconncomp(BW,conn) returns
the connected components where conn specifies
the desired connectivity for the connected components.Code Generation support:
Yes.MATLABĀ® Function Block support: No.


Syntax

CC = bwconncomp(BW) exampleCC = bwconncomp(BW,conn) example


Example

Calculate Centroids of 3-D ObjectsOpen This Example
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 = 

    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 = 

2x1 struct array with fields:

    Centroid

Erase Largest Component from ImageOpen This Example
Read image into the workspace and display it.BW = imread('text.png');
imshow(BW)

Find the number of connected components in the image.CC = bwconncomp(BW)

CC = 

    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)


Output / Return Value


Limitations


Alternatives / See Also


Reference