Steve on Image Processing

July 6th, 2009

A new look for connected component labeling in R2009a

One of the changes we made to the Image Processing Toolbox for R2009a was partially inspired by comments received on this blog. Specifically, several blog readers complained about the memory required by the function bwlabel.

The function bwlabel labels connected components (or objects, or blobs) in a binary image. (I wrote a series of posts a couple of years ago about connected component labeling algorithms.)

I'll illustrate what bwlabel does using a very small binary image.

BW = [1     1     1     0     0     0     0     0
    1     1     1     0     1     1     0     0
    1     1     1     0     1     1     0     0
    0     0     0     0     0     0     1     0
    0     0     0     0     0     0     1     0
    0     0     0     0     0     0     1     0
    0     1     1     0     0     1     1     0
    0     1     1     0     0     0     0     0]
BW =

     1     1     1     0     0     0     0     0
     1     1     1     0     1     1     0     0
     1     1     1     0     1     1     0     0
     0     0     0     0     0     0     1     0
     0     0     0     0     0     0     1     0
     0     0     0     0     0     0     1     0
     0     1     1     0     0     1     1     0
     0     1     1     0     0     0     0     0

L = bwlabel(BW)
L =

     1     1     1     0     0     0     0     0
     1     1     1     0     3     3     0     0
     1     1     1     0     3     3     0     0
     0     0     0     0     0     0     3     0
     0     0     0     0     0     0     3     0
     0     0     0     0     0     0     3     0
     0     2     2     0     0     3     3     0
     0     2     2     0     0     0     0     0

The output, L, of bwlabel is called a label matrix. A label matrix contains nonnegative integer values. 0s correspond to background pixels in the binary image, and positive values identify each labeled object. For example, the elements of L that equal 2 correspond to the second object found by bwlabel.

L == 2
ans =

     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0
     0     1     1     0     0     0     0     0
     0     1     1     0     0     0     0     0

Very often, the output of bwlabel is used as the input to regionprops, a function that measures geometric properties of regions. For example, we can compute the area of each labeled object:

s = regionprops(L, 'Area')
s = 

3x1 struct array with fields:
    Area

[s.Area]   % comma-separated list syntax for structures
ans =

     9     4     9

As I mentioned in the first paragraph above, we occasionally hear complaints about the amount of memory required by bwlabel. Because bwlabel always returns L as a double-precision matrix, the memory required is 8 bytes per image pixel.

Why is L double-precision? Because bwlabel was introduced to the Image Processing Toolbox at a time when support for integer matrices was just getting off the ground. In 1998 or so, the only non-double data type supported by the Image Processing Toolbox was uint8. It did not seem reasonable to limit label matrices to 255 objects, and we were uncomfortable with a design that called for the data type of L to vary according to the number of objects detected.

If there had been more than minimal support at the time for uint32 or single matrices, we might have chosen one of those types. But there wasn't, so we went with double, and that's what it's been ever since.

The memory complaints have persisted, though, particularly with users who are doing multidimensional image processing. 8 bytes per image pixel seemed to be a high price to pay, especially when there were relatively few foreground pixels.

The usual request was to change the data type L to be uint8 when there were only a few objects. We thought about doing that, but there were other issues we wanted to fix:

1. The memory requirement was proportional to the total number of image pixels instead of the number of object pixels. This would still be the case even we used uint8 in some cases.

2. We knew that an early step inside the implementation of regionprops was to compute a list of all the pixel indices corresponding to each object. This information is known inside the bwlabel computation, but it gets lost the label matrix doesn't represent it directly. Recomputing the pixel indices causes a speed penalty.

3. We knew that most users didn't use the label matrix directly. Often, the only reason for computing it was to hand it off to regionprops.

So we looked for a new design that would address all these issues and that would not cause compatibility problems.

Next time I'll show you what we did.


Get the MATLAB code

Published with MATLAB® 7.8

2 Responses to “A new look for connected component labeling in R2009a”

  1. Ahmed Mostafa replied on :

    Hello Steve
    i really like your articles, it helped me a lot through my study. i kinda have a problem with bwconncomp and isosurface, it will be great if you can have time to look at it

    http://www.mathworks.com/matlabcentral/newsreader/view_thread/258281#672407

    Thanks

  2. Steve replied on :

    Ahmed—You need to break down your code a line at a time, looking carefully at each output to see if it matches your expectations. For example, what is the number of elements in the output of bwconncomp? That will tell you how many objects it detected.

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.