{"id":99,"date":"2006-12-05T10:09:21","date_gmt":"2006-12-05T15:09:21","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/?p=99"},"modified":"2019-10-22T16:36:41","modified_gmt":"2019-10-22T20:36:41","slug":"poly2mask-and-roipoly-part-1","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2006\/12\/05\/poly2mask-and-roipoly-part-1\/","title":{"rendered":"POLY2MASK and ROIPOLY &#8211; Part 1"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p>This is the first of several blogs about the algorithm behind <tt>poly2mask<\/tt> and <tt>roipoly<\/tt>.  <tt>poly2mask<\/tt> takes a set of polygon vertices and produces a binary mask image.  The binary mask has 1s for pixels inside the polygon and\r\n      0s for pixels outside.\r\n   <\/p>\r\n   <p>Here's a quick peek at what it does.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">x1 = [40 225 160 40];\r\ny1 = [230 170 45 230];\r\nmask = poly2mask(x1, y1, 300, 300);\r\nimshow(mask)\r\nhold <span style=\"color: #A020F0\">on<\/span>\r\nplot(x1,y1,<span style=\"color: #A020F0\">'LineWidth'<\/span>,3,<span style=\"color: #A020F0\">'Color'<\/span>,<span style=\"color: #A020F0\">'r'<\/span>);\r\nhold <span style=\"color: #A020F0\">off<\/span><\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/99\/roipoly1_01.png\"> <p>Earlier versions of the Image Processing Toolbox just had <tt>roipoly<\/tt>. It contained both the core algorithm as well as a simple GUI for specifying the polygon vertices with mouse clicks.  A few\r\n      releases ago, we separated the core algorithm into its own function, <tt>poly2mask<\/tt>.  Now <tt>roipoly<\/tt> handles image display and grabbing mouse clicks, and it calls <tt>poly2mask<\/tt> for the computation.\r\n   <\/p>\r\n   <p>You can see that the computation might be nontrivial if you use a more complicated polygon, and if you zoom in a bit so you\r\n      can see what's going on along the polygon edges.  My second example computes a 25-by-25 mask, displays the mask using high\r\n      magnification, and displays a gray grid showing the edges of the pixels.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">x2 = [10 24 5 15.5 10];\r\ny2 = [2 5 15 20.5 2];\r\nM = 25;\r\nN = 25;\r\n\r\nmask = poly2mask(x2,y2,25,25);\r\nmap = [255 255 255; 218 157 30] \/ 255;\r\nimshow(mask,map,<span style=\"color: #A020F0\">'InitialMagnification'<\/span>,1000)\r\n\r\nhold <span style=\"color: #A020F0\">on<\/span>\r\nxx = 0.5:1:N+0.5;\r\nyy = 0.5:1:M+0.5;\r\n<span style=\"color: #0000FF\">for<\/span> k = 1:numel(xx)\r\n    plot([xx(k) xx(k)], [0.5 M+0.5], <span style=\"color: #A020F0\">'Color'<\/span>, [.8 .8 .8]);\r\n<span style=\"color: #0000FF\">end<\/span>\r\n\r\n<span style=\"color: #0000FF\">for<\/span> k = 1:numel(yy)\r\n    plot([0.5 N+0.5], [yy(k) yy(k)], <span style=\"color: #A020F0\">'Color'<\/span>, [.8 .8 .8]);\r\n<span style=\"color: #0000FF\">end<\/span>\r\n\r\nplot(x2,y2,<span style=\"color: #A020F0\">'LineWidth'<\/span>,2,<span style=\"color: #A020F0\">'Color'<\/span>,[189 50 21]\/255);\r\nhold <span style=\"color: #A020F0\">off<\/span><\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/99\/roipoly1_02.png\"> <p>The interesting pixels are those which are only partially inside the polygon.  How do we decide the mask value for those pixels?<\/p>\r\n   <p>In the computer graphics space, I believe the most common term for this operation is <i>polygon scan conversion<\/i>.  If you do a Web search on this term you'll find lots of links, many of which go to computer graphics course notes.\r\n   <\/p>\r\n   <p>My own educational background is in electrical engineering, specializing in digital image and signal processing.  When I left\r\n      academia to become a software developer, implementing image processing algorithms, I found that many things I really needed\r\n      to know were in the computer graphics literature, not in the image processing literature.  (That's why the image processing\r\n      team has <i>Graphics Gems<\/i> volumes 1-5 on our bookshelf.)  That is most certainly the case for polygon scan conversion.\r\n   <\/p>\r\n   <p>In my next blogs on this topic, I'll explain the desirable properties that a polygon scan conversion algorithm should have,\r\n      the special cases that tend to trip up simple implementations, and the details of the Image Processing Toolbox implementation.\r\n   <\/p><script language=\"JavaScript\"> \r\n<!--\r\n    function grabCode_62469ba7393149b7854e040e94b7e729() {\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='62469ba7393149b7854e040e94b7e729 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 62469ba7393149b7854e040e94b7e729';\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 2006 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      <\/script>\r\n<noscript><em>A JavaScript-enabled browser is required to use the \"Get the MATLAB code\" link.<\/em><\/noscript>\r\n<p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_62469ba7393149b7854e040e94b7e729()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code<\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.3<br><\/p>\r\n<\/div>\r\n<!--\r\n62469ba7393149b7854e040e94b7e729 ##### SOURCE BEGIN #####\r\n%% POLY2MASK and ROIPOLY - Part 1\r\n% This is the first of several blogs about the algorithm behind \r\n% <https:\/\/www.mathworks.com\/help\/images\/index.htmlpoly2mask.html \r\n% |poly2mask|>\r\n% and \r\n% <https:\/\/www.mathworks.com\/help\/images\/index.htmlroipoly.html \r\n% |roipoly|>.  |poly2mask| takes a set of polygon vertices and produces a\r\n% binary mask image.  The binary mask has 1s for pixels inside the polygon\r\n% and 0s for pixels outside.  \r\n%\r\n% Here's a quick peek at what it does.\r\n\r\nx1 = [40 225 160 40];\r\ny1 = [230 170 45 230];\r\nmask = poly2mask(x1, y1, 300, 300);\r\nimshow(mask)\r\nhold on\r\nplot(x1,y1,'LineWidth',3,'Color','r');\r\nhold off\r\n\r\n%% \r\n% Earlier versions of the Image Processing Toolbox just had |roipoly|. It\r\n% contained both the core algorithm as well as a simple GUI for specifying\r\n% the polygon vertices with mouse clicks.  A few releases ago, we separated\r\n% the core algorithm into its own function, |poly2mask|.  Now |roipoly| handles\r\n% image display and grabbing mouse clicks, and it calls |poly2mask| for the\r\n% computation.\r\n%\r\n% You can see that the computation might be nontrivial if you use a more\r\n% complicated polygon, and if you zoom in a bit so you can see what's going\r\n% on along the polygon edges.  My second example computes a 25-by-25 mask,\r\n% displays the mask using high magnification, and displays a gray grid\r\n% showing the edges of the pixels.\r\n\r\nx2 = [10 24 5 15.5 10];\r\ny2 = [2 5 15 20.5 2];\r\nM = 25;\r\nN = 25;\r\n\r\nmask = poly2mask(x2,y2,25,25);\r\nmap = [255 255 255; 218 157 30] \/ 255;\r\nimshow(mask,map,'InitialMagnification',1000)\r\n\r\nhold on\r\nxx = 0.5:1:N+0.5;\r\nyy = 0.5:1:M+0.5;\r\nfor k = 1:numel(xx)\r\n    plot([xx(k) xx(k)], [0.5 M+0.5], 'Color', [.8 .8 .8]);\r\nend\r\n\r\nfor k = 1:numel(yy)\r\n    plot([0.5 N+0.5], [yy(k) yy(k)], 'Color', [.8 .8 .8]);\r\nend\r\n\r\nplot(x2,y2,'LineWidth',2,'Color',[189 50 21]\/255);\r\nhold off\r\n\r\n%%\r\n% The interesting pixels are those which are only partially inside the\r\n% polygon.  How do we decide the mask value for those pixels?\r\n%\r\n% In the computer graphics space, I believe the most common term for this\r\n% operation is _polygon scan conversion_.  If you do a Web search on this\r\n% term you'll find lots of links, many of which go to computer graphics \r\n% course notes.\r\n%\r\n% My own educational background is in electrical engineering, specializing\r\n% in digital image and signal processing.  When I left academia to become a\r\n% software developer, implementing image processing algorithms, I found\r\n% that many things I really needed to know were in the computer graphics\r\n% literature, not in the image processing literature.  (That's why\r\n% the image processing team has _Graphics Gems_ volumes 1-5 on our\r\n% bookshelf.)  That is most certainly the case for polygon scan conversion.\r\n%\r\n% In my next blogs on this topic, I'll explain the desirable properties\r\n% that a polygon scan conversion algorithm should have, the special cases\r\n% that tend to trip up simple implementations, and the details of the\r\n% Image Processing Toolbox implementation.\r\n\r\n##### SOURCE END ##### 62469ba7393149b7854e040e94b7e729\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   This is the first of several blogs about the algorithm behind poly2mask and roipoly.  poly2mask takes a set of polygon vertices and produces a binary mask image.  The binary mask has 1s for... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2006\/12\/05\/poly2mask-and-roipoly-part-1\/\">read more >><\/a><\/p>","protected":false},"author":42,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[28],"tags":[90,36,162,68,296],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/99"}],"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=99"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/99\/revisions"}],"predecessor-version":[{"id":2231,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/99\/revisions\/2231"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=99"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=99"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=99"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}