{"id":639,"date":"2012-07-09T15:02:29","date_gmt":"2012-07-09T19:02:29","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/?p=639"},"modified":"2019-10-31T14:51:53","modified_gmt":"2019-10-31T18:51:53","slug":"a-cody-problem-simplify-a-polygon","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2012\/07\/09\/a-cody-problem-simplify-a-polygon\/","title":{"rendered":"A Cody problem: simplify a polygon"},"content":{"rendered":"<div class=\"content\"><p>Today I'm combining my blog post with a new <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/cody\/problems\/820\/\">problem submission<\/a> on <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/cody\">Cody<\/a>. The problem is related to the function <tt>bwboundaries<\/tt> in the Image Processing Toolbox. This function traces the boundaries of objects (and holes within objects), returning each boundary as a set of x-y vertices.<\/p><p>Here's an example from the documentation:<\/p><pre class=\"codeinput\">I = imread(<span class=\"string\">'rice.png'<\/span>);\r\nBW = im2bw(I, graythresh(I));\r\n[B,L] = bwboundaries(BW,<span class=\"string\">'noholes'<\/span>);\r\nimshow(label2rgb(L, @jet, [.5 .5 .5]))\r\nhold <span class=\"string\">on<\/span>\r\n<span class=\"keyword\">for<\/span> k = 1:length(B)\r\n    boundary = B{k};\r\n    plot(boundary(:,2), boundary(:,1), <span class=\"string\">'w'<\/span>, <span class=\"string\">'LineWidth'<\/span>, 2)\r\n<span class=\"keyword\">end<\/span>\r\nhold <span class=\"string\">off<\/span>\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2012\/simplify_polygon_cody_01.png\" alt=\"\"> <p>I have heard a few times from customers that they would like to eliminate \"unnecessary vertices\" in the output of <tt>bwboundaries<\/tt>. To illustrate, let's look at the boundary for a simple binary image containing a rectangle.<\/p><pre class=\"codeinput\">close(gcf)\r\nbw = false(15,40);\r\nbw(5:10,10:30) = true;\r\nB = bwboundaries(bw);\r\nboundary = B{1};\r\nimshow(bw,<span class=\"string\">'InitialMagnification'<\/span>, <span class=\"string\">'fit'<\/span>)\r\nhold <span class=\"string\">on<\/span>\r\nplot(boundary(:,2), boundary(:,1))\r\nplot(boundary(:,2), boundary(:,1), <span class=\"string\">'*'<\/span>)\r\nhold <span class=\"string\">off<\/span>\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2012\/simplify_polygon_cody_02.png\" alt=\"\"> <p>Note that the boundary goes through the pixel centers, which is why a little bit of white appears outside the boundary line.<\/p><p>I can imagine that for some applications it would be nice to have the boundary polygon without as many vertices, like this:<\/p><pre class=\"codeinput\">x = [10 30 30 10 10];\r\ny = [5 5 10 10 5];\r\nimshow(bw,<span class=\"string\">'InitialMagnification'<\/span>, <span class=\"string\">'fit'<\/span>)\r\nhold <span class=\"string\">on<\/span>\r\nplot(x, y)\r\nplot(x, y, <span class=\"string\">'*'<\/span>)\r\nhold <span class=\"string\">off<\/span>\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2012\/simplify_polygon_cody_03.png\" alt=\"\"> <p>This idea has been on my potential blog topics list for a long time, and today I finally got around to thinking about it. I can think of several different approaches, and it's not immediately obvious to me which approach might be best (for whatever definition of <i>best<\/i> you believe in).<\/p><p>So I thought I would make a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/cody\/problems\/820\/\">Cody problem<\/a> out of it and see what creative ideas pop up.<\/p><p>I encourage you to give the <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/cody\/problems\/820\/\">problem<\/a> a try. I'll summarize the results later.<\/p><p>Good luck!<\/p>\r\n\r\n<p>\r\n<em>UPDATE: In a week or so, I'll a pick the best solution (according to the well-known purple-seven multiphasic optimality criterion) and send out a prize (a hat or something similar) to the solver.\r\n<\/em>\r\n<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_49af6a53a7d6441381e5d7d1d9e34e08() {\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='49af6a53a7d6441381e5d7d1d9e34e08 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 49af6a53a7d6441381e5d7d1d9e34e08';\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        copyright = 'Copyright 2012 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 copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\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     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_49af6a53a7d6441381e5d7d1d9e34e08()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.14<br><\/p><p class=\"footer\"><br>\r\n      Published with MATLAB&reg; 7.14<br><\/p><\/div><!--\r\n49af6a53a7d6441381e5d7d1d9e34e08 ##### SOURCE BEGIN #####\r\n%%\r\n% Today I'm combining my blog post with a new\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/cody\/problems\/820\/ problem\r\n% submission> on <https:\/\/www.mathworks.com\/matlabcentral\/cody Cody>. The\r\n% problem is related to the function |bwboundaries| in the Image Processing\r\n% Toolbox. This function traces the boundaries of objects (and holes within\r\n% objects), returning each boundary as a set of x-y vertices.\r\n%\r\n% Here's an example from the documentation:\r\n\r\nI = imread('rice.png');\r\nBW = im2bw(I, graythresh(I));\r\n[B,L] = bwboundaries(BW,'noholes');\r\nimshow(label2rgb(L, @jet, [.5 .5 .5]))\r\nhold on\r\nfor k = 1:length(B)\r\n    boundary = B{k};\r\n    plot(boundary(:,2), boundary(:,1), 'w', 'LineWidth', 2)\r\nend\r\nhold off\r\n\r\n%%\r\n% I have heard a few times from customers that they would like to eliminate\r\n% \"unnecessary vertices\" in the output of |bwboundaries|. To illustrate,\r\n% let's look at the boundary for a simple binary image containing a\r\n% rectangle.\r\n\r\nclose(gcf)\r\nbw = false(15,40);\r\nbw(5:10,10:30) = true;\r\nB = bwboundaries(bw);\r\nboundary = B{1};\r\nimshow(bw,'InitialMagnification', 'fit')\r\nhold on\r\nplot(boundary(:,2), boundary(:,1))\r\nplot(boundary(:,2), boundary(:,1), '*')\r\nhold off\r\n\r\n%%\r\n% Note that the boundary goes through the pixel centers, which is why a\r\n% little bit of white appears outside the boundary line.\r\n%\r\n% I can imagine that for some applications it would be nice to have the\r\n% boundary polygon without as many vertices, like this:\r\n\r\nx = [10 30 30 10 10];\r\ny = [5 5 10 10 5];\r\nimshow(bw,'InitialMagnification', 'fit')\r\nhold on\r\nplot(x, y)\r\nplot(x, y, '*')\r\nhold off\r\n\r\n%%\r\n% This idea has been on my potential blog topics list for a long time, and\r\n% today I finally got around to thinking about it. I can think of several\r\n% different approaches, and it's not immediately obvious to me which\r\n% approach might be best (for whatever definition of _best_ you believe\r\n% in).\r\n%\r\n% So I thought I would make a\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/cody\/problems\/820\/ Cody problem>\r\n% out of it and see what creative ideas pop up.\r\n%\r\n% I encourage you to give the\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/cody\/problems\/820\/ problem> a\r\n% try. I'll summarize the results later.\r\n%\r\n% Good luck!\r\n##### SOURCE END ##### 49af6a53a7d6441381e5d7d1d9e34e08\r\n-->","protected":false},"excerpt":{"rendered":"<p>Today I'm combining my blog post with a new problem submission on Cody. The problem is related to the function bwboundaries in the Image Processing Toolbox. This function traces the boundaries of... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2012\/07\/09\/a-cody-problem-simplify-a-polygon\/\">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":[88,278,102,82,90,84,76,36,152,705,68,100],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/639"}],"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=639"}],"version-history":[{"count":8,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/639\/revisions"}],"predecessor-version":[{"id":3791,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/639\/revisions\/3791"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=639"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=639"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=639"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}