{"id":222,"date":"2008-08-05T07:00:40","date_gmt":"2008-08-05T11:00:40","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/2008\/08\/05\/filling-small-holes\/"},"modified":"2019-10-28T09:38:56","modified_gmt":"2019-10-28T13:38:56","slug":"filling-small-holes","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2008\/08\/05\/filling-small-holes\/","title":{"rendered":"Filling small holes"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p>A MATLAB user recently asked in the <a>MATLAB newsgroup<\/a> how to fill \"small\" holes in a binary image. The function <tt>imfill<\/tt> can be used to fill all holes, but this user only wanted to fill holes having an area smaller than some threshold.\r\n   <\/p>\r\n   <p>That's an interesting question.  It can be done using a combination of <tt>imfill<\/tt>, <tt>bwareaopen<\/tt>, and MATLAB logical operators. Here's how.\r\n   <\/p>\r\n   <p>Step 1: Fill all holes using imfill:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">original = imread(<span style=\"color: #A020F0\">'circbw.tif'<\/span>);\r\nimshow(original)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2008\/fill_small_holes_01.png\"> <pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">filled = imfill(original, <span style=\"color: #A020F0\">'holes'<\/span>);\r\nimshow(filled)\r\ntitle(<span style=\"color: #A020F0\">'All holes filled'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2008\/fill_small_holes_02.png\"> <p>Step 2: Identify the hole pixels using logical operators:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">holes = filled &amp; ~original;\r\nimshow(holes)\r\ntitle(<span style=\"color: #A020F0\">'Hole pixels identified'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2008\/fill_small_holes_03.png\"> <p>Step 3: Use bwareaopen on the <tt>holes<\/tt> image to eliminate small holes:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">bigholes = bwareaopen(holes, 200);\r\nimshow(bigholes)\r\ntitle(<span style=\"color: #A020F0\">'Only the big holes'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2008\/fill_small_holes_04.png\"> <p>Step 4: Use logical operators to identify small holes:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">smallholes = holes &amp; ~bigholes;\r\nimshow(smallholes)\r\ntitle(<span style=\"color: #A020F0\">'Only the small holes'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2008\/fill_small_holes_05.png\"> <p>Step 5: Use a logical operator to fill in the small holes in the original image:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">new = original | smallholes;\r\nimshow(new)\r\ntitle(<span style=\"color: #A020F0\">'Small holes filled'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2008\/fill_small_holes_06.png\"> <p>All done!<\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_d8b553ee06c54e2da47d3767d8caa33d() {\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='d8b553ee06c54e2da47d3767d8caa33d ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' d8b553ee06c54e2da47d3767d8caa33d';\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\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_d8b553ee06c54e2da47d3767d8caa33d()\"><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.6<br><\/p>\r\n<\/div>\r\n<!--\r\nd8b553ee06c54e2da47d3767d8caa33d ##### SOURCE BEGIN #####\r\n%%\r\n% A MATLAB user recently asked in the \r\n% <http:\/\/view_thread\/173142#445146 \r\n% MATLAB newsgroup> how to fill \"small\" holes in a binary image.\r\n% The function |imfill| can be used to fill all holes, but this user\r\n% only wanted to fill holes having an area smaller than some\r\n% threshold.\r\n%\r\n% That's an interesting question.  It can be done using a\r\n% combination of |imfill|, |bwareaopen|, and MATLAB logical\r\n% operators. Here's how.\r\n%\r\n% Step 1: Fill all holes using imfill:\r\n\r\noriginal = imread('circbw.tif');\r\nimshow(original)\r\n\r\n%%\r\n\r\nfilled = imfill(original, 'holes');\r\nimshow(filled)\r\ntitle('All holes filled')\r\n\r\n%%\r\n% Step 2: Identify the hole pixels using logical operators:\r\n\r\nholes = filled & ~original;\r\nimshow(holes)\r\ntitle('Hole pixels identified')\r\n\r\n%%\r\n% Step 3: Use bwareaopen on the |holes| image to eliminate small\r\n% holes:\r\n\r\nbigholes = bwareaopen(holes, 200);\r\nimshow(bigholes)\r\ntitle('Only the big holes')\r\n\r\n%% \r\n% Step 4: Use logical operators to identify small holes:\r\n\r\nsmallholes = holes & ~bigholes;\r\nimshow(smallholes)\r\ntitle('Only the small holes')\r\n\r\n%%\r\n% Step 5: Use a logical operator to fill in the small holes in the\r\n% original image:\r\n\r\nnew = original | smallholes;\r\nimshow(new)\r\ntitle('Small holes filled')\r\n\r\n%%\r\n% All done!\r\n\r\n\r\n##### SOURCE END ##### d8b553ee06c54e2da47d3767d8caa33d\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   A MATLAB user recently asked in the MATLAB newsgroup how to fill \"small\" holes in a binary image. The function imfill can be used to fill all holes, but this user only wanted to fill holes... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2008\/08\/05\/filling-small-holes\/\">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":[138,136,76,36,52],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/222"}],"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=222"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/222\/revisions"}],"predecessor-version":[{"id":2746,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/222\/revisions\/2746"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=222"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=222"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=222"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}