{"id":358,"date":"2011-01-07T18:18:58","date_gmt":"2011-01-07T23:18:58","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/2011\/01\/07\/feature-and\/"},"modified":"2019-10-29T13:51:01","modified_gmt":"2019-10-29T17:51:01","slug":"feature-and","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2011\/01\/07\/feature-and\/","title":{"rendered":"Feature AND"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p>I'm going to take a break from <a href=\"https:\/\/blogs.mathworks.com\/steve\/2010\/12\/30\/a-and-b\/\">looking at M&amp;Ms<\/a> this week and instead consider a question that <a href=\"https:\/\/blogs.mathworks.com\/steve\/2009\/02\/27\/using-ismember-with-the-output-of-regionprops\/#comment-23830\">Richard asked yesterday<\/a>:\r\n   <\/p>\r\n   <p>\"I have two binary images of the same size. I want to select regions from image one, where that region has to partially overlap\r\n      with a region from image two.\"\r\n   <\/p>\r\n   <p>I've seen this called the \"Feature-AND\" operation, and you can use the Image Processing Toolbox function <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/toolbox\/images\/ref\/imreconstruct.html\"><tt>imreconstruct<\/tt><\/a> to do it. I'll use one of the toolbox sample images to show you how.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">bw = imread(<span style=\"color: #A020F0\">'text.png'<\/span>);\r\nimshow(bw)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2011\/feature_and_01.png\"> <p>Suppose I've got a \"marker\" image that has a thick line running down the diagonal, like this:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">stripe = logical(eye(256));\r\nstripe = imdilate(stripe, ones(3,3));\r\nimshow(stripe)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2011\/feature_and_02.png\"> <p>Now ask the question, \"Which objects in the original image touch the line?\" It's easy to use a simple elementwise AND to see\r\n      which <b>pixels<\/b> in the original image touch the line:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">touching_pixels = bw &amp; stripe;\r\nimshow(touching_pixels)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2011\/feature_and_03.png\"> <p>Now to get the entire objects that are touching the line, apply an operation called <i>morphological reconstruction<\/i>, which is what <tt>imreconstruct<\/tt> does. This function takes two inputs, a marker image and a mask image. Use the touching pixels as the marker image, and use\r\n      the original image as the mask image.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">marker = touching_pixels;\r\nmask = bw;\r\nout = imreconstruct(marker, mask);\r\nimshow(out)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2011\/feature_and_04.png\"> <p>Neat!<\/p>\r\n   <p>Here's another variation: \"Which objects in the original image contain a vertical stroke that's at least 11 pixels tall?\"<\/p>\r\n   <p>Start by eroding the original image by a vertical line structuring element.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">se = strel(ones(11,1));\r\nbw_eroded = imerode(bw, se);\r\nimshow(bw_eroded)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2011\/feature_and_05.png\"> <p>You can see that many characters are gone completely. If we define the pixels that are left as the marker image, then reconstruction\r\n      gives us back the complete objects:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">marker = bw_eroded;\r\nout = imreconstruct(marker, mask);\r\nimshow(out)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2011\/feature_and_06.png\"> <p>This is a very useful operation, and I recommend that you add it to your bag of image processing tricks.<\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_88cf541db7314309b41cda2bdb9856bb() {\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='88cf541db7314309b41cda2bdb9856bb ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 88cf541db7314309b41cda2bdb9856bb';\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_88cf541db7314309b41cda2bdb9856bb()\"><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.11<br><\/p>\r\n<\/div>\r\n<!--\r\n88cf541db7314309b41cda2bdb9856bb ##### SOURCE BEGIN #####\r\n%%\r\n% I'm going to take a break from <https:\/\/blogs.mathworks.com\/steve\/2010\/12\/30\/a-and-b\/ \r\n% looking at M&Ms> this week and instead\r\n% consider a question that\r\n% <https:\/\/blogs.mathworks.com\/steve\/2009\/02\/27\/using-ismember-with-the-output-of-regionprops\/#comment-23830\r\n% Richard asked yesterday>:\r\n%\r\n% \"I have two binary images of the same size. I want to select regions from\r\n% image one, where that region has to partially overlap with a region from\r\n% image two.\"\r\n%\r\n% I've seen this called the \"Feature-AND\" operation, and you can use the\r\n% Image Processing Toolbox function\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2010b\/toolbox\/images\/ref\/imreconstruct.html\r\n% |imreconstruct|> to do it. I'll use one of the toolbox sample images to\r\n% show you how.\r\n\r\nbw = imread('text.png');\r\nimshow(bw)\r\n\r\n%%\r\n% Suppose I've got a \"marker\" image that has a thick line running down the\r\n% diagonal, like this:\r\n\r\nstripe = logical(eye(256));\r\nstripe = imdilate(stripe, ones(3,3));\r\nimshow(stripe)\r\n\r\n%%\r\n% Now ask the question, \"Which objects in the original image touch the\r\n% line?\" It's easy to use a simple elementwise AND to see which *pixels* in\r\n% the original image touch the line:\r\n\r\ntouching_pixels = bw & stripe;\r\nimshow(touching_pixels)\r\n\r\n%%\r\n% Now to get the entire objects that are touching the line, apply an\r\n% operation called _morphological reconstruction_, which is what\r\n% |imreconstruct| does. This function takes two inputs, a marker image and\r\n% a mask image. Use the touching pixels as the marker image, and use the\r\n% original image as the mask image.\r\n\r\nmarker = touching_pixels;\r\nmask = bw;\r\nout = imreconstruct(marker, mask);\r\nimshow(out)\r\n\r\n%%\r\n% Neat!\r\n%\r\n% Here's another variation: \"Which objects in the original image contain a\r\n% vertical stroke that's at least 11 pixels tall?\"\r\n%\r\n% Start by eroding the original image by a vertical line structuring\r\n% element.\r\n\r\nse = strel(ones(11,1));\r\nbw_eroded = imerode(bw, se);\r\nimshow(bw_eroded)\r\n\r\n%%\r\n% You can see that many characters are gone completely. If we define the\r\n% pixels that are left as the marker image, then reconstruction gives us\r\n% back the complete objects:\r\n\r\nmarker = bw_eroded;\r\nout = imreconstruct(marker, mask);\r\nimshow(out)\r\n\r\n%%\r\n% This is a very useful operation, and I recommend that you add it to your\r\n% bag of image processing tricks.\r\n##### SOURCE END ##### 88cf541db7314309b41cda2bdb9856bb\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   I'm going to take a break from looking at M&amp;Ms this week and instead consider a question that Richard asked yesterday:\r\n   \r\n   \"I have two binary images of the same size. I want to select... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2011\/01\/07\/feature-and\/\">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":[300,124,246,76,438,36,330,288,106],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/358"}],"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=358"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/358\/revisions"}],"predecessor-version":[{"id":3715,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/358\/revisions\/3715"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=358"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=358"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=358"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}