{"id":91,"date":"2006-10-23T07:00:45","date_gmt":"2006-10-23T11:00:45","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/?p=91"},"modified":"2019-10-22T16:30:38","modified_gmt":"2019-10-22T20:30:38","slug":"nonflat-grayscale-dilation-and-erosion","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2006\/10\/23\/nonflat-grayscale-dilation-and-erosion\/","title":{"rendered":"Nonflat grayscale dilation and erosion"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p>Blog reader Alex asked for an explanation of nonflat grayscale dilation and erosion.<\/p>\r\n   <p>In the most commonly-used form of dilation, the structuring element defines a neighborhood of a pixel. In the output image,\r\n      a pixel is computed by taking the maximum of all the input pixels in the neighborhood.  For erosion, an output image pixel\r\n      is computed by taking the minimum of all the input pixels in the neighborhood.\r\n   <\/p>\r\n   <p>In nonflat dilation and erosion, each neighbor has an associated height. A dilation output pixel is computed by first adding\r\n      the neighbor heights to the neighbor input pixels, and then taking the maximum.\r\n   <\/p>\r\n   <p>Here's how <i>Digital Image Processing Using MATLAB<\/i> describes the operation, including the mathematical equation.  (This excerpt is from section 9.6.1, pages 366-367.)\r\n   <\/p>\r\n   <p>\"The <i>gray-scale dilation<\/i> of <i>f<\/i> by structuring element <i>b<\/i> [...] is defined as\r\n   <\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/91\/nonflat_dilation_eq176107.png\"> <\/p>\r\n   <p>where <i>D_b<\/i> is the domain of <i>b<\/i>, and <i>f(x,y)<\/i> is assumed equal to [minus infinity] outside the domain of <i>f<\/i>. This equation implements a process similar to spatial convolution [...]. Conceptually, we can think of rotation the structuring\r\n      element about its origin and translating it to all locations in the image, just as the convolution kernel is rotated and then\r\n      translated about the image. At each translated location, the rotated structuring element values are added to the image pixel\r\n      values and the maximum is computed.\"\r\n   <\/p>\r\n   <p>The <tt>'ball'<\/tt> option of the <tt>strel<\/tt> function produces a nonflat structuring element.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">se = strel(<span style=\"color: #A020F0\">'ball'<\/span>,5,5)<\/pre><pre style=\"font-style:oblique\"> \r\nse =\r\n \r\nNonflat STREL object containing 109 neighbors.\r\nDecomposition: 8 STREL objects containing a total of 24 neighbors\r\n\r\nNeighborhood:\r\n     0     0     1     1     1     1     1     1     1     0     0\r\n     0     1     1     1     1     1     1     1     1     1     0\r\n     1     1     1     1     1     1     1     1     1     1     1\r\n     1     1     1     1     1     1     1     1     1     1     1\r\n     1     1     1     1     1     1     1     1     1     1     1\r\n     1     1     1     1     1     1     1     1     1     1     1\r\n     1     1     1     1     1     1     1     1     1     1     1\r\n     1     1     1     1     1     1     1     1     1     1     1\r\n     1     1     1     1     1     1     1     1     1     1     1\r\n     0     1     1     1     1     1     1     1     1     1     0\r\n     0     0     1     1     1     1     1     1     1     0     0\r\n\r\n\r\nHeight:\r\n  Columns 1 through 10\r\n\r\n      -Inf      -Inf         0    0.6248    1.2497    1.8745    1.2497    0.6248         0      -Inf\r\n      -Inf    0.6248    1.2497    1.8745    2.4994    2.4994    2.4994    1.8745    1.2497    0.6248\r\n         0    1.2497    1.8745    2.4994    3.1242    3.1242    3.1242    2.4994    1.8745    1.2497\r\n    0.6248    1.8745    2.4994    3.1242    3.7491    3.7491    3.7491    3.1242    2.4994    1.8745\r\n    1.2497    2.4994    3.1242    3.7491    4.3739    4.3739    4.3739    3.7491    3.1242    2.4994\r\n    1.8745    2.4994    3.1242    3.7491    4.3739    4.9987    4.3739    3.7491    3.1242    2.4994\r\n    1.2497    2.4994    3.1242    3.7491    4.3739    4.3739    4.3739    3.7491    3.1242    2.4994\r\n    0.6248    1.8745    2.4994    3.1242    3.7491    3.7491    3.7491    3.1242    2.4994    1.8745\r\n         0    1.2497    1.8745    2.4994    3.1242    3.1242    3.1242    2.4994    1.8745    1.2497\r\n      -Inf    0.6248    1.2497    1.8745    2.4994    2.4994    2.4994    1.8745    1.2497    0.6248\r\n      -Inf      -Inf         0    0.6248    1.2497    1.8745    1.2497    0.6248         0      -Inf\r\n\r\n  Column 11\r\n\r\n      -Inf\r\n      -Inf\r\n         0\r\n    0.6248\r\n    1.2497\r\n    1.8745\r\n    1.2497\r\n    0.6248\r\n         0\r\n      -Inf\r\n      -Inf\r\n\r\n \r\n<\/pre><p>As far as I know, flat dilation and erosion are far more commonly used. If you know of an image processing application particularly\r\n      well-suited for applying nonflat dilation and erosion, send me a note - I'd love to hear about it.\r\n   <\/p><script language=\"JavaScript\"> \r\n<!--\r\n    function grabCode_3d5bb47969be45fa9b87d428a71ca18d() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='3d5bb47969be45fa9b87d428a71ca18d ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 3d5bb47969be45fa9b87d428a71ca18d';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'Steve Eddins';\r\n        copyright = 'Copyright 2006 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n-->\r\n      <\/script>\r\n<noscript>\r\n<em>A JavaScript-enabled browser is required to use the \"Get the MATLAB code\" link.<\/em>\r\n<\/noscript>\r\n<p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_3d5bb47969be45fa9b87d428a71ca18d()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code<\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.3<br><\/p>\r\n<\/div>\r\n<!--\r\n3d5bb47969be45fa9b87d428a71ca18d ##### SOURCE BEGIN #####\r\n%% Nonflat dilation and erosion\r\n% Blog reader Alex asked for an explanation of nonflat grayscale dilation \r\n% and erosion.\r\n%\r\n% In the most commonly-used form of dilation, the structuring element\r\n% defines a neighborhood of a pixel. In the output image, a pixel is\r\n% computed by taking the maximum of all the input pixels in the\r\n% neighborhood.  For erosion, an output image pixel is computed by taking\r\n% the minimum of all the input pixels in the neighborhood.\r\n%\r\n% In nonflat dilation and erosion, each neighbor has an associated height.\r\n% A dilation output pixel is computed by first adding the neighbor heights\r\n% to the neighbor input pixels, and then taking the maximum.\r\n%\r\n% Here's how _Digital Image Processing Using MATLAB_ describes the\r\n% operation, including the mathematical equation.  (This excerpt is from\r\n% section 9.6.1, pages 366-367.)\r\n%\r\n% \"The _gray-scale dilation_ of _f_ by structuring element _b_ [...] is\r\n% defined as\r\n%\r\n% $$(f\\oplus b)(x,y) = \\max\\{f(x - x', y - y') + b(x',y') | (x', y') \\in D_b\\} $$\r\n%\r\n% where _D_b_ is the domain of _b_, and _f(x,y)_ is assumed equal to [minus\r\n% infinity] outside the domain of _f_. This equation implements a process\r\n% similar to spatial convolution [...]. Conceptually, we can think of\r\n% rotation the structuring element about its origin and translating it to\r\n% all locations in the image, just as the convolution kernel is rotated and\r\n% then translated about the image. At each translated location, the rotated\r\n% structuring element values are added to the image pixel values and the\r\n% maximum is computed.\"\r\n%\r\n% The |'ball'| option of the |strel| function produces a nonflat\r\n% structuring element.\r\n\r\nse = strel('ball',5,5)\r\n\r\n%%\r\n% As far as I know, flat dilation and erosion are far more commonly used.\r\n% If you know of an image processing application particularly well-suited\r\n% for applying nonflat dilation and erosion, send me a note - I'd love to \r\n% hear about it.\r\n\r\n##### SOURCE END ##### 3d5bb47969be45fa9b87d428a71ca18d\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   Blog reader Alex asked for an explanation of nonflat grayscale dilation and erosion.\r\n   In the most commonly-used form of dilation, the structuring element defines a neighborhood of a pixel. In... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2006\/10\/23\/nonflat-grayscale-dilation-and-erosion\/\">read more >><\/a><\/p>","protected":false},"author":42,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[106],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/91"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/users\/42"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/comments?post=91"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/91\/revisions"}],"predecessor-version":[{"id":3512,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/91\/revisions\/3512"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=91"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=91"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=91"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}