{"id":147,"date":"2007-06-28T10:57:32","date_gmt":"2007-06-28T14:57:32","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/2007\/06\/28\/finding-pixels-adjacent-to-a-mask\/"},"modified":"2019-10-23T12:59:51","modified_gmt":"2019-10-23T16:59:51","slug":"finding-pixels-adjacent-to-a-mask","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2007\/06\/28\/finding-pixels-adjacent-to-a-mask\/","title":{"rendered":"Finding pixels adjacent to a mask"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p>Here's a quick tip.  A user question came in recently that involved a step of finding the pixels adjacent to foreground\r\n      pixels in a binary image.  Suppose you have a binary mask image, like this one:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">bw = imread(<span style=\"color: #A020F0\">'circles.png'<\/span>);\r\nimshow(bw)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/147\/mask_adjacent_01.png\"> <p>How can you find all the black pixels in <tt>bw<\/tt> that are immediately adjacent to a white pixel?  You can do this using <tt>imdilate<\/tt> and a logical operation.\r\n   <\/p>\r\n   <p>Use <tt>imdilate<\/tt> to \"grow\" the mask by one pixel:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">bw2 = imdilate(bw, ones(3,3));<\/pre><p>Now use a logical operation to find which pixels are white in <tt>bw2<\/tt> but black in <tt>bw<\/tt>:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">bw3 = bw2 &amp; ~bw;\r\nimshow(bw3)\r\ntitle(<span style=\"color: #A020F0\">'Adjacent pixels'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/147\/mask_adjacent_02.png\"> <p>Or do it in one step:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">adjacent_pixels = imdilate(bw, ones(3,3)) &amp; ~bw;<\/pre><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_6f10b94d56ae4a199ba9abc03d3359b1() {\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='6f10b94d56ae4a199ba9abc03d3359b1 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 6f10b94d56ae4a199ba9abc03d3359b1';\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_6f10b94d56ae4a199ba9abc03d3359b1()\"><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\n6f10b94d56ae4a199ba9abc03d3359b1 ##### SOURCE BEGIN #####\r\n%%\r\n% Here's a quick tip.  A customer question came in recently that involved a\r\n% step of finding the pixels adjacent to foreground pixels in a binary\r\n% image.  Suppose you have a binary mask image, like this one:\r\n\r\nbw = imread('circles.png');\r\nimshow(bw)\r\n\r\n%%\r\n% How can you find all the black pixels in |bw| that are immediately\r\n% adjacent to a white pixel?  You can do this using |imdilate| and a\r\n% logical operation.\r\n%\r\n% Use |imdilate| to \"grow\" the mask by one pixel:\r\n\r\nbw2 = imdilate(bw, ones(3,3));\r\n\r\n%%\r\n% Now use a logical operation to find which pixels are white in |bw2| but\r\n% black in |bw|:\r\n\r\nbw3 = bw2 & ~bw;\r\nimshow(bw3)\r\ntitle('Adjacent pixels')\r\n\r\n%%\r\n% Or do it in one step:\r\n\r\nadjacent_pixels = imdilate(bw, ones(3,3)) & ~bw;\r\n##### SOURCE END ##### 6f10b94d56ae4a199ba9abc03d3359b1\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   Here's a quick tip.  A user question came in recently that involved a step of finding the pixels adjacent to foreground\r\n      pixels in a binary image.  Suppose you have a binary mask image,... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2007\/06\/28\/finding-pixels-adjacent-to-a-mask\/\">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":[124,76,36,288,52],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/147"}],"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=147"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/147\/revisions"}],"predecessor-version":[{"id":3542,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/147\/revisions\/3542"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=147"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=147"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=147"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}