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

7 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

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


Steve Eddins manages the Image & Geospatial development team at The MathWorks and coauthored Digital Image Processing Using MATLAB. He writes here about image processing concepts, algorithm implementations, and MATLAB.

  • Sana: hi steve, could you explain to me how i would be able to use the dir function, to do a loop through a directory...
  • Nishtha: Sir, I have preprocessed the image in following steps: [1] adaptive histogram equalization [2] thresholding...
  • Kristof: I also strongly support the idea. I have just recently bumped into the problem that im2single was not...
  • Steve: David—I’ m glad you found it useful!
  • David Lalejini: I found your example very useful for finding connected nodes in a large set of input pairs. I start...
  • tommy: Dear Steve, I have a question,please if you are kind to help me regarding the accumulator array dimensions of...
  • Steve: Abc—I don’t know how to distinguish the faces. You might try posting your question in the MATLAB...
  • Manju: well if we have a few ovals within each other like in a cell how do we measure the distance from the center...
  • Steve: Manju—What do you mean? How is each region defined?
  • Manju: if we have 2-3 regions within each other how do we measure the regions of each one?

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