Steve on Image Processing

October 23rd, 2006

Nonflat grayscale dilation and erosion

Blog reader Alex asked for an explanation of nonflat grayscale dilation and erosion.

In the most commonly-used form of dilation, the structuring element defines a neighborhood of a pixel. In the output image, a pixel is computed by taking the maximum of all the input pixels in the neighborhood. For erosion, an output image pixel is computed by taking the minimum of all the input pixels in the neighborhood.

In nonflat dilation and erosion, each neighbor has an associated height. A dilation output pixel is computed by first adding the neighbor heights to the neighbor input pixels, and then taking the maximum.

Here's how Digital Image Processing Using MATLAB describes the operation, including the mathematical equation. (This excerpt is from section 9.6.1, pages 366-367.)

"The gray-scale dilation of f by structuring element b [...] is defined as

where D_b is the domain of b, and f(x,y) is assumed equal to [minus infinity] outside the domain of f. This equation implements a process similar to spatial convolution [...]. Conceptually, we can think of rotation the structuring element about its origin and translating it to all locations in the image, just as the convolution kernel is rotated and then translated about the image. At each translated location, the rotated structuring element values are added to the image pixel values and the maximum is computed."

The 'ball' option of the strel function produces a nonflat structuring element.

se = strel('ball',5,5)
 
se =
 
Nonflat STREL object containing 109 neighbors.
Decomposition: 8 STREL objects containing a total of 24 neighbors

Neighborhood:
     0     0     1     1     1     1     1     1     1     0     0
     0     1     1     1     1     1     1     1     1     1     0
     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     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     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     1     1
     0     1     1     1     1     1     1     1     1     1     0
     0     0     1     1     1     1     1     1     1     0     0


Height:
  Columns 1 through 10

      -Inf      -Inf         0    0.6248    1.2497    1.8745    1.2497    0.6248         0      -Inf
      -Inf    0.6248    1.2497    1.8745    2.4994    2.4994    2.4994    1.8745    1.2497    0.6248
         0    1.2497    1.8745    2.4994    3.1242    3.1242    3.1242    2.4994    1.8745    1.2497
    0.6248    1.8745    2.4994    3.1242    3.7491    3.7491    3.7491    3.1242    2.4994    1.8745
    1.2497    2.4994    3.1242    3.7491    4.3739    4.3739    4.3739    3.7491    3.1242    2.4994
    1.8745    2.4994    3.1242    3.7491    4.3739    4.9987    4.3739    3.7491    3.1242    2.4994
    1.2497    2.4994    3.1242    3.7491    4.3739    4.3739    4.3739    3.7491    3.1242    2.4994
    0.6248    1.8745    2.4994    3.1242    3.7491    3.7491    3.7491    3.1242    2.4994    1.8745
         0    1.2497    1.8745    2.4994    3.1242    3.1242    3.1242    2.4994    1.8745    1.2497
      -Inf    0.6248    1.2497    1.8745    2.4994    2.4994    2.4994    1.8745    1.2497    0.6248
      -Inf      -Inf         0    0.6248    1.2497    1.8745    1.2497    0.6248         0      -Inf

  Column 11

      -Inf
      -Inf
         0
    0.6248
    1.2497
    1.8745
    1.2497
    0.6248
         0
      -Inf
      -Inf

 

As far as I know, flat dilation and erosion are far more commonly used. If you know of an image processing application particularly well-suited for applying nonflat dilation and erosion, send me a note - I'd love to hear about it.


Get the MATLAB code

Published with MATLAB® 7.3

