{"id":2127,"date":"2016-08-22T14:35:28","date_gmt":"2016-08-22T18:35:28","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/?p=2127"},"modified":"2019-11-01T16:46:49","modified_gmt":"2019-11-01T20:46:49","slug":"binary-image-area-filtering","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2016\/08\/22\/binary-image-area-filtering\/","title":{"rendered":"Binary image area filtering"},"content":{"rendered":"<div class=\"content\"><p>In my <a href=\"https:\/\/blogs.mathworks.com\/steve\/2016\/08\/08\/pokemon-go-meets-matlab\/\">August 8 post about Pokemon Go<\/a> (and I still can't quite believe that I did that), one of the processing steps was finding the centroid of the largest object in a binary image.<\/p><p>That reminded me of something that's been on my blog topic ideas list for a long time: filtering a binary image based on object size. Typically this operation is used as a kind of cleanup or preprocessing operation to remove small \"noise-like\" blobs.<\/p><p>Among the mathematical morphology folk, removing connected components that have an area smaller than some threshold is called <i>area opening<\/i>, and there's an Image Processing Toolbox function called <tt>bwareaopen<\/tt> that does it.<\/p><pre class=\"codeinput\">bw = imread(<span class=\"string\">'blobs.png'<\/span>);\r\nimshow(bw)\r\ntitle(<span class=\"string\">'Original'<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/area_filtering_01.png\" alt=\"\"> <p>Keep objects containing at least 10 pixels.<\/p><pre class=\"codeinput\">bw2 = bwareaopen(bw,10);\r\nimshow(bw2)\r\ntitle(<span class=\"string\">'Area opening'<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/area_filtering_02.png\" alt=\"\"> <p>Long-time blog readers might remember a <a href=\"https:\/\/blogs.mathworks.com\/steve\/2011\/09\/02\/area-opening-terminology-question\">post from five years ago<\/a> in which I invited feedback on the <i>area opening<\/i> terminology and the name of the function <tt>bwareaopen<\/tt>. I wondered in that post whether <i>area opening<\/i> was a little too jargony. Several people posted insightful comments.<\/p><p>Well, that discussion had an impact. A few release cycles later, in R2014b, the toolbox development team added a new function: <tt>bwareafilt<\/tt>. This function \"keeps\" a subset of objects in the binary image based on size. There are several ways to define the subset. Here are some examples:<\/p><p>Keep the 10 largest objects.<\/p><pre class=\"codeinput\">bw3 = bwareafilt(bw,10);\r\nimshow(bw3)\r\ntitle(<span class=\"string\">'10 largest objects'<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/area_filtering_03.png\" alt=\"\"> <p>Keep the 10 smallest objects.<\/p><pre class=\"codeinput\">bw4 = bwareafilt(bw,10,<span class=\"string\">'smallest'<\/span>);\r\nimshow(bw4)\r\ntitle(<span class=\"string\">'10 smallest objects'<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/area_filtering_04.png\" alt=\"\"> <p>Keep objects within a range of sizes.<\/p><pre class=\"codeinput\">bw5 = bwareafilt(bw,[20 50]);\r\nimshow(bw5)\r\ntitle(<span class=\"string\">'Size range: 20-50 pixels'<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/area_filtering_05.png\" alt=\"\"> <p>Keep objects with a minimum size by using Inf as the upper bound.<\/p><pre class=\"codeinput\">bw6 = bwareafilt(bw,[100 Inf]);\r\nimshow(bw6)\r\ntitle(<span class=\"string\">'Objects with at least 100 pixels'<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/area_filtering_06.png\" alt=\"\"> <p>Finally, let me show you a quick way to get a histogram of object sizes in an image.<\/p><pre class=\"codeinput\">bw_text = imread(<span class=\"string\">'text.png'<\/span>);\r\nimshow(bw_text)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/area_filtering_07.png\" alt=\"\"> <pre class=\"codeinput\">t = regionprops(<span class=\"string\">'table'<\/span>,bw_text,<span class=\"string\">'Area'<\/span>);\r\nfigure\r\nhistogram(t.Area)\r\ntitle(<span class=\"string\">'Object sizes in text image'<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/area_filtering_08.png\" alt=\"\"> <script language=\"JavaScript\"> <!-- \r\n    function grabCode_606f806ddcc249ba80acc4a598d88162() {\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='606f806ddcc249ba80acc4a598d88162 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 606f806ddcc249ba80acc4a598d88162';\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 2016 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_606f806ddcc249ba80acc4a598d88162()\"><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; R2016a<br><\/p><\/div><!--\r\n606f806ddcc249ba80acc4a598d88162 ##### SOURCE BEGIN #####\r\n%%\r\n% In my <https:\/\/blogs.mathworks.com\/steve\/2016\/08\/08\/pokemon-go-meets-matlab\/ \r\n% August 8 post about Pokemon Go> (and I still can't quite believe that I\r\n% did that), one of the processing steps was finding the centroid of the\r\n% largest object in a binary image.\r\n%\r\n% That reminded me of something that's been on my blog topic ideas list for\r\n% a long time: filtering a binary image based on object size. Typically\r\n% this operation is used as a kind of cleanup or preprocessing operation to\r\n% remove small \"noise-like\" blobs.\r\n%\r\n% Among the mathematical morphology folk, removing connected components\r\n% that have an area smaller than some threshold is called _area opening_,\r\n% and there's an Image Processing Toolbox function called |bwareaopen| that\r\n% does it.\r\n\r\nbw = imread('blobs.png');\r\nimshow(bw)\r\ntitle('Original')\r\n\r\n%%\r\n% Keep objects containing at least 10 pixels.\r\nbw2 = bwareaopen(bw,10);\r\nimshow(bw2)\r\ntitle('Area opening')\r\n\r\n%%\r\n% Long-time blog readers might remember a <https:\/\/blogs.mathworks.com\/steve\/2011\/09\/02\/area-opening-terminology-question \r\n% post from five years ago> in which\r\n% I invited feedback on the _area opening_ terminology and the name of the\r\n% function |bwareaopen|. I wondered in that post whether _area opening_ was\r\n% a little too jargony. Several people posted insightful comments.\r\n%\r\n% Well, that discussion had an impact. A new release cycles later, in\r\n% R2014b, the toolbox development team added a new function: |bwareafilt|.\r\n% This function \"keeps\" a subset of objects in the binary image based on\r\n% size. There are several ways to define the subset. Here are some\r\n% examples:\r\n%\r\n% Keep the 10 largest objects.\r\n\r\nbw3 = bwareafilt(bw,10);\r\nimshow(bw3)\r\ntitle('10 largest objects')\r\n\r\n%%\r\n% Keep the 10 smallest objects.\r\n\r\nbw4 = bwareafilt(bw,10,'smallest');\r\nimshow(bw4)\r\ntitle('10 smallest objects')\r\n\r\n%%\r\n% Keep objects within a range of sizes.\r\n\r\nbw5 = bwareafilt(bw,[20 50]);\r\nimshow(bw5)\r\ntitle('Size range: 20-50 pixels')\r\n\r\n%%\r\n% Keep objects with a minimum size by using Inf as the upper bound.\r\n\r\nbw6 = bwareafilt(bw,[100 Inf]);\r\nimshow(bw6)\r\ntitle('Objects with at least 100 pixels')\r\n\r\n%%\r\n% Finally, let me show you a quick way to get a histogram of object sizes\r\n% in an image.\r\n\r\nbw_text = imread('text.png');\r\nimshow(bw_text)\r\n\r\n%%\r\nt = regionprops('table',bw_text,'Area');\r\nfigure\r\nhistogram(t.Area)\r\ntitle('Object sizes in text image')\r\n\r\n##### SOURCE END ##### 606f806ddcc249ba80acc4a598d88162\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img src=\"https:\/\/blogs.mathworks.com\/steve\/files\/area_filtering_06.png\" class=\"img-responsive attachment-post-thumbnail size-post-thumbnail wp-post-image\" alt=\"\" decoding=\"async\" loading=\"lazy\" \/><\/div><p>In my August 8 post about Pokemon Go (and I still can't quite believe that I did that), one of the processing steps was finding the centroid of the largest object in a binary image.That reminded me... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2016\/08\/22\/binary-image-area-filtering\/\">read more >><\/a><\/p>","protected":false},"author":42,"featured_media":2134,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[1167,138,725,1169,76,36,168,52],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/2127"}],"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=2127"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/2127\/revisions"}],"predecessor-version":[{"id":2137,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/2127\/revisions\/2137"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media\/2134"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=2127"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=2127"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=2127"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}