{"id":231,"date":"2008-10-15T13:16:25","date_gmt":"2008-10-15T17:16:25","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/2008\/10\/15\/dilation-algorithms-performance-of-decomposed-strels\/"},"modified":"2019-10-28T09:45:03","modified_gmt":"2019-10-28T13:45:03","slug":"dilation-algorithms-performance-of-decomposed-strels","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2008\/10\/15\/dilation-algorithms-performance-of-decomposed-strels\/","title":{"rendered":"Dilation algorithms &#8211; performance of decomposed strels"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p>We looked at the decomposition of different structuring element shapes in my <a href=\"https:\/\/blogs.mathworks.com\/steve\/2008\/09\/29\/dilation-algorithms-decomposing-more-shapes\/\">previous post<\/a> on dilation algorithms.  Today let's time some code to how decomposition makes a real difference in performance.\r\n   <\/p>\r\n   <p><i>Note that running the code in this post requires that you download my timeit function from the MATLAB Central File Exchange.<\/i><\/p>\r\n   <p>For our test image, I'll use a grayscale version of the peppers image that ships with the Image Processing Toolbox.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">I = rgb2gray(imread(<span style=\"color: #A020F0\">'peppers.png'<\/span>));\r\nimshow(I)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2008\/dilation_4_01.jpg\"> <p>Let's time dilations of <tt>I<\/tt> with disk structuring elements of varying radii. As I mentioned previously, <tt>strel('disk',r)<\/tt> creates a structuring element that is a decomposable approximation to a disk.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #228B22\">% Dilate using radii of 3 through 25.<\/span>\r\nrr1 = 3:25;\r\n\r\n<span style=\"color: #228B22\">% Initialize the vector that will hold the measured times.<\/span>\r\nt1 = [];\r\n<span style=\"color: #0000FF\">for<\/span> r = rr1\r\n   se = strel(<span style=\"color: #A020F0\">'disk'<\/span>, r);\r\n\r\n   <span style=\"color: #228B22\">% Make a function that dilates I by the structuring element se.<\/span>\r\n   f = @() imdilate(I, se);\r\n\r\n   <span style=\"color: #228B22\">% Measure how long the dilation takes.<\/span>\r\n   t1(end + 1) = timeit(f);\r\n<span style=\"color: #0000FF\">end<\/span>\r\n\r\nplot(rr1, t1)\r\ngrid <span style=\"color: #A020F0\">on<\/span>\r\nylabel(<span style=\"color: #A020F0\">'Time (seconds)'<\/span>)\r\nxlabel(<span style=\"color: #A020F0\">'Disk radius'<\/span>)\r\n<span style=\"color: #228B22\">% Make sure we show the time scale down to 0.  (Thanks, Chris!)<\/span>\r\nylim([0 max(t1)]);\r\ntitle(<span style=\"color: #A020F0\">'Time to compute dilation with decomposable disk'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2008\/dilation_4_02.jpg\"> <p>Now let's repeat the timing experiment using nondecomposable disks.  You make such a structuring element using the syntax\r\n      <tt>strel('disk',r,0)<\/tt>.\r\n   <\/p>\r\n   <p>(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\r\n      tired of waiting for it to finish!)\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #228B22\">% Dilate using radius values 3-8.<\/span>\r\nrr2 = 3:8;\r\nt2 = [];\r\n<span style=\"color: #0000FF\">for<\/span> r = rr2\r\n   se = strel(<span style=\"color: #A020F0\">'disk'<\/span>, r, 0);\r\n   t2(end + 1) = timeit(@() imdilate(I, se));\r\n<span style=\"color: #0000FF\">end<\/span>\r\n\r\n<span style=\"color: #228B22\">% Plot the results of both experiments together.<\/span>\r\nplot(rr, t1, rr2, t2)\r\ngrid <span style=\"color: #A020F0\">on<\/span>\r\nylabel(<span style=\"color: #A020F0\">'Time (seconds)'<\/span>)\r\nxlabel(<span style=\"color: #A020F0\">'Disk radius'<\/span>)\r\n<span style=\"color: #228B22\">% Make sure we show the time scale down to 0.  (Thanks, Chris!)<\/span>\r\nylim([0 max([t1, t2])]);\r\nlegend(<span style=\"color: #A020F0\">'Decomposable'<\/span>, <span style=\"color: #A020F0\">'Nondecomposable'<\/span>);\r\ntitle(<span style=\"color: #A020F0\">'Speed comparison: Decomposable vs. Nondecomposable disks'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2008\/dilation_4_03.jpg\"> <p>I think you can see from the plot above why we chose to use the decomposable disk approximation as the default!<\/p>\r\n   <p>So what does it look like when you dilate gray peppers with a disk?<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">J = imdilate(I, strel(<span style=\"color: #A020F0\">'disk'<\/span>, 25));\r\nimshow(J)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2008\/dilation_4_04.jpg\"> <p>The resulting blobs look kind of octagony.  That's because of the default decomposition used.  You can ask <tt>strel<\/tt> to use a closer approximation to a disk.  It will take a bit longer to compute, although still not nearly as long as using\r\n      the nondecomposable disk.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">se8 = strel(<span style=\"color: #A020F0\">'disk'<\/span>, 25, 8);\r\nJ8 = imdilate(I, se8);\r\nimshow(J8)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2008\/dilation_4_05.jpg\"> <p>Bon appetit!<\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_d3695b8a749f4174b5c868e2936e11c4() {\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='d3695b8a749f4174b5c868e2936e11c4 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' d3695b8a749f4174b5c868e2936e11c4';\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 2008 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-->\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_d3695b8a749f4174b5c868e2936e11c4()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.7<br><\/p>\r\n<\/div>\r\n<!--\r\nd3695b8a749f4174b5c868e2936e11c4 ##### SOURCE BEGIN #####\r\n%%\r\n% We looked at the decomposition of different structuring element shapes in my\r\n% <https:\/\/blogs.mathworks.com\/steve\/2008\/09\/29\/dilation-algorithms-decomposing-more-shapes\/ \r\n% previous post> on dilation algorithms.  Today let's time some code to how\r\n% decomposition makes a real difference in performance.\r\n%\r\n% _Note that running the code in this post requires that you download my\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/loadFile.do?objectId=18798&objectType=FILE \r\n% timeit> \r\n% function from the MATLAB Central File Exchange._\r\n%\r\n% For our test image, I'll use a grayscale version of the peppers image that\r\n% ships with the Image Processing Toolbox.\r\n\r\nI = rgb2gray(imread('peppers.png'));\r\nimshow(I)\r\n\r\n%%\r\n% Let's time dilations of |I| with disk structuring elements of varying radii.\r\n% As I mentioned previously, |strel('disk',r)| creates a structuring element\r\n% that is a decomposable approximation to a disk.\r\n\r\n% Dilate using radii of 3 through 25.\r\nrr1 = 3:25;\r\n\r\n% Initialize the vector that will hold the measured times.\r\nt1 = [];\r\nfor r = rr1\r\n   se = strel('disk', r);\r\n   \r\n   % Make a function that dilates I by the structuring element se.\r\n   f = @() imdilate(I, se);\r\n   \r\n   % Measure how long the dilation takes.\r\n   t1(end + 1) = timeit(@() imdilate(I, se));\r\nend\r\n\r\nplot(rr1, t1)\r\ngrid on\r\nylabel('Time (seconds)')\r\nxlabel('Disk radius')\r\n% Make sure we show the time scale down to 0.  (Thanks, Chris!)\r\nylim([0 max(t1)]);\r\ntitle('Time to compute dilation with decomposable disk')\r\n\r\n%%\r\n% Now let's repeat the timing experiment using nondecomposable disks.  You make\r\n% such a structuring element using the syntax |strel('disk',r,0)|.\r\n% \r\n% (In my first draft of this post, I tried to do this step using the \r\n% same range of radius values as above, 3-25.  But I got tired of waiting for\r\n% it to finish!)\r\n\r\n% Dilate using radius values 3-8.\r\nrr2 = 3:8;\r\nt2 = [];\r\nfor r = rr2\r\n   se = strel('disk', r, 0);\r\n   t2(end + 1) = timeit(@() imdilate(I, se));\r\nend\r\n\r\n% Plot the results of both experiments together.\r\nplot(rr, t1, rr2, t2)\r\ngrid on\r\nylabel('Time (seconds)')\r\nxlabel('Disk radius')\r\n% Make sure we show the time scale down to 0.  (Thanks, Chris!)\r\nylim([0 max([t1, t2])]);\r\nlegend('Decomposable', 'Nondecomposable');\r\ntitle('Speed comparison: Decomposable vs. Nondecomposable disks')\r\n\r\n%%\r\n% I think you can see from the plot above why we chose to use the decomposable\r\n% disk approximation as the default!\r\n%\r\n% So what does it look like when you dilate gray peppers with a disk?\r\n\r\nJ = imdilate(I, strel('disk', 25));\r\nimshow(J)\r\n\r\n%%\r\n% The resulting blobs look kind of\r\n% <http:\/\/www.merriam-webster.com\/dictionary\/octagony octagony>.  That's because\r\n% of the default decomposition used.  You can ask |strel| to use a closer\r\n% approximation to a disk.  It will take a bit longer to compute, although still\r\n% not nearly as long as using the nondecomposable disk.\r\n\r\nse8 = strel('disk', 25, 8);\r\nJ8 = imdilate(I, se8);\r\nimshow(J8)\r\n\r\n%%\r\n% Bon appetite!\r\n##### SOURCE END ##### d3695b8a749f4174b5c868e2936e11c4\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   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... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2008\/10\/15\/dilation-algorithms-performance-of-decomposed-strels\/\">read more >><\/a><\/p>","protected":false},"author":42,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[18],"tags":[70,124,76,36,92,68,104,106,474,52,94,96,298],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/231"}],"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=231"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/231\/revisions"}],"predecessor-version":[{"id":2646,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/231\/revisions\/2646"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=231"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=231"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=231"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}