Steve on Image Processing

March 6th, 2007

Connected component labeling – Part 1

Several people have asked recently about how bwlabel works, so I plan to write about it here during the next several weeks.

bwlabel implements an operation known as connected component labeling, or sometimes blob labeling. So, what is a connected component, and what does it mean to label it?

Let's start by looking at a simple binary image:

bw = imread('http://blogs.mathworks.com/images/steve/119/binary.png');
imshow(bw)

What do you see? Most people would probably say that they see four round objects (or blobs). (Actually, if you look closely at the lower right object, you can see a picture of Cleve Moler from his grad school days. No, just kidding.)

In MATLAB, this image is represented as a simple matrix. It has 74,536 elements. 17,871 of the elements have the value 1; the rest have the value 0. Here are a few of these matrix values:

showPixelValues(bw, [173, 162])

How do we get from 17,871 foreground pixels to four objects? Well, some of the pixels are obviously "connected" to each other. We want to identify all the groups of interconnected foreground pixels. This process is called connected component labeling. (If you are interested in a more precise definition of a connected component, see section 9.4 of Digital Image Processing Using MATLAB.)

In subsequent posts I'll write about several methods you might use to find connected components, including the particular methods used by bwlabel and its multidimensional cousin bwlabeln. I'll also explain how the exact definition of a pixel's neighborhood affects the results.


Get the MATLAB code

Published with MATLAB® 7.4

9 Responses to “Connected component labeling – Part 1”

  1. Capt. Jean-Luc Pikachu replied on :

    Am I the only one who can’t see the images?

  2. Steve replied on :

    I fixed the problem with the missing images. Sorry about that.

  3. fred replied on :

    how can i get rid of small components?

    thanks

  4. Steve replied on :

    Fred—Use bwareaopen.

  5. Nawal replied on :

    Hi steve I wanna to ask you about how we can connect nearest component? such as if we have character “i” as an image and we want to connect dot of i with its stroke?? Thans inadvance

  6. Steve replied on :

    Nawal—Try a morphological closing operation (imclose).

  7. Nawal replied on :

    Thank you steve

  8. ronit replied on :

    I’m writing the bwlabel implementation in matlab.
    Is there an implementation of the union-find in matlab?
    My question releted to the following steps of the two pass algoruthm:
    1.Store the equivalence between neighboring labels
    2.Relabel the element with the lowest equivalent label
    Are there functions in matlab for these two steps?

  9. Steve replied on :

    Ronit—No, you’ll need to implement those steps yourself.


MathWorks
Steve Eddins is a software development manager in the MATLAB and image processing areas at MathWorks. Steve coauthored Digital Image Processing Using MATLAB. He writes here about image processing concepts, algorithm implementations, and MATLAB.

These postings are the author's and don't necessarily represent the opinions of The MathWorks.