{"id":3506,"date":"2012-04-27T08:11:08","date_gmt":"2012-04-27T13:11:08","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=3506"},"modified":"2022-05-29T20:18:00","modified_gmt":"2022-05-30T00:18:00","slug":"calculating-arclengths-made-easy","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2012\/04\/27\/calculating-arclengths-made-easy\/","title":{"rendered":"Calculating arclengths&#8230;made easy!"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction><\/introduction>\r\n   <p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/911\">Brett<\/a>'s Pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/34871-arclength\">Arclength,<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/679\">John D'Errico<\/a>.\r\n   <\/p>\r\n   <p>First, a nod (and some MATLAB swag!) to <a title=\"https:\/\/sites.google.com\/site\/engelgeography\/\">Frank Engel<\/a>, who steered us to John's awesome code! We recently asked users to <a href=\"https:\/\/blogs.mathworks.com\/pick\/2012\/04\/13\/what-is-your-favorite-unrecognized-file-exchange-submission\">nominate their favorite File Exchange contributions<\/a> and Frank jumped in quickly to steer us to Arclength.\r\n   <\/p>\r\n   <p>How timely! For a medical image processing seminar I recently put together, I wanted to measure the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Tortuosity\">tortuosity<\/a> of blood vessels. Defining tortuosity as the ratio of arclength to endpoint distance, I segmented an image of the retinal\r\n      vasculature, <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2012a\/toolbox\/images\/ref\/bwmorph.html\">skeletonized<\/a> the image, and broke the vessels into sub-units after detecting <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2012a\/toolbox\/images\/ref\/bwmorph.html\">branch points<\/a>, and then sought to measure the length of the sub-units.\r\n   <\/p>\r\n   <p>After working on the problem for a while, I came up with two reliable methods--after a few misfires. For my first attempt,\r\n      I sought to reorient vessel segments so that there were no repeated \"x-values,\" and to fit and calculate the length of splines.\r\n      That approach was unwieldy, and yielded poor results. After playing around some more, I found a couple of reliable, robust\r\n      approaches. First, I used <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2012a\/toolbox\/images\/ref\/regionprops.html\"><tt>regionprops<\/tt><\/a> to measure the perimeters of the segments, and divided that value by two--works like a charm, since the vessels were skeletonized.\r\n      Next, I isolated vessel segments using <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2012a\/toolbox\/images\/ref\/bwlabel.html\"><tt>bwlabel<\/tt><\/a>, and then calculated the maximum of the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2012a\/toolbox\/images\/ref\/bwdistgeodesic.html\"><tt>bwdistgeodesic<\/tt><\/a> (geodesic distance transform). Another success!\r\n   <\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/pick\/files\/Retina.png\"> <\/p>\r\n   <p>John's approach helped me to see where I went astray on my first misguided attempt, and made the solution easy:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">img = imread(<span style=\"color: #A020F0\">'seg10.png'<\/span>);\r\n<span style=\"color: #228B22\">%imshow(img);<\/span><\/pre><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/pick\/files\/seg10.png\"> <\/p>\r\n   <p>Here are three approaches:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #228B22\">% Perimeter:<\/span>\r\nperim = regionprops(img,<span style=\"color: #A020F0\">'Perimeter'<\/span>);\r\nperim = perim.Perimeter\/2\r\n\r\n<span style=\"color: #228B22\">% Geodesic Distance Transform:<\/span>\r\n[r,c] = find(bwmorph(img,<span style=\"color: #A020F0\">'endpoints'<\/span>));\r\ngdt = max(max(bwdistgeodesic(img,c(1),r(1),<span style=\"color: #A020F0\">'quasi-euclidean'<\/span>)))\r\n\r\n<span style=\"color: #228B22\">% John's arclength:<\/span>\r\npts = regionprops(img,<span style=\"color: #A020F0\">'pixellist'<\/span>);\r\npts = [pts.PixelList];\r\nvessellength = arclength(pts(:,1),pts(:,2))<\/pre><pre style=\"font-style:oblique\">perim =\r\n       74.527\r\ngdt =\r\n       74.527\r\nvessellength =\r\n       74.527\r\n<\/pre><p>So it appears that all of these approaches work, and isn't it nice to have options? John's code is particularly useful even\r\n      if your x-y- coordinates aren't necessarily extracted from an image. And because he breaks his path \"into a series of integrals\r\n      between each pair of breaks on the curve,\" he avoids the problems I had trying to fit a continuous spline to a rotated set\r\n      of coordinates.\r\n   <\/p>\r\n   <p>Very nice, John. And thanks for the note, Frank. Swag on the way to both of you!<\/p>\r\n   <p>Keep those suggestions coming. This makes my job a lot easier! :)<\/p>\r\n   <p>As always, <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=3506#respond\">comments to this blog post<\/a> are welcome. Or leave a comment for John <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/34871-arclength#comments\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_2829b231d6f74f6fba84870c077f55eb() {\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='2829b231d6f74f6fba84870c077f55eb ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 2829b231d6f74f6fba84870c077f55eb';\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 = 'Brett Shoelson';\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 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_2829b231d6f74f6fba84870c077f55eb()\"><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.14<br><\/p>\r\n<\/div>\r\n<!--\r\n2829b231d6f74f6fba84870c077f55eb ##### SOURCE BEGIN #####\r\n%% Arclengths made easy!\r\n%% \r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/911 Brett>'s Pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/34871-arclength Arclength,> by \r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/679 John D'Errico>.\r\n\r\n%%\r\n% First, a nod (and some MATLAB swag!) to <https:\/\/sites.google.com\/site\/engelgeography\/ Frank Engel>, who steered us\r\n% to John's awesome code! We recently asked users to\r\n% <https:\/\/blogs.mathworks.com\/pick\/2012\/04\/13\/what-is-your-favorite-unrecognized-file-exchange-submission nominate their favorite File Exchange contributions> \r\n% and Frank jumped in quickly to steer us to Arclength.\r\n\r\n%%\r\n% How timely! For a medical image processing seminar I\r\n% recently put together, I wanted to measure the <http:\/\/en.wikipedia.org\/wiki\/Tortuosity tortuosity>\r\n% of blood vessels. Defining tortuosity as the ratio of arclength to endpoint distance, I segmented an image of the retinal\r\n% vasculature, <https:\/\/www.mathworks.com\/help\/releases\/R2012a\/toolbox\/images\/ref\/bwmorph.html skeletonized> the image, and broke the vessels into sub-units after\r\n% detecting <https:\/\/www.mathworks.com\/help\/releases\/R2012a\/toolbox\/images\/ref\/bwmorph.html branch points>,\r\n% and then sought to measure the length of the sub-units.\r\n\r\n%% \r\n% After working on the problem for a while, I came up with\r\n% two reliable methodsREPLACE_WITH_DASH_DASHafter a few misfires. For my first attempt, I sought to reorient vessel segments\r\n% so that there were no repeated \"x-values,\" and to fit and calculate the length of splines.\r\n% That approach was unwieldy, and yielded poor results. After playing around some more, I found a couple of reliable, robust approaches.\r\n% First, I used \r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2012a\/toolbox\/images\/ref\/regionprops.html |regionprops|> to measure \r\n% the perimeters of the segments, and divided that value by twoREPLACE_WITH_DASH_DASHworks like a charm, since the vessels were skeletonized.\r\n% Next, I isolated vessel segments using \r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2012a\/toolbox\/images\/ref\/bwlabel.html |bwlabel|>, and then calculated the maximum of the <https:\/\/www.mathworks.com\/help\/releases\/R2012a\/toolbox\/images\/ref\/bwdistgeodesic.html |bwdistgeodesic|> \r\n% (geodesic distance transform). Another success! \r\n\r\n%%\r\n% \r\n% <<https:\/\/blogs.mathworks.com\/pick\/files\/Retina.png>>\r\n% \r\n\r\n%% \r\n% John's approach helped me to see where I went astray on my first misguided attempt,\r\n% and made the solution easy:\r\n\r\nimg = imread('seg10.png');\r\n%imshow(img);\r\n%%\r\n% \r\n% <<https:\/\/blogs.mathworks.com\/pick\/files\/seg10.png>>\r\n% \r\n\r\n%%\r\n% Here are three approaches:\r\n\r\n\r\n% Perimeter:\r\nperim = regionprops(img,'Perimeter');\r\nperim = perim.Perimeter\/2\r\n\r\n% Geodesic Distance Transform:\r\n[r,c] = find(bwmorph(img,'endpoints'));\r\ngdt = max(max(bwdistgeodesic(img,c(1),r(1),'quasi-euclidean')))\r\n\r\n% John's arclength:\r\npts = regionprops(img,'pixellist');\r\npts = [pts.PixelList];\r\nvessellength = arclength(pts(:,1),pts(:,2))\r\n\r\n\r\n%%\r\n% So it appears that all of these approaches work, and isn't\r\n% it nice to have options? John's code is particularly\r\n% useful even if your x-y- coordinates aren't necessarily\r\n% extracted from an image. And because he breaks his path\r\n% \"into a series of integrals between each pair of breaks on the curve,\" he avoids the problems I had \r\n% trying to fit a continuous spline to a rotated set of coordinates.\r\n\r\n%%\r\n% Very nice, John. And thanks for the note, Frank. Swag on the way to both of you!\r\n\r\n%%\r\n% Keep those suggestions coming. This makes my job a lot\r\n% easier! :)\r\n\r\n%% \r\n% As always, <https:\/\/blogs.mathworks.com\/pick\/?p=3506#respond comments to this blog post> are welcome. Or leave a\r\n% comment for John\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/34871-arclength#comments here>.\r\n\r\n##### SOURCE END ##### 2829b231d6f74f6fba84870c077f55eb\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n   Brett's Pick this week is Arclength, by John D'Errico.\r\n   \r\n   First, a nod (and some MATLAB swag!) to Frank Engel, who steered us to John's awesome code! We recently asked users to... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2012\/04\/27\/calculating-arclengths-made-easy\/\">read more >><\/a><\/p>","protected":false},"author":34,"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\/3506"}],"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\/34"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=3506"}],"version-history":[{"count":22,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/3506\/revisions"}],"predecessor-version":[{"id":15974,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/3506\/revisions\/15974"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=3506"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=3506"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=3506"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}