{"id":5114,"date":"2014-02-07T09:00:52","date_gmt":"2014-02-07T14:00:52","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=5114"},"modified":"2014-02-06T09:11:09","modified_gmt":"2014-02-06T14:11:09","slug":"downsampling-polygons","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2014\/02\/07\/downsampling-polygons\/","title":{"rendered":"Downsampling polygons"},"content":{"rendered":"\r\n\r\n<div class=\"content\"><p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\">Jiro<\/a>'s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/45342-polygon-simplification\">Polygon simplification<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/22116\">Peter Bone<\/a>.<\/p><p>Continuing from one of my recent posts on <a href=\"https:\/\/blogs.mathworks.com\/pick\/2013\/12\/27\/connecting-points-with-smooth-curves\/\">\"Connecting points with smooth curves\"<\/a>, I came across this new entry by Peter that would work nicely with the <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/42302-smooth-3d-bezier-curves-with-implicit-control-points\"><tt>hobbysplines<\/tt><\/a> function in tracing objects.<\/p><p>Here's how I would use it. Let's start with the original amoeba image.<\/p><pre class=\"codeinput\">im = imread(<span class=\"string\">'amoeba.png'<\/span>);\r\nimshow(im)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_reduce_poly\/potw_reduce_poly_01.png\" alt=\"\"> <p>I'll convert this grayscale image to black and white, and use a function called <a href=\"https:\/\/www.mathworks.com\/help\/images\/ref\/bwboundaries.html\"><tt>bwboundaries<\/tt><\/a> from the <a href=\"https:\/\/www.mathworks.com\/products\/image\/\">Image Processing Toolbox<\/a> to automatically trace out the boundaries.<\/p><pre class=\"codeinput\">im2 = im2bw(im);\r\nboundaries = bwboundaries(~im2, <span class=\"string\">'noholes'<\/span>)\r\n<\/pre><pre class=\"codeoutput\">boundaries = \r\n    [1696x2 double]\r\n    [  30x2 double]\r\n    [  33x2 double]\r\n    [ 111x2 double]\r\n    [  31x2 double]\r\n    [  34x2 double]\r\n<\/pre><p>Notice that <tt>bwboundaries<\/tt> returned a cell array of points for each boundary it found. In this case, it found 6 boundaries. For this discussion, I'll just focus on the largest boundary, i.e. the boundary with the most number of points. I can see that it is the first element. <i>Question for everyone: how would I programmatically find the largest boundary? There are multiple ways of doing it, from basic MATLAB programming to advanced image processing. Post your response <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=5114#respond\">below<\/a>.<\/i><\/p><pre class=\"codeinput\">largest = boundaries{1};\r\nhold <span class=\"string\">on<\/span>\r\nplot(largest(:,2),largest(:,1),<span class=\"string\">'r'<\/span>,<span class=\"string\">'LineWidth'<\/span>,2)\r\nhold <span class=\"string\">off<\/span>\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_reduce_poly\/potw_reduce_poly_02.png\" alt=\"\"> <p>Now, suppose that I don't care for the details (e.g. the individual hairs) and would like to get just the general shape of the amoeba. I can do some filtering or smoothing of the data, or simply down-sample the boundary points. Peter's <tt>reduce_poly<\/tt> reduces the points by automatically removing the \"least important\" points. For example, I can ask for 100 points.<\/p><pre class=\"codeinput\">numpts = 100;\r\nlargest2 = reduce_poly(largest', numpts)';\r\n\r\nimshow(im)\r\nhold <span class=\"string\">on<\/span>\r\nplot(largest2(:,2),largest2(:,1),<span class=\"string\">'r'<\/span>,<span class=\"string\">'LineWidth'<\/span>,2)\r\nhold <span class=\"string\">off<\/span>\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_reduce_poly\/potw_reduce_poly_03.png\" alt=\"\"> <p>That's better, but it's still too many points for my taste. I can modify <tt>numpts<\/tt> and rerun the block of code. <i>Or<\/i>, I can <a href=\"#bth__57-7\">increment a value and run the section<\/a> interactively.<\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_reduce_poly\/increment_animation.gif\" alt=\"\"> <\/p><p>I'll go with <tt>numpts = 20<\/tt> for this case.<\/p><pre class=\"codeinput\">numpts = 20;\r\nlargest2 = reduce_poly(largest', numpts)';\r\n\r\nimshow(im)\r\nhold <span class=\"string\">on<\/span>\r\nplot(largest2(:,2),largest2(:,1),<span class=\"string\">'r'<\/span>,<span class=\"string\">'LineWidth'<\/span>,2)\r\nhold <span class=\"string\">off<\/span>\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_reduce_poly\/potw_reduce_poly_04.png\" alt=\"\"> <p>Now, I'll use <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/364671\">Will<\/a>'s <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/42302-smooth-3d-bezier-curves-with-implicit-control-points\"><tt>hobbysplines<\/tt><\/a> to connect the points with smooth splines.<\/p><pre class=\"codeinput\"><span class=\"comment\">% Convert the boundary points to the format required by hobbysplines<\/span>\r\npts = fliplr(largest2(1:end-1,:));\r\n\r\nimshow(im)\r\nhobbysplines(num2cell(pts,2), <span class=\"keyword\">...<\/span>\r\n    <span class=\"string\">'linestyle'<\/span>,{<span class=\"string\">'linewidth'<\/span>,2}, <span class=\"keyword\">...<\/span>\r\n    <span class=\"string\">'color'<\/span>,<span class=\"string\">'red'<\/span>);\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_reduce_poly\/potw_reduce_poly_05.png\" alt=\"\"> <p><b>Comments<\/b><\/p><p>Let us know what you think <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=5114#respond\">here<\/a> or leave a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/45342-polygon-simplification#comments\">comment<\/a> for Peter.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_a90f311d23bc44339c02250eced4d43f() {\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='a90f311d23bc44339c02250eced4d43f ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' a90f311d23bc44339c02250eced4d43f';\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 2014 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_a90f311d23bc44339c02250eced4d43f()\"><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; R2013b<br><\/p><p class=\"footer\"><br>\r\n      Published with MATLAB&reg; R2013b<br><\/p><\/div><!--\r\na90f311d23bc44339c02250eced4d43f ##### SOURCE BEGIN #####\r\n%%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\r\n% Jiro>'s pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/45342-polygon-simplification Polygon\r\n% simplification> by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/22116 Peter\r\n% Bone>.\r\n%\r\n% Continuing from one of my recent posts on\r\n% <https:\/\/blogs.mathworks.com\/pick\/2013\/12\/27\/connecting-points-with-smooth-curves\/\r\n% \"Connecting points with smooth curves\">, I came across this new entry by\r\n% Peter that would work nicely with the\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/42302-smooth-3d-bezier-curves-with-implicit-control-points\r\n% |hobbysplines|> function in tracing objects.\r\n%\r\n% Here's how I would use it. Let's start with the original amoeba image.\r\n\r\nim = imread('amoeba.png');\r\nimshow(im)\r\n\r\n%%\r\n% I'll convert this grayscale image to black and white, and use a function\r\n% called <https:\/\/www.mathworks.com\/help\/images\/ref\/bwboundaries.html\r\n% |bwboundaries|> from the <https:\/\/www.mathworks.com\/products\/image\/ Image\r\n% Processing Toolbox> to automatically trace out the boundaries.\r\n\r\nim2 = im2bw(im);\r\nboundaries = bwboundaries(~im2, 'noholes')\r\n\r\n%%\r\n% Notice that |bwboundaries| returned a cell array of points for each\r\n% boundary it found. In this case, it found 6 boundaries. For this\r\n% discussion, I'll just focus on the largest boundary, i.e. the boundary\r\n% with the most number of points. I can see that it is the first element.\r\n% _Question for everyone: how would I programmatically find the largest\r\n% boundary? There are multiple ways of doing it, from basic MATLAB\r\n% programming to advanced image processing. Post your response\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=5114#respond below>._\r\n\r\nlargest = boundaries{1};\r\nhold on\r\nplot(largest(:,2),largest(:,1),'r','LineWidth',2)\r\nhold off\r\n\r\n%%\r\n% Now, suppose that I don't care for the details (e.g. the individual\r\n% hairs) and would like to get just the general shape of the amoeba. I can\r\n% do some filtering or smoothing of the data, or simply down-sample the\r\n% boundary points. Peter's |reduce_poly| reduces the points by\r\n% automatically removing the \"least important\" points. For example, I can\r\n% ask for 100 points.\r\n\r\nnumpts = 100;\r\nlargest2 = reduce_poly(largest', numpts)';\r\n\r\nimshow(im)\r\nhold on\r\nplot(largest2(:,2),largest2(:,1),'r','LineWidth',2)\r\nhold off\r\n\r\n%%\r\n% That's better, but it's still too many points for my taste. I can modify\r\n% |numpts| and rerun the block of code. _Or_, I can\r\n% <#bth__57-7\r\n% increment a value and run the section> interactively.\r\n%\r\n% <<increment_animation.gif>>\r\n%\r\n% I'll go with |numpts = 20| for this case.\r\n\r\nnumpts = 20;\r\nlargest2 = reduce_poly(largest', numpts)';\r\n\r\nimshow(im)\r\nhold on\r\nplot(largest2(:,2),largest2(:,1),'r','LineWidth',2)\r\nhold off\r\n\r\n%%\r\n% Now, I'll use\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/364671\r\n% Will>'s <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/42302-smooth-3d-bezier-curves-with-implicit-control-points\r\n% |hobbysplines|> to connect the points with smooth splines.\r\n\r\n% Convert the boundary points to the format required by hobbysplines\r\npts = fliplr(largest2(1:end-1,:));\r\n\r\nimshow(im)\r\nhobbysplines(num2cell(pts,2), ...\r\n    'linestyle',{'linewidth',2}, ...\r\n    'color','red');\r\n\r\n%%\r\n% *Comments*\r\n%\r\n% Let us know what you think\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=5114#respond here> or leave a\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/45342-polygon-simplification#comments\r\n% comment> for Peter.\r\n\r\n##### SOURCE END ##### a90f311d23bc44339c02250eced4d43f\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_reduce_poly\/potw_reduce_poly_01.png\" onError=\"this.style.display ='none';\" \/><\/div><p>\r\n\r\nJiro's pick this week is Polygon simplification by Peter Bone.Continuing from one of my recent posts on \"Connecting points with smooth curves\", I came across this new entry by Peter that would... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2014\/02\/07\/downsampling-polygons\/\">read more >><\/a><\/p>","protected":false},"author":35,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/5114"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=5114"}],"version-history":[{"count":4,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/5114\/revisions"}],"predecessor-version":[{"id":5118,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/5114\/revisions\/5118"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=5114"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=5114"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=5114"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}