{"id":398,"date":"2011-12-06T07:00:11","date_gmt":"2011-12-06T12:00:11","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/?p=398"},"modified":"2019-10-29T17:02:03","modified_gmt":"2019-10-29T21:02:03","slug":"exploring-shortest-paths-part-4","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2011\/12\/06\/exploring-shortest-paths-part-4\/","title":{"rendered":"Exploring shortest paths &#8211; part 4"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p>In my previous posts on <i>Exploring shortest paths<\/i> (<a href=\"https:\/\/blogs.mathworks.com\/steve\/2011\/11\/01\/exploring-shortest-paths-part-1\/\">November 1<\/a>, <a href=\"https:\/\/blogs.mathworks.com\/steve\/2011\/11\/26\/exploring-shortest-paths-part-2\/\">November 26<\/a>, and December 3), I have noted several times that there isn't a unique shortest path (in general) between one object and another in a binary\r\n      image. To illustrate, here's a recap of the 'quasi-euclidean' example from last time.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">bw = logical([ <span style=\"color: #0000FF\">...<\/span>\r\n    0 0 0 0 0 0 0\r\n    0 1 0 0 0 0 0\r\n    0 0 0 0 0 0 0\r\n    0 0 0 0 0 0 0\r\n    0 0 0 0 0 0 0\r\n    0 0 0 0 0 0 0\r\n    0 0 0 0 0 0 0\r\n    0 0 0 0 0 0 0\r\n    0 0 0 0 0 0 0\r\n    0 0 0 0 0 1 0\r\n    0 0 0 0 0 0 0  ]);\r\nL = bwlabel(bw);\r\nbw1 = (L == 1);\r\nbw2 = (L == 2);\r\n\r\nD1 = bwdist(bw1, <span style=\"color: #A020F0\">'quasi-euclidean'<\/span>);\r\nD2 = bwdist(bw2, <span style=\"color: #A020F0\">'quasi-euclidean'<\/span>);\r\nD = D1 + D2;\r\nD = round(D * 32) \/ 32;\r\n\r\npaths = imregionalmin(D);\r\nP = false(size(bw));\r\nP = imoverlay(P, paths, [.5 .5 .5]);\r\nP = imoverlay(P, bw, [1 1 1]);\r\nimshow(P, <span style=\"color: #A020F0\">'InitialMagnification'<\/span>, <span style=\"color: #A020F0\">'fit'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2011\/shortest_path_d_01.png\"> <p>The gray pixels shown above <b>all<\/b> belong to one or more of the shortest paths between the two objects. So how do we pick one path?\r\n   <\/p>\r\n   <p>The first idea I had was to thin the paths \"blob\" using the 'thin' option of <tt>bwmorph<\/tt>.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">paths_thinned_once = bwmorph(paths, <span style=\"color: #A020F0\">'thin'<\/span>);\r\nP = false(size(bw));\r\nP = imoverlay(P, paths, [.5 .5 .5]);\r\nP = imoverlay(P, paths_thinned_once, [1 1 0]);\r\nP = imoverlay(P, bw, [1 1 1]);\r\nimshow(P, <span style=\"color: #A020F0\">'InitialMagnification'<\/span>, <span style=\"color: #A020F0\">'fit'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2011\/shortest_path_d_02.png\"> <p>That seems promising. What if we keep thinning until convergence? You do that by passing <tt>inf<\/tt> as the repetition value to <tt>bwmorph<\/tt>.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">paths_thinned_many = bwmorph(paths, <span style=\"color: #A020F0\">'thin'<\/span>, inf);\r\nP = false(size(bw));\r\nP = imoverlay(P, paths, [.5 .5 .5]);\r\nP = imoverlay(P, paths_thinned_many, [1 1 0]);\r\nP = imoverlay(P, bw, [1 1 1]);\r\nimshow(P, <span style=\"color: #A020F0\">'InitialMagnification'<\/span>, <span style=\"color: #A020F0\">'fit'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2011\/shortest_path_d_03.png\"> <p>That looks great.<\/p>\r\n   <p>Let's try the whole sequence with a longer path.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">url = <span style=\"color: #A020F0\">'https:\/\/blogs.mathworks.com\/images\/steve\/2011\/two-letters-cropped.png'<\/span>;\r\nbw = imread(url);\r\nimshow(bw, <span style=\"color: #A020F0\">'InitialMagnification'<\/span>, <span style=\"color: #A020F0\">'fit'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2011\/shortest_path_d_04.png\"> <pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">L = bwlabel(bw);\r\nbw1 = (L == 1);\r\nbw2 = (L == 2);\r\n\r\nD1 = bwdist(bw1, <span style=\"color: #A020F0\">'quasi-euclidean'<\/span>);\r\nD2 = bwdist(bw2, <span style=\"color: #A020F0\">'quasi-euclidean'<\/span>);\r\nD = D1 + D2;\r\nD = round(D * 32) \/ 32;\r\n\r\npaths = imregionalmin(D);\r\n\r\npaths_thinned_many = bwmorph(paths, <span style=\"color: #A020F0\">'thin'<\/span>, inf);\r\nP = false(size(bw));\r\nP = imoverlay(P, paths, [.5 .5 .5]);\r\nP = imoverlay(P, paths_thinned_many, [1 1 0]);\r\nP = imoverlay(P, bw, [1 1 1]);\r\nimshow(P, <span style=\"color: #A020F0\">'InitialMagnification'<\/span>, <span style=\"color: #A020F0\">'fit'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2011\/shortest_path_d_05.png\"> <p>As before, the pixels belonging to any shortest path are shown in gray. The pixels in yellow belong to one particular path.<\/p>\r\n   <p>Next time I'll explain how to find shortest paths subject to constraints.<\/p>\r\n<h4>All the posts in this series<\/h4>\r\n      <ul>\r\n         <li>the basic idea of finding shortest paths by adding two distance transforms together (<a href=\"https:\/\/blogs.mathworks.com\/steve\/2011\/11\/01\/exploring-shortest-paths-part-1\/\">part 1<\/a>)\r\n         <\/li>\r\n         <li>the nonuniqueness of the shortest paths (<a href=\"https:\/\/blogs.mathworks.com\/steve\/2011\/11\/26\/exploring-shortest-paths-part-2\/\">part 2<\/a>)\r\n         <\/li>\r\n         <li>handling floating-point round-off effects (<a href=\"https:\/\/blogs.mathworks.com\/steve\/2011\/12\/02\/exploring-shortest-paths-part-3\/\">part 3<\/a>)\r\n         <\/li>\r\n         <li>using thinning to pick out a single path (<a href=\"https:\/\/blogs.mathworks.com\/steve\/2011\/12\/06\/exploring-shortest-paths-part-4\/\">part 4<\/a>)\r\n         <\/li>\r\n         <li>using <tt>bwdistgeodesic<\/tt> to find shortest paths subject to constraint (<a href=\"https:\/\/blogs.mathworks.com\/steve\/2011\/12\/13\/exploring-shortest-paths-part-5\/\">part 5<\/a>)\r\n         <\/li>\r\n      <\/ul>\r\n<script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_0529a645ec9649a7a6ac7ed899be8d75() {\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='0529a645ec9649a7a6ac7ed899be8d75 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 0529a645ec9649a7a6ac7ed899be8d75';\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 2011 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_0529a645ec9649a7a6ac7ed899be8d75()\"><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.13<br><\/p>\r\n<\/div>\r\n<!--\r\n0529a645ec9649a7a6ac7ed899be8d75 ##### SOURCE BEGIN #####\r\n%%\r\n% In my previous posts on _Exploring shortest paths_\r\n% (<https:\/\/blogs.mathworks.com\/steve\/2011\/11\/01\/exploring-shortest-paths-part-1\/\r\n% November 1>,\r\n% <https:\/\/blogs.mathworks.com\/steve\/2011\/11\/26\/exploring-shortest-paths-part-2\/\r\n% November 26>, and\r\n% <https:\/\/blogs.mathworks.com\/steve\/2011\/12\/03\/exploring-shortest-paths-part-3\/\r\n% December 3>), I have noted several times that there isn't a unique\r\n% shortest path (in general) between one object and another in a binary\r\n% image. To illustrate, here's a recap of the 'quasi-euclidean' example\r\n% from last time.\r\n\r\nbw = logical([ ...\r\n    0 0 0 0 0 0 0\r\n    0 1 0 0 0 0 0\r\n    0 0 0 0 0 0 0\r\n    0 0 0 0 0 0 0\r\n    0 0 0 0 0 0 0\r\n    0 0 0 0 0 0 0\r\n    0 0 0 0 0 0 0\r\n    0 0 0 0 0 0 0\r\n    0 0 0 0 0 0 0\r\n    0 0 0 0 0 1 0 \r\n    0 0 0 0 0 0 0  ]);\r\nL = bwlabel(bw);\r\nbw1 = (L == 1);\r\nbw2 = (L == 2);\r\n\r\nD1 = bwdist(bw1, 'quasi-euclidean');\r\nD2 = bwdist(bw2, 'quasi-euclidean');\r\nD = D1 + D2;\r\nD = round(D * 32) \/ 32;\r\n\r\npaths = imregionalmin(D);\r\nP = false(size(bw));\r\nP = imoverlay(P, paths, [.5 .5 .5]);\r\nP = imoverlay(P, bw, [1 1 1]);\r\nimshow(P, 'InitialMagnification', 'fit')\r\n\r\n%%\r\n% The gray pixels shown above *all* belong to one or more of the shortest\r\n% paths between the two objects. So how do we pick one path?\r\n%\r\n% The first idea I had was to thin the paths \"blob\" using the 'thin' option\r\n% of |bwmorph|.\r\n\r\npaths_thinned_once = bwmorph(paths, 'thin');\r\nP = false(size(bw));\r\nP = imoverlay(P, paths, [.5 .5 .5]);\r\nP = imoverlay(P, paths_thinned_once, [1 1 0]);\r\nP = imoverlay(P, bw, [1 1 1]);\r\nimshow(P, 'InitialMagnification', 'fit')\r\n\r\n%%\r\n% That seems promising. What if we keep thinning until convergence? You do\r\n% that by passing |inf| as the repetition value to |bwmorph|.\r\n\r\npaths_thinned_many = bwmorph(paths, 'thin', inf);\r\nP = false(size(bw));\r\nP = imoverlay(P, paths, [.5 .5 .5]);\r\nP = imoverlay(P, paths_thinned_many, [1 1 0]);\r\nP = imoverlay(P, bw, [1 1 1]);\r\nimshow(P, 'InitialMagnification', 'fit')\r\n\r\n%%\r\n% That looks great. \r\n%\r\n% Let's try the whole sequence with a longer path.\r\nurl = 'https:\/\/blogs.mathworks.com\/images\/steve\/2011\/two-letters-cropped.png';\r\nbw = imread(url);\r\nimshow(bw, 'InitialMagnification', 'fit')\r\n\r\n%%\r\nL = bwlabel(bw);\r\nbw1 = (L == 1);\r\nbw2 = (L == 2);\r\n\r\nD1 = bwdist(bw1, 'quasi-euclidean');\r\nD2 = bwdist(bw2, 'quasi-euclidean');\r\nD = D1 + D2;\r\nD = round(D * 32) \/ 32;\r\n\r\npaths = imregionalmin(D);\r\n\r\npaths_thinned_many = bwmorph(paths, 'thin', inf);\r\nP = false(size(bw));\r\nP = imoverlay(P, paths, [.5 .5 .5]);\r\nP = imoverlay(P, paths_thinned_many, [1 1 0]);\r\nP = imoverlay(P, bw, [1 1 1]);\r\nimshow(P, 'InitialMagnification', 'fit')\r\n\r\n%%\r\n% As before, the pixels belonging to any shortest path are shown in gray.\r\n% The pixels in yellow belong to one particular path.\r\n%\r\n% Next time I'll explain how to find shortest paths subject to constraints.\r\n\r\n##### SOURCE END ##### 0529a645ec9649a7a6ac7ed899be8d75\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   In my previous posts on Exploring shortest paths (November 1, November 26, and December 3), I have noted several times that there isn't a unique shortest path (in general) between one object and... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2011\/12\/06\/exploring-shortest-paths-part-4\/\">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":[456,166,86,102,737,76,366,36,330,188,190],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/398"}],"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=398"}],"version-history":[{"count":3,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/398\/revisions"}],"predecessor-version":[{"id":2295,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/398\/revisions\/2295"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=398"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=398"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=398"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}