Steve on Image Processing

September 17th, 2008

Dilation algorithms—structuring element decomposition

This is the second post in my series on algorithm concepts behind the implementation of dilation and erosion in the Image Processing Toolbox. Today I want to talk about structuring element decomposition.

Some useful structuring elements can be represented as the dilation of two or more smaller structuring elements. Dilation implementations can exploit this decomposition of structuring elements into smaller structuring elements by taking advantage of the associativity property of dilation.

That is, suppose image g is the dilation of image f with structuring element b:

where the star operator means dilation. And further suppose that b is the dilation of two other structuring elements:

Then the original image dilation equation can be rewritten as:

In English, the equation above says we can dilate f with structuring element b by:

1. Dilating f first with structuring element b1.

2. Dilating the result of step 1 with structuring element b2.

Let's see how this might help make dilation faster. One of the most commonly-used structuring element shapes is the square. For example, here's a simple 5-by-5 array of 1s that can define a structuring element domain (or neighborhood):

b = ones(5, 5)
b =

     1     1     1     1     1
     1     1     1     1     1
     1     1     1     1     1
     1     1     1     1     1
     1     1     1     1     1

It turns out that a square (or rectangular) structuring element can be decomposed into two structuring elements: a horizontal line, and a vertical line.

b1 = ones(1, 5)
b1 =

     1     1     1     1     1

b2 = ones(5, 1)
b2 =

     1
     1
     1
     1
     1

You can check that the dilation of b1 and b2 equals b by using the 'full' option of imdilate:

imdilate(b1, b2, 'full')
ans =

     1     1     1     1     1
     1     1     1     1     1
     1     1     1     1     1
     1     1     1     1     1
     1     1     1     1     1

How does this help us? Well, as I discussed last time, computing dilation directly from its defining equation requires Q comparisons per pixel, where Q is the number of elements in the structuring element domain. The 5-by-5 square structuring element has 25 elements in its domain. But structuring elements b1 and b2 have only five elements each. So you could dilate by one and then again by the other and require only 40% of the comparisons.

Let's look at how structuring element decompositions are computed and represented in the Image Processing Toolbox. The function strel makes a structuring element object. You give strel the name of a shape, such as 'rectangle', plus any parameters required for the shape. When you use strel to make a rectangular structuring element, the resulting object displays information about itself, including the decomposition:

se = strel('rectangle', [5 5])
 
se =
 
Flat STREL object containing 25 neighbors.
Decomposition: 2 STREL objects containing a total of 10 neighbors

Neighborhood:
     1     1     1     1     1
     1     1     1     1     1
     1     1     1     1     1
     1     1     1     1     1
     1     1     1     1     1

 

You can call the function getsequence (actually a method of the strel class) to get the structuring elements in the decomposition:

decomp = getsequence(se)
 
decomp =
 
2x1 array of STREL objects
 

You use ordinary MATLAB array indexing to get each of the structuring elements from the decomposition:

decomp(1)
 
ans =
 
Flat STREL object containing 5 neighbors.

Neighborhood:
     1
     1
     1
     1
     1

 
decomp(2)
 
ans =
 
Flat STREL object containing 5 neighbors.

Neighborhood:
     1     1     1     1     1

 

Ordinarily, of course, strel and imdilate together handle all of these details for you when you perform image dilation.

Next time I plan to look at some of the other shapes offered by strel and see how they get decomposed.


Get the MATLAB code

Published with MATLAB® 7.6

4 Responses to “Dilation algorithms—structuring element decomposition”

  1. lala replied on :

    Hi Steve,

    I’m searching for algorithm concepts behind the implementation of erosion and closing in the Image Processing Toolbox in Matlab. Looks like you haven’t update on this yet. Can you please give me a brief detail on this? Thank you very much.

    ~Lala

  2. Steve replied on :

    Lala—Dilation and erosion are dual operators. There is relatively little difference between them in terms of algorithms and implementation. Everything I’ve written about dilation applies to erosion, so I have no plans to write anything further that’s specific to erosion. Closing is just dilation followed by erosion.

  3. Anne replied on :

    Hi Steve,

    It seems to me that the time savings you get by decomposing the structuring element depend a bit on the initial volume. In the case of a mostly empty volume with only a small percentage of non-zero voxels, I think decomposition could actually cost more time! For the limiting case, say we have a 3D volume with only 1 nonzero voxel which we are dilating with a 5×5x5 structuring element. In the case of no decomposition, 125 comparison are made. However, if you decompose the structuring element into 3 linear elements, you have to do 1*5 (first step) + 5*5 (second step) + 25*5 (third step) = 155 comparisons. Is my logic correct here?

    Thanks,
    Anne

  4. Steve replied on :

    Anne—That’s a very good point. The performance of implementations of binary dilation and erosion can be data-dependent. When we do performance benchmarking of binary dilation, we usually use a couple of images: one with just a few foreground pixels and one with many foreground pixels. It’s difficult to write a binary dilation routine that performs equally well for all kinds of input images.

    For grayscale dilation and erosion, though, the performance is not data dependent and structuring element decomposition is a clear win.

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.