Steve on Image Processing

June 12th, 2007

Connected component labeling - Part 7

I described in my previous connected component labeling post the algorithm used by bwlabeln. This time I'll talk about the variation used by bwlabel. This variation uses run-length encoding as the first step.

Here's what I mean by run-length encoding. Consider this small binary image matrix:

Such a binary image can be represented as a collection of runs, or sequences of 1s. Working columnwise, this image contains 9 runs:

Each run can be represented by its starting pixel and by the number of pixels in the run, which is called the run length. Hence the term run-length encoding.

For a larger binary image containing larger objects, the number of runs can be much smaller than the number of foreground pixels. But the connectivity analysis can be completely determined based on the runs. In our small example:

  • Run #3 is connected to run #1
  • Run #4 is connected to run #2
  • Run #6 is connected to run #4
  • Run #7 is connected to run #5
  • Run #8 is connected to run #6
  • Run #9 is connected to run #7
  • Run #9 is connected to run #8

This set of connectivity pairs can be resolved into equivalence classes using the technique I described earlier based on dmperm. In this example there are only two equivalence classes, corresponding to the two connected components.

The function bwlabel calls computes a run-length encoding first. As it finds each run, it also determines which runs on the previous column are connected, if any. Then it constructs a sparse adjacency matrix and calls dmperm to compute the equivalence classes. Then it labels each run in the output label matrix according to the equivalence class it belongs to.

This is a very efficient method because it scans each pixel in the input image only once, and also because the adjacency matrix is usually relatively small. It is R-by-R, where R is the number of runs.

You can find this method described in Haralick and Shapiro, Computer and Robot Vision Volume 1, Addison-Wesley, 1992, pp. 40-48. (Note that the equivalence class resolution method used by bwlabel is different than the one described in the book.)


Get the MATLAB code

Published with MATLAB® 7.4

6 Responses to “Connected component labeling - Part 7”

  1. Emanuele Rodolà replied on :

    Just finished reading through your seven-parts tour. Thanks a lot, found it very useful!

  2. Steve replied on :

    Emanuele—I’m glad you liked it. Thanks for taking the time to comment.

  3. jxwht replied on :

    Hi Steve,
    As you mentioned before, we can find the 4- or 8- connected “pairs” by using linear indexing and linear neighbor index offsets, is there a similar algorithm to find the 4- or 8- connected “pairs” RUNs?
    Thank you!

  4. Steve replied on :

    Jxwht—Not directly by using indexing offsets, but it can be programmed in MATLAB. I have done it in the past, but a long time ago. I don’t have code for it anymore.

  5. Ashwati replied on :

    Dear Steve,
    I am currently doing a project in Digital Image Processing. I just wanted to say that I have followed your book, DIP with Matlab, and have found it a very satisfying experience, especially the section of Frequency Domain processing, cleared a lot of cobwebs. Thank you very much.
    Ashwati

  6. Steve replied on :

    Ashwati—Thanks! I forwarded your comment to Dr. Gonzalez, who wrote the frequency domain processing chapter.

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.