Steve on Image Processing

April 15th, 2007

Connected component labeling - Part 4

In the previous post in this series I talked about forming a graph based on pixel neighbor relationships, and then finding the connected components of the graph. Another approach starts by finding a foreground pixel and then iteratively searching that pixel's neighbors to find the connected component. Sometimes this technique is called the flood fill approach. I'll demonstrate the basic idea using a small sample image and a series of diagrams.

The matrix below and on the left is our binary image. Using 4-connected neighbors, the image has two connected components. The matrix on the right is going to become our label matrix when we're done. It is initialized to contain all zeros.

Step 1: Find a foreground pixel. Set the corresponding label matrix pixel to 1, which is the first label.

Step 2: Set the pixels that were just labeled to 0, and then find all the nonzero 4-connected neighbors. Set the corresponding pixels in the label matrix to 1.

Step 3: Set the pixels that were just labeled to 0, and then find all the nonzero 4-connected neighbors. Set the corresponding pixels in the label matrix to 1. (This step should seem familiar.)

Step 4: Set the pixels that were just labeled to 0, and then find all the nonzero 4-connected neighbors. Oops, there aren't any!

Step 5: We have finished labeling object 1. Now search for the next nonzero pixel. Set the corresponding pixel in the label matrix to 2, which is the next object label.

Step 6: Set the pixels that were just labeled to 0, and then find all the nonzero 4-connected neighbors. Set the corresponding pixels in the label matrix to 2.

Step 7: Set the pixels that were just labeled to 0, and then find all the nonzero 4-connected neighbors. Set the corresponding pixels in the label matrix to 2.

Step 8: Set the pixels that were just labeled to 0, and then find all the nonzero 4-connected neighbors. Since there aren't any, we know we have finished labeling object 2. Furthermore, since there are no nonzero pixels left in the image, we know we have completed the labeling operation.

Now let's talk about how you might program this in MATLAB. I'd use the neighbor indexing technique that I wrote about a couple of weeks ago. Start from this point:

Call the matrix on the left bw, and call the matrix on the right L.

bw = logical([0 0 0 0 0 0 0
    0 0 1 0 0 0 0
    0 1 0 1 1 1 0
    0 1 0 0 1 0 0
    0 0 0 0 0 0 0]);

L = [0 0 0 0 0 0 0
    0 1 1 0 0 0 0
    0 1 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0];

The linear indices of the pixels that were just labeled are 8 and 12:

idx = [8; 12];

Here are the neighbor offsets for a four-connected neighborhood:

offsets = [-1; 5; 1; -5];

"Set the pixels that were just labeled to 0".

bw(idx) = 0;

"Find all the nonzero 4-connected neighbors." Do this by adding all neighbor offsets to all pixels in the current set.

neighbors = bsxfun(@plus, idx, offsets')
neighbors =

     7    13     9     3
    11    17    13     7

Remove duplicates.

neighbors = unique(neighbors(:))
neighbors =

     3
     7
     9
    11
    13
    17

Keep only the neighbors that are nonzero.

idx = neighbors(bw(neighbors))
idx =

    13

"Set the corresponding pixels in the label matrix to 1."

L(idx) = 1;

You'd repeat these operations in a loop until idx was empty, indicating that a connected component has been completely labeled.

I've covered two connected components methods in this series so far. In my next post I'll start to explain a third method - the one used by the Image Processing Toolbox functions bwlabel and bwlabeln.

PS. Good luck to everyone running in the Boston Marathon tomorrow!


Get the MATLAB code

Published with MATLAB® 7.4

2 Responses to “Connected component labeling - Part 4”

  1. jxwht replied on :

    Hi,Steve,it has a small mistake,the sentence:
    idx = neighbors(bw(neigbhors))
    should be
    idx = neighbors(bw(neighbors))

  2. Steve replied on :

    Jxwht—Thanks. I fixed the problem.

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.