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.

  • murat: Hi Steve, I have an rgb image of a kind of cream and it contains some small black particles (black dots). In...
  • Steve: Ernest—Look at setting the FaceColor property. The code for setting that is shown on the page you asked...
  • Ernest Miller: Hi Steve, Understood. However, can you explain how to change the colors? Thanks, Ernest
  • Jan: Hi Steve Very useful code, yet what if I parts of my rotated+translated object are outside the original...
  • Steve: MoHDa—It might be possible. You’ll need to use one of the options that produces closed edge...
  • MoHDa: I have one question about the ROIPOLY: I have an image with stripes, I use the “edge” command for...
  • Steve: Shahn—My November 17, 2006 post shows you how to do it.
  • Steve: Kay-Uwe—Thanks for following up. I am planning to make it easier to use test directories in a package....
  • shahn: Hello Steve Instead of superimposing a star on the image to show the centroide. How would you superimpose a...
  • Kay-Uwe: Having TestSuite.fromPackag e() would be nice to have, but so far using simple “test” subdirs...

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