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



Just finished reading through your seven-parts tour. Thanks a lot, found it very useful!
Emanuele—I’m glad you liked it. Thanks for taking the time to comment.
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!
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.
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
Ashwati—Thanks! I forwarded your comment to Dr. Gonzalez, who wrote the frequency domain processing chapter.