Steve on Image Processing

March 13th, 2007

Connected component labeling - Part 2

In this series I'm discussing different ways to compute the connected components of a binary image. Before I get into specific algorithms, though, I need to touch briefly on the notion of connectivity.

For a given pixel p, what is the set of neighbors of p? In other words, what is p's neighborhood? There's no single answer that works best for all applications. One definition is that the neighbors of p are the pixels that share an edge with p. There are four such neighbors:

  1
4 p 2
  3

This is called a four-connected neighborhood.

Another common neighborhood definition is the set of pixels that share an edge or a corner with p. There are eight:

8 1 2
7 p 3
6 5 4

Here's a binary image that demonstrates the practical difference between these neighborhood definitions:

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

showPixelValues(bw)

If we use a four-connected neighborhood definition, then the image above has two connected components: an upper-left 2-by-2 component, and a lower-right 2-by-3 component. With an eight-connected neighborhood definition, there's just one connected component.

Many Image Processing Toolbox functions support other kinds of neighborhood definitions as well, via an optional input argument called CONN (for connectivity). For two-dimensional processing, CONN is a 3-by-3 matrix of 0s and 1s. The matrix has to be symmetric about its central element. The 1s define the neighbors. For example, here are the CONN matrices for four-connected and eight-connected neighborhoods:

conn4 = [0 1 0; 1 1 1; 0 1 0]
conn4 =

     0     1     0
     1     1     1
     0     1     0

conn8 = [1 1 1; 1 1 1; 1 1 1]
conn8 =

     1     1     1
     1     1     1
     1     1     1

In my next post in this series, I'll talk about representing the collection of neighbor relationships among foreground pixels as a graph.


Get the MATLAB code

Published with MATLAB® 7.4

5 Responses to “Connected component labeling - Part 2”

  1. karthick replied on :

    Steve, Please give us some insight in to resolving equivalences using the union find algorithm/ some detail on the equivalence table structure.
    Thanks

  2. Steve replied on :

    Karthick - yes, I plan to give more information on that topic later in this series of postings yet. It’s not written yet. :-)

  3. Raymond replied on :

    Steve, your blog is great and I would like to browse old postings, but the uncategorized link only goes back so far. How do I access older entries?

  4. Steve replied on :

    Raymond - click on the “Archives” link in the sidebar, just above “Categories.”

  5. karthick replied on :

    Steve

    Looking forward for it. Thanks

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.