17 Responses to “Nonflat grayscale dilation and erosion”

  1. amin replied on :

    Dear steve,
    please can you give me some references available online, about Nonflat grayscale morphological analysis.
    thank you in advance.

  2. Steve replied on :

    Amin - the reference I recommend for morphology is the book by P. Soille, Morphological Image Analysis. Nonflat grayscale dilation and erosion is also covered, briefly, in Digital Image Processing Using MATLAB. As for online references, I have no doubt that Web search engines will lead you to helpful information.

  3. DKS replied on :

    Dear Steve,
    Could you explain why f(x,y) is assumed equal to [minus infinity] outside the domain of f.

    thank you in advance.

  4. Steve replied on :

    DKS - The basic answer is to avoid boundary effects. Specifically, if f(x,y) equals minus infinity outside the image boundaries, then the output of the dilation operation will take on only defined values of f. Perhaps it would have been less surprising if I had said that f(x,y) equals 0 outside the image boundaries. That works fine when image pixels are nonnegative. The key idea is that the assumed value outside the boundary be no greater than any possible image pixel value.

    There is a more detailed treatment of this question in Soille, Morphological Image Analysis: Principles and Applications, 2nd ed. See pages 51, 67, and 73. I’ve made a note to look at this issue again and see whether it’s worth writing more about.

  5. DKS replied on :

    Dear Steve,

    Could you help me on the following?
    What does ( x-x’, y-y’) mean? What happens when x-x’ is negative? in the formula of dilataion

    (f + b)(x,y) = max {f(x-x’,y-y’)+b(x’,y ’)| (x’,y’)belongs to Db}

    Why in dilation it is x-x’ and y-y’, whilst in erosion it is x+x’ and y+y’?

    (f – b)(x,y) = min {f(x+x’,y+y’)+b(x’,y’)| (x’,y’)belongs to Db}

    Please let me know at your earliest convenience. It’s urgent

    Thank you very much.

    DKS

  6. Steve replied on :

    DKS - The notation refers to function f defined on a Cartesian grid, (x,y). (x’,y’) is a point in the domain of the structuring element. For example, if you have a flat, zero-valued structuring element whose domain is -1 < = x' <= 1, -1 <= y' <= 1, then there'll be 9 terms inside the max{} operator: f(x - (-1), y - (-1)), f(x - 0, y - (-1)), ..., f(x - 1, y - 1).

    For any position (x-x’, y-y’) outside the defined domain of f, you have to assume a value for f, or you have to omit those values from the max{} operator. I talked about choosing those “pad” values in my blog last week.

    The choice of equations for dilation and erosion is somewhat a matter of convention. I have the impression that the equations you quoted are more common in the US, whereas a different convention is more often used in Europe. It doesn’t matter that much, except that the choice affects the definition of morphological opening and closing. With the equations you quoted, opening is erosion followed by dilation with the same structuring element. With the European convention, the structuring element has to be reflected for the erosion step.

  7. clcclcclc replied on :

    Recently,I found that some grayscale erosion or dilation using a scaled hemispheric elements.Would you please tell me what the scaled hemispheric elements,and how to use them,thank you very much

  8. Steve replied on :

    clcclcclc—That’s an interesting name that you have. How do you pronounce it?

    I haven’t seen the term scaled hemispheric element before, but I’d guess that it’s the kind of structuring element returned by strel(’ball’, …).

    See the strel doc for more information.

  9. clcclcclc replied on :

    Oh,thank you very much.Recently I saw a paper named
    “FACE DETECTION IN COLOR IMAGES” by Rein-Lien Hsu§, Mohamed Abdel-Mottaleb*, and Anil K. Jain §,in which mentioned that “In our eye detection algorithm, the greyscale dilation and erosion using hemispheric structuring elements at an estimated scale are applied independently to construct EyeMap in the luma”.
    Now I know the algorithm,but I don’t know the structure to be used.
    I am a Chinese student,thank you again for your answer

  10. Christina replied on :

    Hi Steve, I have the following problem: I have a 256*256*20 Volume (MRImage) and I want to use strel to cluster connected elements. It works perfect if I use it in 2-D like

    se = strel(’diamond’,1);
    area = imerode(area,se);

    what I want is to cluster the actual plane with the plane before and the plane behind.

    If I use
    NHOOD=[0 1 0; 1 1 1 ; 0 1 0];
    HEIGHT=[0 0 0 ; 0 1 0; 0 0 0];
    se=strel(NHOOD,HEIGHT);
    Volume2=imerode(Volume,se);
    nothing happens. Where is my mistake?
    Thanks in advance, Christina

  11. Steve replied on :

    Christina—You have used a two-dimensional structuring element neighborhood (NHOOD), so you’re only going to get within-plane effects. Use a three-dimensional one instead. Also, I doubt that you need the HEIGHT argument.

  12. Christina replied on :

    Hi, thanks for the answer, but how should I modify the HEIGHT to make it 3d?
    I have a Volume and I want to ‘filter’ or take out all structures whos values that are higher than a certain threshold and that are connected in the origin/center over 3 slices. Is it correct than, if I use this 3D strel?
    Thanks so much, Christina

  13. Steve replied on :

    Christina—I do not understand why you are using the HEIGHT argument at all. NHOOD controls the shape of the structuring element; make that 3-D. Your threshold value will have to be applied as a separate step; erosion does not do that.

  14. kezia replied on :

    steve,
    how to perform rotation of structuring element by 15 degrees.
    kindly answer my question.
    thank u
    kezia joseph

  15. Steve replied on :

    Kezia—Try imrotate.

  16. Fredric replied on :

    Is there any hemispheric structuring element coding
    in MATLAB?

  17. Steve replied on :

    Fredric—Use the ‘ball’ with strel. You can also construct a structuring element of arbitrary shape by specifying the domain (nhood) and height values yourself.

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.