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.

  • murat: Hi Steve, I have an rgb image of a kind of cream and it contains some small black particles (black dots). In...
  • Steve: Ernest—Look at setting the FaceColor property. The code for setting that is shown on the page you asked...
  • Ernest Miller: Hi Steve, Understood. However, can you explain how to change the colors? Thanks, Ernest
  • Jan: Hi Steve Very useful code, yet what if I parts of my rotated+translated object are outside the original...
  • Steve: MoHDa—It might be possible. You’ll need to use one of the options that produces closed edge...
  • MoHDa: I have one question about the ROIPOLY: I have an image with stripes, I use the “edge” command for...
  • Steve: Shahn—My November 17, 2006 post shows you how to do it.
  • Steve: Kay-Uwe—Thanks for following up. I am planning to make it easier to use test directories in a package....
  • shahn: Hello Steve Instead of superimposing a star on the image to show the centroide. How would you superimpose a...
  • Kay-Uwe: Having TestSuite.fromPackag e() would be nice to have, but so far using simple “test” subdirs...

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