{"id":375,"date":"2011-07-08T03:38:21","date_gmt":"2011-07-08T07:38:21","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/2011\/07\/08\/binary-image-hit-miss-operator\/"},"modified":"2019-10-29T16:44:01","modified_gmt":"2019-10-29T20:44:01","slug":"binary-image-hit-miss-operator","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2011\/07\/08\/binary-image-hit-miss-operator\/","title":{"rendered":"Binary image hit-miss operator"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p>A few months ago, MATLAB user Julia asked how to \"fill 4-connected pixels exclusively\" on <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/answers\/4445-fill-4-connected-cells-exclusively\">MATLAB Answers<\/a>.  Here's a more precise statement of the problem: if a zero-valued pixel in a binary image has one-valued north, east, south,\r\n      and west neighbors, then change that zero-valued pixel to a one-valued pixel.\r\n   <\/p>\r\n   <p>I suggested <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011a\/toolbox\/images\/ref\/bwhitmiss.html\"><tt>bwhitmiss<\/tt><\/a> in my answer. The function <tt>bwhitmiss<\/tt> is a very useful tool for searching for specific pixel neighborhood configurations in a binary image. Here's how it works.\r\n   <\/p>\r\n   <p>First, create one structuring element (as a matrix of zeros and ones) representing neighborhood pixels you want to \"hit.\"\r\n      For example, if you want the north, east, south, and west neighbors in a 3-by-3 neighborhood to be one-valued, use this structuring\r\n      element:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">se1 = [0 1 0\r\n       1 0 1\r\n       0 1 0]<\/pre><pre style=\"font-style:oblique\">\r\nse1 =\r\n\r\n     0     1     0\r\n     1     0     1\r\n     0     1     0\r\n\r\n<\/pre><p>Now create a second structuring element representing neighborhood pixels you want to \"miss.\" For example, if you want the\r\n      center pixel of a 3-by-3 neighborhood to be zero-valued, use this structuring element:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">se2 = [0 0 0\r\n       0 1 0\r\n       0 0 0]<\/pre><pre style=\"font-style:oblique\">\r\nse2 =\r\n\r\n     0     0     0\r\n     0     1     0\r\n     0     0     0\r\n\r\n<\/pre><p>Then you'd pass your binary image and both of these structuring elements to <tt>bwhitmiss<\/tt>. Let's construct a small test image to experiment with.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">bw = [1 1 0 0 0 0 0\r\n      1 0 1 0 0 1 0\r\n      0 1 0 0 0 0 0\r\n      0 0 0 0 0 0 0\r\n      1 1 1 0 0 1 1\r\n      1 0 1 0 1 0 1\r\n      1 1 1 0 1 0 1]<\/pre><pre style=\"font-style:oblique\">\r\nbw =\r\n\r\n     1     1     0     0     0     0     0\r\n     1     0     1     0     0     1     0\r\n     0     1     0     0     0     0     0\r\n     0     0     0     0     0     0     0\r\n     1     1     1     0     0     1     1\r\n     1     0     1     0     1     0     1\r\n     1     1     1     0     1     0     1\r\n\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">pixel_matches = bwhitmiss(bw,se1,se2)<\/pre><pre style=\"font-style:oblique\">\r\npixel_matches =\r\n\r\n     0     0     0     0     0     0     0\r\n     0     1     0     0     0     0     0\r\n     0     0     0     0     0     0     0\r\n     0     0     0     0     0     0     0\r\n     0     0     0     0     0     0     0\r\n     0     1     0     0     0     0     0\r\n     0     0     0     0     0     0     0\r\n\r\n<\/pre><p>You can see that <tt>bwhitmiss<\/tt> found two places where a zero-valued pixel had one-valued north, east, south, and west neighbors. To modify the original\r\n      image by turning these zero-valued pixels into one-valued pixels, use an elementwise OR operator:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">bw2 = bw | pixel_matches<\/pre><pre style=\"font-style:oblique\">\r\nbw2 =\r\n\r\n     1     1     0     0     0     0     0\r\n     1     1     1     0     0     1     0\r\n     0     1     0     0     0     0     0\r\n     0     0     0     0     0     0     0\r\n     1     1     1     0     0     1     1\r\n     1     1     1     0     1     0     1\r\n     1     1     1     0     1     0     1\r\n\r\n<\/pre><p>Sometimes you'll see references to \"don't care\" neighbors in discussions about the hit-miss operator.  These are neighborhood\r\n      pixels that can be either zero or one without affecting the match. In terms of the inputs to <tt>bwhitmiss<\/tt>, the \"don't care\" neighbors are the ones where neither <tt>se1<\/tt> nor <tt>se1<\/tt> is equal to one.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">dont_care_neighbors = ~(se1 | se2)<\/pre><pre style=\"font-style:oblique\">\r\ndont_care_neighbors =\r\n\r\n     1     0     1\r\n     0     0     0\r\n     1     0     1\r\n\r\n<\/pre><p>Here's one more example to finish with. Let's use <tt>bwhitmiss<\/tt> to identify isolated pixels. These are one-valued pixels that are completely surrounded by zero-pixels.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">se1 = [0 0 0\r\n       0 1 0\r\n       0 0 0];\r\n\r\nse2 = [1 1 1\r\n       1 0 1\r\n       1 1 1];\r\n\r\nbw3 = bwhitmiss(bw,se1,se2)<\/pre><pre style=\"font-style:oblique\">\r\nbw3 =\r\n\r\n     0     0     0     0     0     0     0\r\n     0     0     0     0     0     1     0\r\n     0     0     0     0     0     0     0\r\n     0     0     0     0     0     0     0\r\n     0     0     0     0     0     0     0\r\n     0     0     0     0     0     0     0\r\n     0     0     0     0     0     0     0\r\n\r\n<\/pre><p>There was only one isolated pixel in our little sample image.<\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_f00d0574a88b4ef099b0c289e16c54bd() {\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='f00d0574a88b4ef099b0c289e16c54bd ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' f00d0574a88b4ef099b0c289e16c54bd';\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 2011 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_f00d0574a88b4ef099b0c289e16c54bd()\"><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.12<br><\/p>\r\n<\/div>\r\n<!--\r\nf00d0574a88b4ef099b0c289e16c54bd ##### SOURCE BEGIN #####\r\n%%\r\n% A few months ago, MATLAB user Julia asked how to \"fill 4-connected pixels\r\n% exclusively\" on\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/answers\/4445-fill-4-connected-cells-exclusively\r\n% MATLAB Answers>.  Here's a more precise statement of the problem: if a\r\n% zero-valued pixel in a binary image has one-valued north, east, south,\r\n% and west neighbors, then change that zero-valued pixel to a one-valued\r\n% pixel.\r\n%\r\n% I suggested\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2011a\/toolbox\/images\/ref\/bwhitmiss.html\r\n% |bwhitmiss|> in my answer. The function |bwhitmiss| is a very useful tool\r\n% for searching for specific pixel neighborhood configurations in a binary\r\n% image. Here's how it works.\r\n%\r\n% First, create one structuring element (as a matrix of zeros and ones)\r\n% representing neighborhood pixels you want to \"hit.\" For example, if you\r\n% want the north, east, south, and west neighbors in a 3-by-3 neighborhood\r\n% to be one-valued, use this structuring element:\r\n\r\nse1 = [0 1 0\r\n       1 0 1\r\n       0 1 0]\r\n\r\n%%\r\n% Now create a second structuring element representing neighborhood pixels\r\n% you want to \"miss.\" For example, if you want the center pixel of a 3-by-3\r\n% neighborhood to be zero-valued, use this structuring element:\r\n\r\nse2 = [0 0 0\r\n       0 1 0\r\n       0 0 0]\r\n   \r\n%%\r\n% Then you'd pass your binary image and both of these structuring elements\r\n% to |bwhitmiss|. Let's construct a small test image to experiment with.\r\n\r\nbw = [1 1 0 0 0 0 0\r\n      1 0 1 0 0 1 0\r\n      0 1 0 0 0 0 0\r\n      0 0 0 0 0 0 0\r\n      1 1 1 0 0 1 1\r\n      1 0 1 0 1 0 1\r\n      1 1 1 0 1 0 1]\r\n  \r\n%%\r\n\r\npixel_matches = bwhitmiss(bw,se1,se2)\r\n\r\n%%\r\n% You can see that |bwhitmiss| found two places where a zero-valued pixel\r\n% had one-valued north, east, south, and west neighbors. To modify the\r\n% original image by turning these zero-valued pixels into one-valued\r\n% pixels, use an elementwise OR operator:\r\n\r\nbw2 = bw | pixel_matches\r\n\r\n%%\r\n% Sometimes you'll see references to \"don't care\" neighbors in discussions\r\n% about the hit-miss operator.  These are neighborhood pixels that can be\r\n% either zero or one without affecting the match. In terms of the inputs to\r\n% |bwhitmiss|, the \"don't care\" neighbors are the ones where neither |se1|\r\n% nor |se1| is equal to one.\r\n\r\ndont_care_neighbors = ~(se1 | se2)\r\n\r\n%%\r\n% Here's one more example to finish with. Let's use |bwhitmiss| to identify\r\n% isolated pixels. These are one-valued pixels that are completely\r\n% surrounded by zero-pixels.\r\n\r\nse1 = [0 0 0\r\n       0 1 0\r\n       0 0 0];\r\n   \r\nse2 = [1 1 1\r\n       1 0 1\r\n       1 1 1];\r\n   \r\nbw3 = bwhitmiss(bw,se1,se2)\r\n\r\n%%\r\n% There was only one isolated pixel in our little sample image.\r\n\r\n##### SOURCE END ##### f00d0574a88b4ef099b0c289e16c54bd\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   A few months ago, MATLAB user Julia asked how to \"fill 4-connected pixels exclusively\" on MATLAB Answers.  Here's a more precise statement of the problem: if a zero-valued pixel in a binary... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2011\/07\/08\/binary-image-hit-miss-operator\/\">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":[520],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/375"}],"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=375"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/375\/revisions"}],"predecessor-version":[{"id":3735,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/375\/revisions\/3735"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=375"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=375"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=375"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}