{"id":1326,"date":"2015-06-04T16:01:24","date_gmt":"2015-06-04T20:01:24","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/?p=1326"},"modified":"2019-11-01T11:40:39","modified_gmt":"2019-11-01T15:40:39","slug":"image-region-filling-an-updated-design","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2015\/06\/04\/image-region-filling-an-updated-design\/","title":{"rendered":"Image region filling &#8211; an updated design"},"content":{"rendered":"<div class=\"content\"><p>About a year ago, I wrote a <a href=\"https:\/\/blogs.mathworks.com\/steve\/2014\/03\/18\/roifill-design-critique\/\">blog post<\/a> criticizing one of my functional designs from the 1990s, <a href=\"https:\/\/www.mathworks.com\/help\/images\/ref\/roifill.html\"><tt>roifill<\/tt><\/a>. Now I have an update based on the R2015a release of Image Processing Toolbox.<\/p><p>Let me show you an example to refresh your memory about <tt>roifill<\/tt>.<\/p><pre class=\"codeinput\">I = imread(<span class=\"string\">'rice.png'<\/span>);\r\nimshow(I)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2015\/regionfill_replaces_roifill_01.png\" alt=\"\"> <p>You can use <tt>roifill<\/tt> to fill in pixels inside a region. (ROI in the function name stands for ''region of interest.'') You can specify the region interactively, using a mouse, or by providing the coordinates of a polygonal boundary, or by providing a binary mask. Here I'll provide the coordinates directly.<\/p><pre class=\"codeinput\">x = [47 50 71 83 81 66 53 48];\r\ny = [102 110 110 103 92 92 97 102];\r\n\r\nJ = roifill(I,x,y);\r\nimshow(J)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2015\/regionfill_replaces_roifill_02.png\" alt=\"\"> <p>As I described in my <a href=\"https:\/\/blogs.mathworks.com\/steve\/2014\/03\/18\/roifill-design-critique\/\">earlier post<\/a>, the form of <tt>roifill<\/tt> that takes a mask image suffered from a design flaw. Most people would reasonably assume that the mask image would specify the set of pixel values that are to be filled. And that's the way I should have designed it. Unfortunately, though, <tt>roifill<\/tt> actually only fills the <b>interior<\/b> pixels of the mask.<\/p><p>Suppose your fill mask looked like this:<\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2014\/roifill-mask-1.png\" alt=\"\"> <\/p><p>The function <tt>roifill<\/tt> only replaces these interior pixels of the mask:<\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2014\/roifill-mask-2.png\" alt=\"\"> <\/p><p>The pixels on the edge of the mask are used to establish boundary conditions for the fill equation. Those pixels stay the same, which is not what most people expect.<\/p><p>So how could we fix this problem? Changing the default behavior of <tt>roifill<\/tt> would be a significant incompatibility. But we have learned that optional behaviors often go undiscovered, so most people don't benefit from them.<\/p><p>The Image Processing Toolbox team decided to address the problem by introducing a new function, <a href=\"https:\/\/www.mathworks.com\/help\/images\/ref\/regionfill.html\"><tt>regionfill<\/tt><\/a>, with the desired behavior. The function <tt>roifill<\/tt> remains in the product, so existing code that uses it will continue to work. Presumably, anyone currently using <tt>roifill<\/tt> with the mask syntax has already worked around its awkward behavior.<\/p><p>At the bottom of the reference page for <tt>regionfill<\/tt>, there is a note that this function was introduced in R2015a.<\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2015\/regionfill-in-R2015a.png\" alt=\"\"> <\/p><p>And at the top of the reference page for <tt>roifill<\/tt>, there is a note encouraging the use of <tt>regionfill<\/tt>.<\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2015\/roill-not-recommended.png\" alt=\"\"> <\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_0e8beca9e930431b97254622e6dc2c6f() {\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='0e8beca9e930431b97254622e6dc2c6f ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 0e8beca9e930431b97254622e6dc2c6f';\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_0e8beca9e930431b97254622e6dc2c6f()\"><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\n0e8beca9e930431b97254622e6dc2c6f ##### SOURCE BEGIN #####\r\n%%\r\n% About a year ago, I wrote a <https:\/\/blogs.mathworks.com\/steve\/2014\/03\/18\/roifill-design-critique\/ \r\n% blog post> criticizing one of my functional\r\n% designs from the 1990s, <https:\/\/www.mathworks.com\/help\/images\/ref\/roifill.html \r\n% |roifill|>. Now I have an update based on the R2015a release of Image\r\n% Processing Toolbox.\r\n%\r\n% Let me show you an example to refresh your memory about |roifill|. \r\n\r\nI = imread('rice.png');\r\nimshow(I)\r\n\r\n%%\r\n% You can use |roifill| to fill in pixels inside a region. (ROI in the\r\n% function name stands for ''region of interest.'') You can specify the\r\n% region interactively, using a mouse, or by providing the coordinates of a\r\n% polygonal boundary, or by providing a binary mask. Here I'll provide the\r\n% coordinates directly.\r\n\r\nx = [47 50 71 83 81 66 53 48];\r\ny = [102 110 110 103 92 92 97 102];\r\n\r\nJ = roifill(I,x,y);\r\nimshow(J)\r\n\r\n%%\r\n% As I described in my <https:\/\/blogs.mathworks.com\/steve\/2014\/03\/18\/roifill-design-critique\/ \r\n% earlier post>, the form of |roifill| that takes a\r\n% mask image suffered from a design flaw. Most people would reasonably\r\n% assume that the mask image would specify the set of pixel values that are\r\n% to be filled. And that's the way I should have designed it.\r\n% Unfortunately, though, |roifill| actually only fills the *interior*\r\n% pixels of the mask.\r\n%\r\n% Suppose your fill mask looked like this:\r\n%\r\n% <<https:\/\/blogs.mathworks.com\/images\/steve\/2014\/roifill-mask-1.png>>\r\n%\r\n% The function |roifill| only replaces these interior pixels of the mask:\r\n%\r\n% <<https:\/\/blogs.mathworks.com\/images\/steve\/2014\/roifill-mask-2.png>>\r\n%\r\n% The pixels on the edge of the mask are used to establish boundary\r\n% conditions for the fill equation. Those pixels stay the same, which is\r\n% not what most people expect.\r\n%\r\n% So how could we fix this problem? Changing the default behavior of\r\n% |roifill| would be a significant incompatibility. But we have learned\r\n% that optional behaviors often go undiscovered, so most people don't\r\n% benefit from them.\r\n%\r\n% The Image Processing Toolbox team decided to address the problem by\r\n% introducing a new function,\r\n% <https:\/\/www.mathworks.com\/help\/images\/ref\/regionfill.html\r\n% |regionfill|>, with the desired behavior. The function |roifill| remains\r\n% in the product, so existing code that uses it will continue to work.\r\n% Presumably, anyone currently using |roifill| with the mask syntax has\r\n% already worked around its awkward behavior.\r\n% \r\n% At the bottom of the reference page for |regionfill|, there is a note\r\n% that this function was introduced in R2015a.\r\n%\r\n% <<https:\/\/blogs.mathworks.com\/images\/steve\/2015\/regionfill-in-R2015a.png>>\r\n%\r\n% And at the top of the reference page for |roifill|, there is a note\r\n% discouraging the use of |regionfill|.\r\n%\r\n% <<https:\/\/blogs.mathworks.com\/images\/steve\/2015\/roill-not-recommended.png>>\r\n##### SOURCE END ##### 0e8beca9e930431b97254622e6dc2c6f\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img src=\"https:\/\/blogs.mathworks.com\/steve\/files\/roifill-mask-2.png\" class=\"img-responsive attachment-post-thumbnail size-post-thumbnail wp-post-image\" alt=\"\" decoding=\"async\" loading=\"lazy\" \/><\/div><p>About a year ago, I wrote a blog post criticizing one of my functional designs from the 1990s, roifill. Now I have an update based on the R2015a release of Image Processing Toolbox.Let me show you an... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2015\/06\/04\/image-region-filling-an-updated-design\/\">read more >><\/a><\/p>","protected":false},"author":42,"featured_media":1329,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[76,36,374,368],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/1326"}],"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=1326"}],"version-history":[{"count":4,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/1326\/revisions"}],"predecessor-version":[{"id":2587,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/1326\/revisions\/2587"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media\/1329"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=1326"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=1326"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=1326"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}