{"id":990,"date":"2014-03-18T13:42:49","date_gmt":"2014-03-18T17:42:49","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/?p=990"},"modified":"2019-11-01T11:00:33","modified_gmt":"2019-11-01T15:00:33","slug":"roifill-design-critique","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2014\/03\/18\/roifill-design-critique\/","title":{"rendered":"roifill design critique"},"content":{"rendered":"\r\n<div class=\"content\"><p><i>\"Is there anything else you would like to tell us?\"<\/i><\/p><p>This was the question whose answer led me to revisit an old friend of mine, the <tt>roifill<\/tt> function. This is a function that smoothly fills a specified region of interest in a gray-scale image. I implemented it in the early 1990s following a hallway conversation with another MathWorks developer, who said to me something along the lines of \"I bet you could fill an image region based on the equation for a soap film surface along a wire rim.\"<\/p><p>(I don't know when the term <i>inpainting<\/i> became common, but I certainly had not heard it way back then.)<\/p><p>Here's an example of <tt>roifill<\/tt> in action.<\/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\/2014\/roifill_design_issue_01.png\" alt=\"\"> <p>If you now call <tt>roifill<\/tt>, you can select a polyline around any object you like, and the function will remove the object and smoothly fill in the resulting hole. Here's a sample output (can you find the missing grain of rice?):<\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2014\/rice-down-one.png\" alt=\"\"> <\/p><p>Anyway, fast-forward to just two weeks ago. A customer that I know was helping us with a usability test for a planned MATLAB feature. I was sitting in the observation room. After the test tasks were completed, the user experience expert asked a common question: <i>Is there anything else you'd like to tell us about?<\/i><\/p><p>It turns out that this customer knows a great deal about the Image Processing Toolbox, and he had some interesting feedback about the <tt>roifill<\/tt> function and the <i>mask<\/i> image.<\/p><p><tt>roifill<\/tt>, you see, takes a \"mask\" image that specifies the set of pixel values that are to be filled. At least, that's the way I should have designed it. Unfortunately, I didn't, and my design decision was causing a lot of confusion. That's because <tt>roifill<\/tt> actually only replaces the interior pixels of the mask.<\/p><p>Let me show you a diagram to explain what I mean. 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>It would be a very reasonable expectation to think that <i>roifill<\/i> would replace all the pixels corresponding to this mask. And that's what our intrepid customer thought should happen. But it actually replaces only the <i>interior<\/i> pixels of the mask. That is, it replaces the pixels marked in gray:<\/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. These pixels stay the same. Hence the confusion.<\/p><p>As I walked back to my office after the usability test, I tried to figure out why I would have originally designed the function this way. I think there were two reasons. First, I was much less experienced with designing image processing functions then. Second, I'm pretty sure I was only thinking about an <i>interactive workflow<\/i>. That is, a user interactively draws a region around an object using a mouse, and then <tt>roifill<\/tt> processes the result.<\/p><p>In the interactive workflow, it doesn't matter (very much) how the mask border pixels are handled. However, in a noninteractive workflow, I now think it really only makes sense for the mask to represent the set of \"bad pixels\" that all need to be replaced by the function.<\/p><p>Ah well. Lessons learned. I will suggest that the Image Processing Toolbox development team think about how to address this behavior quirk in a future release.<\/p><p>In the meantime, dear reader ... <i>Is there anything else you would like to tell us?<\/i><\/p><p>Let us know by leaving a comment.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_784edfa153654b97a574d88f26364a8a() {\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='784edfa153654b97a574d88f26364a8a ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 784edfa153654b97a574d88f26364a8a';\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 2014 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_784edfa153654b97a574d88f26364a8a()\"><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; R2014a<br><\/p><p class=\"footer\"><br>\r\n      Published with MATLAB&reg; R2014a<br><\/p><\/div><!--\r\n784edfa153654b97a574d88f26364a8a ##### SOURCE BEGIN #####\r\n%%\r\n% _\"Is there anything else you would like to tell us?\"_\r\n%\r\n% This was the question whose answer led me to revisit an old friend of\r\n% mine, the |roifill| function. This is a function that smoothly fills a\r\n% specified region of interest in a gray-scale image. I implemented it in\r\n% the early 1990s following a hallway conversation with another MathWorks\r\n% developer, who said to me something along the lines of \"I bet you could\r\n% fill an image region based on the equation for a soap film surface along\r\n% a wire rim.\"\r\n%\r\n% (I don't know when the term _inpainting_ became common, but I certainly\r\n% had not heard it way back then.)\r\n%\r\n% Here's an example of |roifill| in action.\r\n\r\nI = imread('rice.png');\r\nimshow(I)\r\n\r\n%%\r\n% If you now call |roifill|, you can select a polyline around any object\r\n% you like, and the function will remove the object and smoothly fill in\r\n% the resulting hole. Here's a sample output (can you find the missing\r\n% grain of rice?):\r\n%\r\n% <<https:\/\/blogs.mathworks.com\/images\/steve\/2014\/rice-down-one.png>>\r\n%\r\n% Anyway, fast-forward to just two weeks ago. A customer that I know was\r\n% helping us with a usability test for a planned MATLAB feature. I was\r\n% sitting in the observation room. After the test tasks were completed, the\r\n% user experience expert asked a common question: _Is there anything else\r\n% you'd like to tell us about?_\r\n%\r\n% It turns out that this customer knows a great deal about the Image\r\n% Processing Toolbox, and he had some interesting feedback about the\r\n% |roifill| function and the _mask_ image. \r\n%\r\n% |roifill|, you see, takes a \"mask\" image that specifies the set of pixel\r\n% values that are to be filled. At least, that's the way I should\r\n% have designed it. Unfortunately, I didn't, and my design decision was\r\n% causing a lot of confusion. That's because |roifill| actually only\r\n% replaces the interior pixels of the mask.\r\n%\r\n% Let me show you a diagram to explain what I mean. Suppose your fill mask\r\n% looked like this:\r\n%\r\n% <<https:\/\/blogs.mathworks.com\/images\/steve\/2014\/roifill-mask-1.png>>\r\n%\r\n% It would be a very reasonable expectation to think that _roifill_ would\r\n% replace all the pixels corresponding to this mask. And that's what our\r\n% intrepid customer thought should happen. But it actually replaces only\r\n% the _interior_ pixels of the mask. That is, it replaces the pixels marked\r\n% in gray:\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. These pixels stay the same. Hence the\r\n% confusion.\r\n%\r\n% As I walked back to my office after the usability test, I tried to figure\r\n% out why I would have originally designed the function this way. I think\r\n% there were two reasons. First, I was much less experienced with designing\r\n% image processing functions then. Second, I'm pretty sure I was only\r\n% thinking about an _interactive workflow_. That is, a user interactively\r\n% draws a region around an object using a mouse, and then |roifill|\r\n% processes the result.\r\n%\r\n% In the interactive workflow, it doesn't matter (very much) how the mask\r\n% border pixels are handled. However, in a noninteractive workflow, I now\r\n% think it really only makes sense for the mask to represent the set of\r\n% \"bad pixels\" that all need to be replaced by the function.\r\n%\r\n% Ah well. Lessons learned. I will suggest that the Image Processing\r\n% Toolbox development team think about how to address this behavior quirk\r\n% in a future release.\r\n%\r\n% In the meantime, dear reader ... _Is there anything else you would like\r\n% to tell us?_\r\n%\r\n% Let us know by leaving a comment.\r\n\r\n##### SOURCE END ##### 784edfa153654b97a574d88f26364a8a\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2014\/roifill_design_issue_01.png\" onError=\"this.style.display ='none';\" \/><\/div><p>\r\n\"Is there anything else you would like to tell us?\"This was the question whose answer led me to revisit an old friend of mine, the roifill function. This is a function that smoothly fills a... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2014\/03\/18\/roifill-design-critique\/\">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":[76,36,368],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/990"}],"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=990"}],"version-history":[{"count":5,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/990\/revisions"}],"predecessor-version":[{"id":995,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/990\/revisions\/995"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=990"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=990"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=990"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}