{"id":295,"date":"2009-10-19T07:00:36","date_gmt":"2009-10-19T11:00:36","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/2009\/10\/19\/faster-imreconstruct-in-r2009b\/"},"modified":"2019-10-29T08:01:00","modified_gmt":"2019-10-29T12:01:00","slug":"faster-imreconstruct-in-r2009b","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2009\/10\/19\/faster-imreconstruct-in-r2009b\/","title":{"rendered":"Faster morphological reconstruction in R2009b"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p>We've been working for a while now to make Image Processing Toolbox functions run faster.  The <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009b\/toolbox\/images\/rn\/rn_intro.html\">R2009b release notes<\/a> mention several performance improvements.  We've gotten some feedback, though, that our release notes are pretty vague about\r\n      the improvements.  I can't argue with that impression.  We tend to be vague because performance optimization is a very complex\r\n      topic, and it can be quite difficult to characterize performance changes in a way that is brief, understandable, and accurate\r\n      for every user's own hardware and data.\r\n   <\/p>\r\n   <p>But I've decided to start posting more detailed information here about the performance improvements.  I have more flexibility\r\n      (and room!) here than we have with the release notes.\r\n   <\/p>\r\n   <p>Today I'll tackle <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009b\/toolbox\/images\/index.html?\/access\/helpdesk\/help\/releases\/R2009b\/toolbox\/images\/imreconstruct.html\"><tt>imreconstruct<\/tt><\/a>, which performs morphological reconstruction.  Reconstruction is a very useful operation that I've written about here before.\r\n       For example, see my <a href=\"https:\/\/blogs.mathworks.com\/steve\/2008\/07\/14\/opening-by-reconstruction\/\">post from last year on opening by reconstruction<\/a>.  Several other Image Processing Toolbox functions call <tt>imreconstruct<\/tt>, including <tt>imclearborder<\/tt>, <tt>imfill<\/tt>, <tt>imhmax<\/tt>, <tt>imhmin<\/tt>, <tt>imextendedmax<\/tt>, <tt>imextendedmin<\/tt>, and <tt>imimposemin<\/tt>.\r\n   <\/p>\r\n   <p>Let me use the <a href=\"https:\/\/blogs.mathworks.com\/steve\/2008\/07\/14\/opening-by-reconstruction\/\">opening by reconstruction example<\/a> as a benchmark case.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">url = <span style=\"color: #A020F0\">'https:\/\/blogs.mathworks.com\/images\/steve\/2008\/book_text.png'<\/span>;\r\ntext = imread(url);\r\nimshow(text, <span style=\"color: #A020F0\">'InitialMagnification'<\/span>, 25)\r\ntitle(<span style=\"color: #A020F0\">'918-by-2018 image displayed at 25% magnification'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2009\/imreconstruct_performance_01.png\"> <p>The example task was to find letters containing vertical strokes by eroding with a vertical structuring element and then performing\r\n      reconstruction.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">se = strel(ones(51, 1));\r\nmarker = imerode(text, se);\r\ntext2 = imreconstruct(marker, text);\r\nimshow(text2, <span style=\"color: #A020F0\">'InitialMagnification'<\/span>, 25)\r\ntitle(<span style=\"color: #A020F0\">'Output image displayed at 25% magnification'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2009\/imreconstruct_performance_02.png\"> <p>So how long does that call to <tt>imreconstruct<\/tt> take in R2009b?  I'll use my function <tt>timeit<\/tt>, which you can download from the MATLAB Central File Exchange.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">timeit(@() imreconstruct(marker, text))<\/pre><pre style=\"font-style:oblique\">\r\nans =\r\n\r\n    0.0241\r\n\r\n<\/pre><p>That time is about 45 times faster than the same operation performed in R2009a.  Note that I'm running on two-core computer;\r\n      the improved <tt>imreconstruct<\/tt> is multithreaded, so the performance improvement would be greater on a four-core computer, for example.\r\n   <\/p>\r\n   <p>Now let's time gray-scale reconstruction.  I'll make a 1024-by-1024 test image and compute a marker image by subtraction.\r\n       This kind of operation is often used to suppress small peaks in an image.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">I = repmat(imread(<span style=\"color: #A020F0\">'rice.png'<\/span>), 4, 4);\r\nmarker = I - 20;\r\ntimeit(@() imreconstruct(marker, I))<\/pre><pre style=\"font-style:oblique\">\r\nans =\r\n\r\n    0.0201\r\n\r\n<\/pre><p>This time is about 30 times faster than R2009a, again running on my two-core laptop.<\/p>\r\n   <p>Now for some key caveats you should know.  For now, the performance improvements described here only work for 2-D inputs that\r\n      are uint8, uint16, or single, and only when the specified connectivity is 4 or 8.  We'll be working in the future to extend\r\n      the speed improvements to other inputs.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_b2a3075faf7d470d90cb0c77ce2782fa() {\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='b2a3075faf7d470d90cb0c77ce2782fa ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' b2a3075faf7d470d90cb0c77ce2782fa';\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 = '';\r\n        copyright = 'Copyright 2009 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_b2a3075faf7d470d90cb0c77ce2782fa()\"><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.9<br><\/p>\r\n<\/div>\r\n<!--\r\nb2a3075faf7d470d90cb0c77ce2782fa ##### SOURCE BEGIN #####\r\n%%\r\n% We've been working for a while now to make Image Processing Toolbox functions\r\n% run faster.  The \r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2009b\/toolbox\/images\/rn\/rn_intro.html \r\n% R2009b release notes> mention several performance\r\n% improvements.  We've gotten some feedback, though, that our release notes are\r\n% pretty vague about the improvements.  I can't argue with that impression.  We tend\r\n% to be vague because performance optimization is a very complex topic, and it\r\n% can be quite difficult to characterize performance changes in a way that is\r\n% brief, understandable, and accurate for every user's own hardware and data.\r\n%\r\n% But I've decided to start posting more detailed information here about the\r\n% performance improvements.  I have more flexibility (and room!) here than we\r\n% have with the release notes.\r\n%\r\n% Today I'll tackle \r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2009b\/toolbox\/images\/index.html?\/access\/helpdesk\/help\/releases\/R2009b\/toolbox\/images\/imreconstruct.html \r\n% |imreconstruct|>, which performs morphological\r\n% reconstruction.  Reconstruction is a very useful operation that I've written\r\n% about here before.  For example, see my \r\n% <https:\/\/blogs.mathworks.com\/steve\/2008\/07\/14\/opening-by-reconstruction\/ \r\n% post from last year on opening by reconstruction>.  Several other Image Processing\r\n% Toolbox functions call |imreconstruct|, including |imclearborder|, |imfill|,\r\n% |imhmax|, |imhmin|, |imextendedmax|, |imextendedmin|, and |imimposemin|. \r\n%\r\n% Let me use the \r\n% <https:\/\/blogs.mathworks.com\/steve\/2008\/07\/14\/opening-by-reconstruction\/  \r\n% opening by reconstruction example> as a benchmark case.\r\n\r\nurl = 'https:\/\/blogs.mathworks.com\/images\/steve\/2008\/book_text.png';\r\ntext = imread(url);\r\nimshow(text, 'InitialMagnification', 25)\r\ntitle('918-by-2018 image displayed at 25% magnification')\r\n\r\n%%\r\n% The example task was to find letters containing vertical strokes by eroding\r\n% with a vertical structuring element and then performing reconstruction.\r\n\r\nse = strel(ones(51, 1));\r\nmarker = imerode(text, se);\r\ntext2 = imreconstruct(marker, text);\r\nimshow(text2, 'InitialMagnification', 25)\r\ntitle('Output image displayed at 25% magnification')\r\n\r\n%%\r\n% So how long does that call to |imreconstruct| take in R2009b?  I'll use my\r\n% function\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/18798-timeit-benchmarking-function \r\n% |timeit|>, which you can download from the MATLAB Central File Exchange.\r\n\r\ntimeit(@() imreconstruct(marker, text))\r\n\r\n%%\r\n% That time is about 45 times faster than the same operation performed in\r\n% R2009a.  Note that I'm running on two-core computer; the improved\r\n% |imreconstruct| is multithreaded, so the performance improvement would be\r\n% greater on a four-core computer, for example.\r\n%\r\n% Now let's time gray-scale reconstruction.  I'll make a 1024-by-1024 test\r\n% image and compute a marker image by subtraction.  This kind of operation is\r\n% often used to suppress small peaks in an image.\r\n\r\nI = repmat(imread('rice.png'), 4, 4);\r\nmarker = I - 20;\r\ntimeit(@() imreconstruct(marker, I))\r\n\r\n%%\r\n% This time is about 30 times faster than R2009a, again running on my two-core\r\n% laptop.\r\n%\r\n% Now for some key caveats you should know.  For now, the performance\r\n% improvements described here only work for 2-D inputs that are uint8, uint16,\r\n% or single, and only when the specified connectivity is 4 or 8.  We'll be\r\n% working in the future to extend the speed improvements to other inputs.\r\n\r\n\r\n##### SOURCE END ##### b2a3075faf7d470d90cb0c77ce2782fa\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   We've been working for a while now to make Image Processing Toolbox functions run faster.  The R2009b release notes mention several performance improvements.  We've gotten some feedback, though,... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2009\/10\/19\/faster-imreconstruct-in-r2009b\/\">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":[246,76,438,36,288,116,106,474,52],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/295"}],"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=295"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/295\/revisions"}],"predecessor-version":[{"id":2637,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/295\/revisions\/2637"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=295"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=295"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=295"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}