{"id":116,"date":"2007-02-24T11:49:17","date_gmt":"2007-02-24T16:49:17","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/?p=116"},"modified":"2019-10-23T08:42:34","modified_gmt":"2019-10-23T12:42:34","slug":"roipoly-rectangular-regions-and-logical-indexing","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2007\/02\/24\/roipoly-rectangular-regions-and-logical-indexing\/","title":{"rendered":"ROIPOLY &#8211; rectangular regions and logical indexing"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>Several readers commented on my recent <tt>roipoly<\/tt> and <tt>poly2mask<\/tt> posts, and I wanted to follow up on a couple of issues they raised.  The first issue is a common point of confusion about\r\n         the selection of rectangular regions, and the second is how to access and modify pixels contained within a region of interest.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">Rectangular regions<\/a><\/li>\r\n         <li><a href=\"#4\">Logical indexing<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Rectangular regions<a name=\"1\"><\/a><\/h3>\r\n   <p>Users sometimes wonder why <tt>poly2mask<\/tt> and <tt>roipoly<\/tt> don't return some of the boundary pixels of a rectangular region as part of the mask.  Here's an example.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">x = [2 4 4 2 2];\r\ny = [2 2 4 4 2];\r\n\r\nmask = poly2mask(x, y, 5, 5)<\/pre><pre style=\"font-style:oblique\">\r\nmask =\r\n\r\n     0     0     0     0     0\r\n     0     0     0     0     0\r\n     0     0     1     1     0\r\n     0     0     1     1     0\r\n     0     0     0     0     0\r\n\r\n<\/pre><p>Some users expect rows 2 through 4 and columns 2 through 4 to be included in the mask.  One reader commented that he uses\r\n      a different function instead of <tt>roipoly<\/tt> because of this behavior.\r\n   <\/p>\r\n   <p>As I have illustrated in my previous posts, <tt>roipoly<\/tt> and <tt>poly2mask<\/tt> treat a pixel not as a point, but as a unit square area.  The upper-left corner of the (2,2) pixel is (1.5,1.5), and the\r\n      lower-right corner is (2.5,2.5).  So the polygon defined by the x-y vertices in the example above covers only one-fourth of\r\n      the (2,2) pixel.\r\n   <\/p>\r\n   <p>I suggest that you get used to defining rectangles that cover complete pixel areas.  Such rectangles go along pixel edges,\r\n      not through pixel centers.  Here's a second example to demonstrate:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">xp = [1.5 4.5 4.5 1.5 1.5];\r\nyp = [1.5 1.5 4.5 4.5 1.5];\r\n\r\nmaskp = poly2mask(xp, yp, 5, 5)<\/pre><pre style=\"font-style:oblique\">\r\nmaskp =\r\n\r\n     0     0     0     0     0\r\n     0     1     1     1     0\r\n     0     1     1     1     0\r\n     0     1     1     1     0\r\n     0     0     0     0     0\r\n\r\n<\/pre><p>Now the \"mystery\" is completely removed about which border rows and columns are in the mask.  There's no need to use some\r\n      other function.\r\n   <\/p>\r\n   <h3>Logical indexing<a name=\"4\"><\/a><\/h3>\r\n   <p>Another reader asked how to get and modify pixels inside a region of interest.  One nice way to do that in MATLAB is to use\r\n      <i>logical indexing<\/i>.  The expression <tt>A(B)<\/tt>, if <tt>B<\/tt> is logical and the same size as <tt>A<\/tt>, selects all the elements in <tt>A<\/tt> corresponding to the <tt>true<\/tt> elements of <tt>B<\/tt>.\r\n   <\/p>\r\n   <p>I'll demonstrate using an example mask from the roipoly documentation.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">clear\r\nI = imread(<span style=\"color: #A020F0\">'eight.tif'<\/span>);\r\nimshow(I)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/116\/roipoly_followup_01.jpg\"> <pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">c = [222 272 300 270 221 194];\r\nr = [21 21 75 121 121 75];\r\nmask = roipoly(I,c,r);\r\nimshow(mask)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/116\/roipoly_followup_02.jpg\"> <p>If you use <tt>whos<\/tt> or look in the workspace browser, you can see that <tt>mask<\/tt> is a logical matrix.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">whos<\/pre><pre style=\"font-style:oblique\">  Name        Size             Bytes  Class      Attributes\r\n\r\n  I         242x308            74536  uint8                \r\n  c           1x6                 48  double               \r\n  mask      242x308            74536  logical              \r\n  r           1x6                 48  double               \r\n\r\n<\/pre><p>The expression <tt>I(mask)<\/tt> is a column vector containing all the elements of <tt>I<\/tt> corresponding to the <tt>true<\/tt> values of <tt>mask<\/tt>.  Here, for example, is how you would compute the mean of the pixels inside the region of interest:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">mean(I(mask))<\/pre><pre style=\"font-style:oblique\">\r\nans =\r\n\r\n  156.0468\r\n\r\n<\/pre><p>You can use logical indexing on the left-hand side of an assignment as well.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">I2 = I;\r\nI2(mask) = 255 - I(mask);  <span style=\"color: #228B22\">% Or imcomplement(I(mask)).<\/span>\r\nimshow(I2)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/116\/roipoly_followup_03.jpg\"> <p>Here's a <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/math\/matrix-indexing.html#bq7egb6-1\">documentation link<\/a> for logical indexing.  Or you might be interested in this <a href=\"https:\/\/www.mathworks.com\/company\/newsletters\/articles\/matrix-indexing-in-matlab.html\">MATLAB Digest article<\/a> that <a href=\"https:\/\/blogs.mathworks.com\/loren\">Loren<\/a> and I wrote about MATLAB indexing.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_7432ae5423bd425d852effe835358e71() {\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='7432ae5423bd425d852effe835358e71 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 7432ae5423bd425d852effe835358e71';\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_7432ae5423bd425d852effe835358e71()\"><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.3<br><\/p>\r\n<\/div>\r\n<!--\r\n7432ae5423bd425d852effe835358e71 ##### SOURCE BEGIN #####\r\n%%\r\n% Several readers commented on my recent |roipoly| and |poly2mask| posts, and I\r\n% wanted to follow up on a couple of issues they raised.  The first issue\r\n% is a common point of confusion about the selection of rectangular\r\n% regions, and the second is how to access and modify pixels contained\r\n% within a region of interest.\r\n\r\n%% Rectangular regions\r\n% Users sometimes wonder why |poly2mask| and |roipoly| don't return some of the\r\n% boundary pixels of a rectangular region as part of the mask.  Here's an\r\n% example.\r\n\r\n\r\nx = [2 4 4 2 2];\r\ny = [2 2 4 4 2];\r\n\r\nmask = poly2mask(x, y, 5, 5)\r\n\r\n%%\r\n% Some users expect rows 2 through 4 and columns 2 through 4 to be included\r\n% in the mask.  One reader commented that he uses a different function\r\n% instead of |roipoly| because of this behavior.\r\n%\r\n% As I have illustrated in my previous posts, |roipoly| and |poly2mask| \r\n% treat a pixel not\r\n% as a point, but as a unit square area.  The upper-left corner of the\r\n% (2,2) pixel is (1.5,1.5), and the lower-right corner is (2.5,2.5).  So\r\n% the polygon defined by the x-y vertices in the example above covers only\r\n% one-fourth of the (2,2) pixel. \r\n%\r\n% I suggest that you get used to defining rectangles that cover complete\r\n% pixel areas.  Such rectangles go along pixel edges, not through pixel\r\n% centers.  Here's a second example to demonstrate:\r\n\r\nxp = [1.5 4.5 4.5 1.5 1.5];\r\nyp = [1.5 1.5 4.5 4.5 1.5];\r\n\r\nmaskp = poly2mask(xp, yp, 5, 5)\r\n\r\n%%\r\n% Now the \"mystery\" is completely removed about which border rows and \r\n% columns are in the mask.  There's no need to use some other function.\r\n\r\n%% Logical indexing\r\n% Another reader asked how to get and modify pixels inside a region of\r\n% interest.  One nice way to do that in MATLAB is to use _logical\r\n% indexing_.  The expression |A(B)|, if |B| is logical and the same size as\r\n% |A|, selects all the elements in |A| corresponding to the |true| elements\r\n% of |B|. \r\n%\r\n% I'll demonstrate using an example mask from the roipoly documentation.\r\n\r\nclear\r\nI = imread('eight.tif');\r\nimshow(I)\r\n\r\n%%\r\n\r\nc = [222 272 300 270 221 194];\r\nr = [21 21 75 121 121 75];\r\nmask = roipoly(I,c,r);\r\nimshow(mask)\r\n\r\n%%\r\n% If you use |whos| or look in the workspace browser, you can see that\r\n% |mask| is a logical matrix.\r\n\r\nwhos\r\n\r\n%%\r\n% The expression |I(mask)| is a column vector containing all the elements\r\n% of |I| corresponding to the |true| values of |mask|.  Here, for example,\r\n% is how you would compute the mean of the pixels inside the region of\r\n% interest:\r\n\r\nmean(I(mask))\r\n\r\n%%\r\n% You can use logical indexing on the left-hand side of an assignment as\r\n% well.\r\n\r\nI2 = I;\r\nI2(mask) = 255 - I(mask);  % Or imcomplement(I(mask)).\r\nimshow(I2)\r\n\r\n%%\r\n% Here's a <https:\/\/www.mathworks.com\/access\/helpdesk\/help\/techdoc\/matlab_prog\/f2-63872.html#f2-49079 \r\n% documentation link> for logical indexing.  Or you might be\r\n% interested in this <https:\/\/www.mathworks.com\/company\/newsletters\/articles\/matrix-indexing-in-matlab.html\r\n% MATLAB Digest article> that <https:\/\/blogs.mathworks.com\/loren Loren> \r\n% and I wrote about\r\n% MATLAB indexing.\r\n\r\n##### SOURCE END ##### 7432ae5423bd425d852effe835358e71\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      Several readers commented on my recent roipoly and poly2mask posts, and I wanted to follow up on a couple of issues they raised.  The first issue is a common point of confusion about\r\n  ... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2007\/02\/24\/roipoly-rectangular-regions-and-logical-indexing\/\">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":[302,76,36,308,296,304,306],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/116"}],"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=116"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/116\/revisions"}],"predecessor-version":[{"id":2219,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/116\/revisions\/2219"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=116"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=116"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=116"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}