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.

  • Steve: Kezia—Try imrotate.
  • kezia: steve, how to perform rotation of structuring element by 15 degrees. kindly answer my question. thank u kezia...
  • Steve: Tasha—I only accept comments that are relevant to the particular blog post or are questions or comments...
  • Tasha: Steve,I send you a comment here but still didn’t get any reply yet.I did not see my comment posted here...
  • Steve: Carsten—Thanks for your input.
  • Carsten: Another vote for either imtranslate.m, or at least a blurb in the imtransform help why pure translation...
  • Loren Shure: If you look towards the end of the fftfilt program, you will see that there’s a check to see if...
  • Steve: Sonja—My imwritesize submission on the MATLAB Central File Exchange might be helpful. It was posted...
  • Steve: Grant—Sorry, but it won’t be for R2010a. That development deadline has already passed.
  • Sonja: My publisher is wanting images for a new book to be 300 dpi. Only 5 of the 19 images are 300, the rest are...

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