{"id":86,"date":"2006-09-25T13:28:21","date_gmt":"2006-09-25T17:28:21","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/?p=86"},"modified":"2019-10-22T16:27:26","modified_gmt":"2019-10-22T20:27:26","slug":"dilation-erosion-and-the-morphological-gradient","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2006\/09\/25\/dilation-erosion-and-the-morphological-gradient\/","title":{"rendered":"Dilation, erosion, and the morphological gradient"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>The morphological operator dilation acts like a local maximum operator. Erosion acts like a local minimum operator.  You can\r\n         use them together to compute something called the <i>morphological gradient<\/i>.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">Dilation<\/a><\/li>\r\n         <li><a href=\"#7\">Erosion<\/a><\/li>\r\n         <li><a href=\"#9\">Morphological gradient<\/a><\/li>\r\n         <li><a href=\"#12\">Direction gradients<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Dilation<a name=\"1\"><\/a><\/h3>\r\n   <p>The basic form of grayscale image dilation computes, for each image pixel, the maximum value of its neighboring pixels.  The\r\n      neighborhood is defined by the <i>structuring element<\/i>.  For example, this structuring element:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">se1 = strel([1 1 1])<\/pre><pre style=\"font-style:oblique\"> \r\nse1 =\r\n \r\nFlat STREL object containing 3 neighbors.\r\n\r\nNeighborhood:\r\n     1     1     1\r\n\r\n \r\n<\/pre><p>defines a neighborhood consisting of the pixel itself, together with its left and right neighbors.  Whereas this structuring\r\n      element:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">se2 = strel([1; 1; 1])<\/pre><pre style=\"font-style:oblique\"> \r\nse2 =\r\n \r\nFlat STREL object containing 3 neighbors.\r\n\r\nNeighborhood:\r\n     1\r\n     1\r\n     1\r\n\r\n \r\n<\/pre><p>defines a neighborhood consisting of the pixel itself, together with the pixels above and below it.<\/p>\r\n   <p>Let's try dilation on everyone's favorite sample MATLAB matrix, the magic square:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">m5 = magic(5)<\/pre><pre style=\"font-style:oblique\">\r\nm5 =\r\n\r\n    17    24     1     8    15\r\n    23     5     7    14    16\r\n     4     6    13    20    22\r\n    10    12    19    21     3\r\n    11    18    25     2     9\r\n\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">d1 = imdilate(m5, se1)<\/pre><pre style=\"font-style:oblique\">\r\nd1 =\r\n\r\n    24    24    24    15    15\r\n    23    23    14    16    16\r\n     6    13    20    22    22\r\n    12    19    21    21    21\r\n    18    25    25    25     9\r\n\r\n<\/pre><p><tt>d1(3,3)<\/tt>, which is 20, is the maximum of <tt>[m5(3,2), m5(3,3), m5(3,4)]<\/tt>, or <tt>[6 13 20]<\/tt>.\r\n   <\/p>\r\n   <p>Dilating with <tt>se2<\/tt> computes the 3-pixel local maximum vertically:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">d2 = imdilate(m5, se2)<\/pre><pre style=\"font-style:oblique\">\r\nd2 =\r\n\r\n    23    24     7    14    16\r\n    23    24    13    20    22\r\n    23    12    19    21    22\r\n    11    18    25    21    22\r\n    11    18    25    21     9\r\n\r\n<\/pre><p><tt>d2(3,3)<\/tt> is the maximum of <tt>[m5(2,3), m5(3,3), m5(4,3)]<\/tt>.\r\n   <\/p>\r\n   <h3>Erosion<a name=\"7\"><\/a><\/h3>\r\n   <p>Grayscale image erosion computes the minimum of each pixel's neighborhood.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">e1 = imerode(m5, se1)<\/pre><pre style=\"font-style:oblique\">\r\ne1 =\r\n\r\n    17     1     1     1     8\r\n     5     5     5     7    14\r\n     4     4     6    13    20\r\n    10    10    12     3     3\r\n    11    11     2     2     2\r\n\r\n<\/pre><p><tt>e1(3,3)<\/tt> is the minimum of <tt>[m5(3,2), m5(3,3), m5(3,4)]<\/tt>.\r\n   <\/p>\r\n   <h3>Morphological gradient<a name=\"9\"><\/a><\/h3>\r\n   <p>Dilation and erosion are often used in combination to produce a desired image processing effect.  One simple combination is\r\n      the <i>morphological gradient<\/i>.  P. Soille, in section 3.8 of the second edition of <i>Morphological Image Analysis: Principles and Applications<\/i>, talks about three kinds of basic morphological gradients:\r\n   <\/p>\r\n   <div>\r\n      <ul>\r\n         <li>dilated_image - eroded_image<\/li>\r\n         <li>original_image - eroded_image<\/li>\r\n         <li>dilated_image - original_image<\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <p>Soille calls the first one the basic morphological gradient, and you compute it this way using MATLAB and the Image Processing\r\n      Toolbox:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #228B22\">% Read in circuit board image, crop it so it's not too big for the blog<\/span>\r\n<span style=\"color: #228B22\">% page, and convert it to grayscale:<\/span>\r\n\r\nrgb = imread(<span style=\"color: #A020F0\">'board.tif'<\/span>);\r\nI = rgb2gray(rgb(1:256,1:256,:));\r\n\r\nse = strel(ones(3,3));\r\nbasic_gradient = imdilate(I, se) - imerode(I, se);\r\n\r\nsubplot(1,2,1), imshow(I), imcredit(<span style=\"color: #A020F0\">'Image courtesy of Alexander V. Panasyuk'<\/span>)\r\n\r\nsubplot(1,2,2), imshow(basic_gradient, [])<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/86\/morpho_gradient_01.jpg\"> <p>The second form is called the <i>half-gradient by erosion<\/i> or <i>internal gradient<\/i>.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">internal_gradient = I - imerode(I, se);\r\n\r\nsubplot(1,2,2), imshow(internal_gradient, [])<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/86\/morpho_gradient_02.jpg\"> <p>\"The internal gradient enhances internal boundaries of objects brighter than their background and external boundaries of objects\r\n      darker than their background. For binary images, the internal gradient generates a mask of the internal boundaries of the\r\n      foreground image objects.\" [Soille, page 86]\r\n   <\/p>\r\n   <p>The third form is called the <i>half-gradient by dilation<\/i> or <i>external gradient<\/i>:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">external_gradient = imdilate(I, se) - I;<\/pre><h3>Direction gradients<a name=\"12\"><\/a><\/h3>\r\n   <p>By using line segments as structuring elements, you can compute directional gradients.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">seh = strel([1 1 1]);\r\nsev = strel([1;1;1]);\r\n\r\nhorizontal_gradient = imdilate(I,seh) - imerode(I,seh);\r\nvertical_gradient = imdilate(I,sev) - imerode(I,sev);\r\n\r\nsubplot(1,2,1)\r\nimshow(horizontal_gradient, []), title(<span style=\"color: #A020F0\">'Horizontal gradient'<\/span>)\r\n\r\nsubplot(1,2,2)\r\nimshow(vertical_gradient, []), title(<span style=\"color: #A020F0\">'Vertical gradient'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/86\/morpho_gradient_03.jpg\"> <p>Try this on your own grayscale images.  You can experiment with larger structuring elements, too, such as <tt>strel('disk',7)<\/tt>.\r\n   <\/p>\r\n   <p>I'll be covering ways to combine erosion, dilation, and other fundamental morphological operators in the near future.<\/p>\r\n\r\n <script language=\"JavaScript\"> \r\n<!--\r\n    function grabCode_86() {\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='86 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 86';\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_86()\"><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\n86 ##### SOURCE BEGIN #####\r\n%% Dilation, erosion, and the morphological gradient\r\n% The morphological operator dilation acts like a local maximum operator.\r\n% Erosion acts like a local minimum operator.  You can use them together to\r\n% compute something called the _morphological gradient_.\r\n%\r\n%% Dilation\r\n% The basic form of grayscale image dilation computes, for each image\r\n% pixel, the maximum value of its neighboring pixels.  The neighborhood is\r\n% defined by the _structuring element_.  For example, this structuring \r\n% element:\r\n\r\nse1 = strel([1 1 1])\r\n\r\n%%\r\n% defines a neighborhood consisting of the pixel itself, together with its\r\n% left and right neighbors.  Whereas this structuring element:\r\n\r\nse2 = strel([1; 1; 1])\r\n\r\n%%\r\n% defines a neighborhood consisting of the pixel itself, together with the\r\n% pixels above and below it.\r\n%\r\n% Let's try dilation on everyone's favorite sample MATLAB matrix, the magic\r\n% square:\r\n\r\nm5 = magic(5)\r\n\r\n%%\r\n\r\nd1 = imdilate(m5, se1)\r\n\r\n%%\r\n% |d1(3,3)|, which is 20, is the maximum of |[m5(3,2), m5(3,3), m5(3,4)]|, or\r\n% |[6 13 20]|.\r\n%\r\n% Dilating with |se2| computes the 3-pixel local maximum vertically:\r\n\r\nd2 = imdilate(m5, se2)\r\n\r\n%%\r\n% |d2(3,3)| is the maximum of |[m5(2,3), m5(3,3), m5(4,3)]|.\r\n\r\n%% Erosion\r\n% Grayscale image erosion computes the minimum of each pixel's\r\n% neighborhood.\r\n\r\ne1 = imerode(m5, se1)\r\n\r\n%%\r\n% |e1(3,3)| is the minimum of |[m5(3,2), m5(3,3), m5(3,4)]|.\r\n\r\n%% Morphological gradient\r\n% Dilation and erosion are often used in combination to produce a desired\r\n% image processing effect.  One simple combination is the _morphological\r\n% gradient_.  P. Soille, in section 3.8 of the second edition of\r\n% _Morphological Image Analysis: Principles and Applications_, talks about\r\n% three kinds of basic morphological gradients:\r\n%\r\n% * dilated_image - eroded_image\r\n% * original_image - eroded_image\r\n% * dilated_image - original_image\r\n%\r\n% Soille calls the first one the basic morphological gradient, and you\r\n% compute it this way using MATLAB and the Image Processing Toolbox:\r\n\r\n% Read in circuit board image, crop it so it's not too big for the blog\r\n% page, and convert it to grayscale:\r\n\r\nrgb = imread('board.tif');\r\nI = rgb2gray(rgb(1:256,1:256,:));\r\n\r\nse = strel(ones(3,3));\r\nbasic_gradient = imdilate(I, se) - imerode(I, se);\r\n\r\nsubplot(1,2,1), imshow(I), imcredit('Image courtesy of Alexander V. Panasyuk')\r\n\r\nsubplot(1,2,2), imshow(basic_gradient, [])\r\n\r\n%%\r\n% The second form is called the _half-gradient by erosion_ or _internal\r\n% gradient_.\r\n\r\ninternal_gradient = I - imerode(I, se);\r\n\r\nsubplot(1,2,2), imshow(internal_gradient, [])\r\n\r\n%%\r\n% \"The internal gradient enhances internal boundaries of objects brighter\r\n% than their background and external boundaries of objects darker than\r\n% their background. For binary images, the internal gradient generates a\r\n% mask of the internal boundaries of the foreground image objects.\"\r\n% [Soille, page 86]\r\n%\r\n% The third form is called the _half-gradient by dilation_ or _external\r\n% gradient_:\r\n\r\nexternal_gradient = imdilate(I, se) - I;\r\n\r\n%% Direction gradients\r\n% By using line segments as structuring elements, you can compute\r\n% directional gradients.\r\n\r\nseh = strel([1 1 1]);\r\nsev = strel([1;1;1]);\r\n\r\nhorizontal_gradient = imdilate(I,seh) - imerode(I,seh);\r\nvertical_gradient = imdilate(I,sev) - imerode(I,sev);\r\n\r\nsubplot(1,2,1)\r\nimshow(horizontal_gradient, []), title('Horizontal gradient')\r\n\r\nsubplot(1,2,2)\r\nimshow(vertical_gradient, []), title('Vertical gradient')\r\n\r\n%%\r\n% Try this on your own grayscale images.  You can experiment with larger\r\n% structuring elements, too, such as |strel('disk',7)|.\r\n%\r\n% I'll be covering ways to combine erosion, dilation, and other fundamental\r\n% morphological operators in the near future.\r\n\r\n##### SOURCE END ##### 86\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      The morphological operator dilation acts like a local maximum operator. Erosion acts like a local minimum operator.  You can\r\n         use them together to compute something called the... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2006\/09\/25\/dilation-erosion-and-the-morphological-gradient\/\">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":[124,246,76,36,54,104,106,72,52],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/86"}],"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=86"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/86\/revisions"}],"predecessor-version":[{"id":3504,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/86\/revisions\/3504"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=86"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=86"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=86"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}