{"id":155,"date":"2007-07-26T14:58:26","date_gmt":"2007-07-26T18:58:26","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/2007\/07\/26\/filling-holes-in-images\/"},"modified":"2019-10-23T13:43:38","modified_gmt":"2019-10-23T17:43:38","slug":"filling-holes-in-images","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2007\/07\/26\/filling-holes-in-images\/","title":{"rendered":"Filling holes in images"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p>Didja know? You can fill holes, or pits, in grayscale images by using the Image Processing Toolbox function <tt>imfill<\/tt>.\r\n   <\/p>\r\n   <p>The other day I was rereading the Tarboton paper on upslope area, trying to decide what to do next in my series on that topic. I noticed that the author described filling in \"pits\" in digital\r\n      elevation models (DEMs) as a common preprocessing step.  He outlined a method based on pixel flow directions, but didn't give\r\n      details.  I realized that this preprocessing step is very easy to do using the Image Processing Toolbox, but that many users\r\n      might not know about this capability.\r\n   <\/p>\r\n   <p>Let me first define a little more precisely what I mean by \"pit\" or \"hole,\" and then I'll show you how to fill it in.  It's\r\n      a little easier to show in one dimension, so let's start there.\r\n   <\/p>\r\n   <p>Here's a one-dimensional function:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">t = linspace(0, 3*pi, 100);\r\ny = t\/2 + sin(t);\r\nplot(t,y)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/155\/filling_holes_01.png\"> <p>The plot above has a local minimum in its interior. This is a hole, or pit.  If we are careful to define our connectivity\r\n      appropriately for one dimension, we can use <tt>imfill<\/tt> with the <tt>'holes'<\/tt> option to fill in the hole.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #228B22\">% Define a one-dimensional connectivity in the horizontal direction.<\/span>\r\nconn = [0 0 0; 1 1 1; 0 0 0];\r\ny2 = imfill(y, conn, <span style=\"color: #A020F0\">'holes'<\/span>);\r\n\r\nplot(t,y2)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/155\/filling_holes_02.png\"> <p>The filled curve has this key property: From any interior point, you can now travel to a boundary minimum without ever going\r\n      uphill. Here's what it looks for an image:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">I = imread(<span style=\"color: #A020F0\">'tire.tif'<\/span>);\r\nI2 = imfill(I,<span style=\"color: #A020F0\">'holes'<\/span>);  <span style=\"color: #228B22\">% the default connectivity is fine<\/span>\r\nsubplot(1,2,1)\r\nimshow(I)\r\ntitle(<span style=\"color: #A020F0\">'Original image'<\/span>)\r\nsubplot(1,2,2)\r\nimshow(I2)\r\ntitle(<span style=\"color: #A020F0\">'Filled image'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/155\/filling_holes_03.png\"> <p>The hole-filling algorithm in <tt>imfill<\/tt> is based on <i>morphological reconstruction<\/i> (<tt>imreconstruct<\/tt>). If you are interested in the theoretical details, see section 6.3.7 of <i>Morphological Image Analysis: Principles and Applications<\/i>, 2nd ed., Springer, by P. Soille.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_f79a95d4a8a549aaa510960d6cd520be() {\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='f79a95d4a8a549aaa510960d6cd520be ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' f79a95d4a8a549aaa510960d6cd520be';\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 2007 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_f79a95d4a8a549aaa510960d6cd520be()\"><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.4<br><\/p>\r\n<\/div>\r\n<!--\r\nf79a95d4a8a549aaa510960d6cd520be ##### SOURCE BEGIN #####\r\n%%\r\n% Didja know? You can fill holes, or pits, in grayscale images by using the\r\n% Image Processing Toolbox function |imfill|.\r\n%\r\n% The other day I was rereading the <http:\/\/www.engineering.usu.edu\/cee\/faculty\/dtarb\/96wr03137.pdf \r\n% Tarboton paper on upslope area>, trying\r\n% to decide what to do next in my series on that topic. I noticed that the\r\n% author described filling in \"pits\" in digital elevation models (DEMs) as a\r\n% common preprocessing step.  He outlined a method based on pixel flow\r\n% directions, but didn't give details.  I realized that this preprocessing\r\n% step is very easy to do using the Image Processing Toolbox, but that many\r\n% users might not know about this capability.\r\n%\r\n% Let me first define a little more precisely what I mean by \"pit\" or\r\n% \"hole,\" and then I'll show you how to fill it in.  It's a little easier\r\n% to show in one dimension, so let's start there.\r\n%\r\n% Here's a one-dimensional function:\r\n\r\nt = linspace(0, 3*pi, 100);\r\ny = t\/2 + sin(t);\r\nplot(t,y)\r\n\r\n%%\r\n% The plot above has a local minimum in its interior. This is a hole, or\r\n% pit.  If we are careful to define our connectivity appropriately for one\r\n% dimension, we can use |imfill| with the |'holes'| option to fill in the\r\n% hole.\r\n\r\n% Define a one-dimensional connectivity in the horizontal direction.\r\nconn = [0 0 0; 1 1 1; 0 0 0];\r\ny2 = imfill(y, conn, 'holes');\r\n\r\nplot(t,y2)\r\n\r\n%%\r\n% The filled curve has this key property: From any interior point, you can\r\n% now travel to a boundary minimum without ever going uphill. Here's what\r\n% it looks for an image:\r\n\r\nI = imread('tire.tif');\r\nI2 = imfill(I,'holes');  % the default connectivity is fine\r\nsubplot(1,2,1)\r\nimshow(I)\r\ntitle('Original image')\r\nsubplot(1,2,2)\r\nimshow(I2)\r\ntitle('Filled image')\r\n\r\n%%\r\n% The hole-filling algorithm in |imfill| is based on _morphological\r\n% reconstruction_ (|imreconstruct|). If you are interested in the\r\n% theoretical details, see section 6.3.7 of _Morphological Image Analysis:\r\n% Principles and Applications_, 2nd ed., Springer, by P. Soille.\r\n##### SOURCE END ##### f79a95d4a8a549aaa510960d6cd520be\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   Didja know? You can fill holes, or pits, in grayscale images by using the Image Processing Toolbox function imfill.\r\n   \r\n   The other day I was rereading the Tarboton paper on upslope area,... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2007\/07\/26\/filling-holes-in-images\/\">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":[136,76,36,32,68,72,52],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/155"}],"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=155"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/155\/revisions"}],"predecessor-version":[{"id":2250,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/155\/revisions\/2250"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=155"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=155"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=155"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}