Steve on Image Processing

October 15th, 2008

Dilation algorithms - performance of decomposed strels

We looked at the decomposition of different structuring element shapes in my previous post on dilation algorithms. Today let's time some code to how decomposition makes a real difference in performance.

Note that running the code in this post requires that you download my timeit function from the MATLAB Central File Exchange.

For our test image, I'll use a grayscale version of the peppers image that ships with the Image Processing Toolbox.

I = rgb2gray(imread('peppers.png'));
imshow(I)

Let's time dilations of I with disk structuring elements of varying radii. As I mentioned previously, strel('disk',r) creates a structuring element that is a decomposable approximation to a disk.

% Dilate using radii of 3 through 25.
rr1 = 3:25;

% Initialize the vector that will hold the measured times.
t1 = [];
for r = rr1
   se = strel('disk', r);

   % Make a function that dilates I by the structuring element se.
   f = @() imdilate(I, se);

   % Measure how long the dilation takes.
   t1(end + 1) = timeit(f);
end

plot(rr1, t1)
grid on
ylabel('Time (seconds)')
xlabel('Disk radius')
% Make sure we show the time scale down to 0.  (Thanks, Chris!)
ylim([0 max(t1)]);
title('Time to compute dilation with decomposable disk')

Now let's repeat the timing experiment using nondecomposable disks. You make such a structuring element using the syntax strel('disk',r,0).

(In my first draft of this post, I tried to do this step using the same range of radius values as above, 3-25. But I got tired of waiting for it to finish!)

% Dilate using radius values 3-8.
rr2 = 3:8;
t2 = [];
for r = rr2
   se = strel('disk', r, 0);
   t2(end + 1) = timeit(@() imdilate(I, se));
end

% Plot the results of both experiments together.
plot(rr, t1, rr2, t2)
grid on
ylabel('Time (seconds)')
xlabel('Disk radius')
% Make sure we show the time scale down to 0.  (Thanks, Chris!)
ylim([0 max([t1, t2])]);
legend('Decomposable', 'Nondecomposable');
title('Speed comparison: Decomposable vs. Nondecomposable disks')

I think you can see from the plot above why we chose to use the decomposable disk approximation as the default!

So what does it look like when you dilate gray peppers with a disk?

J = imdilate(I, strel('disk', 25));
imshow(J)

The resulting blobs look kind of octagony. That's because of the default decomposition used. You can ask strel to use a closer approximation to a disk. It will take a bit longer to compute, although still not nearly as long as using the nondecomposable disk.

se8 = strel('disk', 25, 8);
J8 = imdilate(I, se8);
imshow(J8)

Bon appetit!


Get the MATLAB code

Published with MATLAB® 7.7

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.