Steve on Image Processing with MATLAB

Image processing concepts, algorithms, and MATLAB

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.




Published with MATLAB® 7.8

|
  • print

Comments

To leave a comment, please click here to sign in to your MathWorks Account or create a new one.