{"id":1321,"date":"2015-05-06T16:51:59","date_gmt":"2015-05-06T20:51:59","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/?p=1321"},"modified":"2015-05-06T16:51:59","modified_gmt":"2015-05-06T20:51:59","slug":"gray-scale-dilation-equation","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2015\/05\/06\/gray-scale-dilation-equation\/","title":{"rendered":"Gray-scale dilation equation"},"content":{"rendered":"<div class=\"content\"><p>A question came into tech support a month or so ago regarding the <a href=\"https:\/\/www.mathworks.com\/help\/images\/ref\/imdilate.html\">documentation for <tt>imdilate<\/tt><\/a>. The question concerned an apparent discrepancy in the equations for binary and grayscale dilation. Here's the formula given for binary dilation:<\/p><p>$$A \\oplus B = \\{ z | (\\hat{B})_z \\cap A \\neq 0 \\}$$<\/p><p>where $\\hat{B}$ is the reflection of the structuring element $B$. The reference page goes on to say, \"in other words, it is the set of pixel locations $z$ where the reflected structuring element overlaps with foreground pixels of $A$ when translated to $z$.\"<\/p><p>The reference page then gives another equation for gray-scale dilation.<\/p><p>$$(A \\oplus B)(x,y) = \\max \\{ A(x - x', y - y') + B(x',y') | (x',y') \\in D_B\\}$$<\/p><p>where $D_B$ is the domain of the structuring element and $A(x,y)$ is assumed to be $-\\infty$ outside the domain of the image.<\/p><p>The user who contacted tech support wondered if there might be an error in the equation for gray-scale dilation because the equation doesn't show a reflection of the structuring element. The case was escalated to me for comment.<\/p><p>The gray-scale dilation equation above is correct. (Well, it's correct for about half the world. The other half uses a slightly different form.)<\/p><p>But there are several different but equivalent mathematical equations that can be used to define dilation. Each of these equations corresponds to a different but equivalent geometric interpretation. The equation above can be interpreted as follows:<\/p><p>To compute the output at $(x,y)$, flip (or reflect) $A$ through the origin and then slide the origin pixel over to $(x,y)$. Form the sums of the $A$ pixels with the structuring element heights underneath. Find the maximum of these sums and record the result as the output at $(x,y)$.<\/p><p>As it turns out, dilation is commutative. That suggests that there is a form of the equation, and a corresponding geometric interpretation, in which the structuring element is reflected instead of the image. We can take the first step in that direction via a substitution of variables. Let $q = x - x'$ and $r = y - y'$. Then:<\/p><p>$$(A \\oplus B)(x,y) = \\max \\{ A(q,r) + B(x-q, y-r) | (x-q, y-r) \\in D_B \\}$$<\/p><p>Since $q$ and $r$ are \"dummy\" variables, we can rewrite them as $x'$ and $y'$.<\/p><p>$$(A \\oplus B)(x,y) = \\max \\{ A(x',y') + B(x-x',y-y') | (x-x',y-y') \\in D_B \\}$$<\/p><p>This second equation has the geometric interpretation of leaving the image in placing, flipping (reflecting) and sliding the structuring element, performing sums of the corresponding image pixels and structuring element heights, and then taking the maximum of the sums.<\/p><p>So which equation should we use? Well, both are correct. Which form to use for a real implementation is completely up to the implementer. And these are not the only two equations and geometric interpretations that are valid.<\/p><p>I do think, though, that there is some merit in modifying the gray-scale dilation equation in our documentation to make it more consistent with the form used for binary dilation.<\/p><p><b>Dear reader, I am curious: do you use nonflat grayscale dilation or erosion in your work?<\/b> As far as I can tell, there are not many practical applications for using nonflat structuring elements. If you have a use for it, please leave me a comment below.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_603fb18e7b1a4043b097f55ffd2ebed5() {\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='603fb18e7b1a4043b097f55ffd2ebed5 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 603fb18e7b1a4043b097f55ffd2ebed5';\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        copyright = 'Copyright 2015 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 copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\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     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_603fb18e7b1a4043b097f55ffd2ebed5()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2015a<br><\/p><\/div><!--\r\n603fb18e7b1a4043b097f55ffd2ebed5 ##### SOURCE BEGIN #####\r\n%%\r\n% A question came into tech support a month or so ago regarding the\r\n% <https:\/\/www.mathworks.com\/help\/images\/ref\/imdilate.html documentation for\r\n% |imdilate|>. The question concerned an apparent discrepancy in the equations\r\n% for binary and grayscale dilation. Here's the formula given for binary\r\n% dilation:\r\n%\r\n% $$A \\oplus B = \\{ z | (\\hat{B})_z \\cap A \\neq 0 \\}$$\r\n%\r\n% where $\\hat{B}$ is the reflection of the structuring element $B$. The\r\n% reference page goes on to say, \"in other words, it is the set of pixel\r\n% locations $z$ where the reflected structuring element overlaps with foreground\r\n% pixels of $A$ when translated to $z$.\"\r\n%\r\n% The reference page then gives another equation for gray-scale dilation.\r\n%\r\n% $$(A \\oplus B)(x,y) = \\max \\{ A(x - x', y - y') + B(x',y') | (x',y') \\in D_B\\}$$\r\n%\r\n% where $D_B$ is the domain of the structuring element and $A(x,y)$ is assumed\r\n% to be $-\\infty$ outside the domain of the image.\r\n%\r\n% The user who contacted tech support wondered if there might be an error in\r\n% the equation for gray-scale dilation because the equation doesn't show a\r\n% reflection of the structuring element. The case was escalated to me for\r\n% comment.\r\n%\r\n% The gray-scale dilation equation above is correct. (Well, it's correct for\r\n% about half the world. The other half uses a slightly different form.)\r\n%\r\n% But there are several different but equivalent mathematical equations that can\r\n% be used to define dilation. Each of these equations corresponds to a different\r\n% but equivalent geometric interpretation. The equation above can be interpreted\r\n% as follows:\r\n%\r\n% To compute the output at $(x,y)$, flip (or reflect) $A$ through the origin and\r\n% then slide the origin pixel over to $(x,y)$. Form the sums of the $A$ pixels\r\n% with the structuring element heights underneath. Find the maximum of these\r\n% sums and record the result as the output at $(x,y)$.\r\n%\r\n% As it turns out, dilation is commutative. That suggests that there is a form\r\n% of the equation, and a corresponding geometric interpretation, in which the\r\n% structuring element is reflected instead of the image. We can take the first\r\n% step in that direction via a substitution of variables. Let $q = x - x'$ and\r\n% $r = y - y'$. Then:\r\n%\r\n% $$(A \\oplus B)(x,y) = \\max \\{ A(q,r) + B(x-q, y-r) | (x-q, y-r) \\in D_B \\}$$\r\n%\r\n% Since $q$ and $r$ are \"dummy\" variables, we can rewrite them as $x'$ and $y'$.\r\n%\r\n% $$(A \\oplus B)(x,y) = \\max \\{ A(x',y') + B(x-x',y-y') | (x-x',y-y') \\in D_B \\}$$\r\n%\r\n% This second equation has the geometric interpretation of leaving the image in\r\n% placing, flipping (reflecting) and sliding the structuring element, performing sums of the\r\n% corresponding image pixels and structuring element heights, and then taking\r\n% the maximum of the sums.\r\n%\r\n% So which equation should we use? Well, both are correct. Which form to use for\r\n% a real implementation is completely up to the implementer. And these are not\r\n% the only two equations and geometric interpretations that are valid.\r\n%\r\n% I do think, though, that there is some merit in modifying the gray-scale\r\n% dilation equation in our documentation to make it more consistent with the\r\n% form used for binary dilation.\r\n%\r\n% *Dear reader, I am curious: do you use nonflat grayscale dilation or erosion\r\n% in your work?* As far as I can tell, there are not many practical applications\r\n% for using nonflat structuring elements. If you have a use for it, please leave\r\n% me a comment below.\r\n##### SOURCE END ##### 603fb18e7b1a4043b097f55ffd2ebed5\r\n-->","protected":false},"excerpt":{"rendered":"<p>A question came into tech support a month or so ago regarding the documentation for imdilate. The question concerned an apparent discrepancy in the equations for binary and grayscale dilation. Here's... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2015\/05\/06\/gray-scale-dilation-equation\/\">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":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/1321"}],"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=1321"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/1321\/revisions"}],"predecessor-version":[{"id":1322,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/1321\/revisions\/1322"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=1321"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=1321"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=1321"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}