{"id":263,"date":"2009-06-05T07:00:53","date_gmt":"2009-06-05T11:00:53","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/2009\/06\/05\/continental-divide-7-all-steps\/"},"modified":"2019-10-28T15:36:25","modified_gmt":"2019-10-28T19:36:25","slug":"continental-divide-7-all-steps","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2009\/06\/05\/continental-divide-7-all-steps\/","title":{"rendered":"Locating the US continental divide, part 7 &#8211; Putting it all together"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p>Today is the final post in my <a href=\"https:\/\/blogs.mathworks.com\/steve\/category\/continental-divide\/\">continental divide series<\/a>. In earlier posts I have used the problem of computing the US continental divide as a vehicle for exploring data import,\r\n      image display scaling, the watershed transform, label matrices, local and regional minima, binary image manipulation, boundary\r\n      tracing, and visualization techniques.\r\n   <\/p>\r\n   <p>For this last post, I thought it would useful to gather together all the computational steps in one short script, starting\r\n      with data import and finishing with the visualization.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #228B22\">% Import the tiles e10g and f10g.<\/span>\r\ndata_size = [6000 10800 1];  <span style=\"color: #228B22\">% The data has 1 band.<\/span>\r\nprecision = <span style=\"color: #A020F0\">'int16=&gt;int16'<\/span>;  <span style=\"color: #228B22\">% Read 16-bit signed ints into a int16 array.<\/span>\r\nheader_bytes = 0;\r\ninterleave = <span style=\"color: #A020F0\">'bsq'<\/span>;          <span style=\"color: #228B22\">% Band sequential. Not critical for 1 band.<\/span>\r\nbyte_order = <span style=\"color: #A020F0\">'ieee-le'<\/span>;\r\nE = multibandread(<span style=\"color: #A020F0\">'e10g'<\/span>, data_size, precision, header_bytes, <span style=\"color: #0000FF\">...<\/span>\r\n    interleave, byte_order);\r\nF = multibandread(<span style=\"color: #A020F0\">'f10g'<\/span>, data_size, precision, header_bytes, <span style=\"color: #0000FF\">...<\/span>\r\n    interleave, byte_order);\r\nEF = [E, F];\r\n\r\n<span style=\"color: #228B22\">% Crop the data.<\/span>\r\ndem = EF(1:4000, 6000:14500);\r\n\r\n<span style=\"color: #228B22\">% Form an ocean mask.<\/span>\r\nocean_mask = dem == -500;\r\n\r\n<span style=\"color: #228B22\">% Modify the ocean mask so it contains only the two connected components on<\/span>\r\n<span style=\"color: #228B22\">% the left and right side.<\/span>\r\n[M, N] = size(dem);\r\nocean_mask = bwselect(ocean_mask, [1 N], [1 1]);\r\n\r\n<span style=\"color: #228B22\">% Modify the DEM so that its only regional minima are the ocean_mask<\/span>\r\n<span style=\"color: #228B22\">% pixels.<\/span>\r\ndem_modified = imimposemin(dem, ocean_mask);\r\n\r\n<span style=\"color: #228B22\">% Compute the watershed transform of the modified DEM.<\/span>\r\nL = watershed(dem_modified);\r\n\r\n<span style=\"color: #228B22\">% Visualize the result. First, convert the output of watershed to a color<\/span>\r\n<span style=\"color: #228B22\">% image.<\/span>\r\npacific = [0.2 1 0.2];\r\natlantic = [0.3 0.3 1];\r\nridge = [1 0 0];\r\nrgb = label2rgb(L, [pacific; atlantic], ridge);\r\n\r\n<span style=\"color: #228B22\">% Superimpose the colored watershed image over the DEM.<\/span>\r\nimshow(dem, [-500 3000], <span style=\"color: #A020F0\">'InitialMagnification'<\/span>, <span style=\"color: #A020F0\">'fit'<\/span>)\r\nhold <span style=\"color: #A020F0\">on<\/span>\r\nh = imshow(rgb);\r\nset(h, <span style=\"color: #A020F0\">'AlphaData'<\/span>, 0.2);\r\n\r\n<span style=\"color: #228B22\">% Trace the watershed ridge line and superimpose it as a thick red line.<\/span>\r\nb = bwboundaries(L == 0, 4);\r\nb1 = b{1};  <span style=\"color: #228B22\">% There's only 1 boundary found here - the ridge line.<\/span>\r\nx = b1(:,2);\r\ny = b1(:,1);\r\nplot(x, y, <span style=\"color: #A020F0\">'r'<\/span>, <span style=\"color: #A020F0\">'LineWidth'<\/span>, 2);<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2009\/continental_divide_part_7_all_steps_01.jpg\"> <p>That's it! I hope you enjoyed this series.<\/p>\r\n   <p>Do you have your own ideas for fun computation and visualization using DEM data?  Please comment.<\/p>\r\n   <p><i>About this Series<\/i><\/p>\r\n   <p><a href=\"https:\/\/blogs.mathworks.com\/steve\/category\/continental-divide\/\">This series<\/a> explores the problem of computing the location of the continental divide for the United States.  The divide separates the\r\n      Atlantic and Pacific Ocean catchment basins for the North American continent.\r\n   <\/p>\r\n   <p>As an algorithm development problem, computing the divide lets us explore aspects of data import and visualization, manipulating\r\n      binary image masks, label matrices, regional minima, and the watershed transform.\r\n   <\/p>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"https:\/\/blogs.mathworks.com\/steve\/2009\/04\/24\/continental-divide-1-intro\/\">Part 1<\/a> - Introduction. Data import and display. <tt>multibandread<\/tt>, <tt>imshow<\/tt>.\r\n         <\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"https:\/\/blogs.mathworks.com\/steve\/2009\/05\/01\/continental-divide-2-watershed\/\">Part 2<\/a> - Watershed transform. <tt>watershed<\/tt>, <tt>label2rgb<\/tt>.\r\n         <\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"https:\/\/blogs.mathworks.com\/steve\/2009\/05\/08\/continental-divide-3-regmin\/\">Part 3<\/a> - Regional minima. <tt>imerode<\/tt>, <tt>imregionalmin<\/tt>.\r\n         <\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"https:\/\/blogs.mathworks.com\/steve\/2009\/05\/15\/continental-divide-4-oceans\/\">Part 4<\/a> - Ocean masks. binary image manipulation, <tt>bwselect<\/tt>.\r\n         <\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"https:\/\/blogs.mathworks.com\/steve\/2009\/05\/22\/continental-divide-5-imposemin\/\">Part 5<\/a> - Minima imposition. <tt>imimposemin<\/tt>.\r\n         <\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"https:\/\/blogs.mathworks.com\/steve\/2009\/05\/29\/continental-divide-6-watershed\/\">Part 6<\/a> - Computing and visualizing the divide. <tt>watershed<\/tt>, <tt>label2rgb<\/tt>, <tt>bwboundaries<\/tt>.\r\n         <\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"https:\/\/blogs.mathworks.com\/steve\/2009\/06\/05\/continental-divide-7-all-steps\/\">Part 7<\/a> - Putting it all together. One script that does everything, from data import through computation and visualization\r\n            of the divide.\r\n         <\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <p>Data credit: <i>GLOBE Task Team and others (Hastings, David A., Paula K. Dunbar, Gerald M. Elphingstone, Mark Bootz, Hiroshi Murakami, Hiroshi\r\n         Maruyama, Hiroshi Masaharu, Peter Holland, John Payne, Nevin A. Bryant, Thomas L. Logan, J.-P. Muller, Gunter Schreier, and\r\n         John S. MacDonald), eds., 1999. The Global Land One-kilometer Base Elevation (GLOBE) Digital Elevation Model, Version 1.0.\r\n         National Oceanic and Atmospheric Administration, National Geophysical Data Center, 325 Broadway, Boulder, Colorado 80305-3328,\r\n         U.S.A. Digital data base on the World Wide Web (URL: <a href=\"http:\/\/www.ngdc.noaa.gov\/mgg\/topo\/globe.html\">http:\/\/www.ngdc.noaa.gov\/mgg\/topo\/globe.html<\/a>) and CD-ROMs.<\/i><\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_8ff4d8cabad4478d86f611a4b546bdc7() {\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='8ff4d8cabad4478d86f611a4b546bdc7 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 8ff4d8cabad4478d86f611a4b546bdc7';\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 2009 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_8ff4d8cabad4478d86f611a4b546bdc7()\"><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.8<br><\/p>\r\n<\/div>\r\n<!--\r\n8ff4d8cabad4478d86f611a4b546bdc7 ##### SOURCE BEGIN #####\r\n%%\r\n% Today is the final post in my \r\n% <https:\/\/blogs.mathworks.com\/steve\/category\/continental-divide\/\r\n% continental divide series>. In earlier posts I have used the problem of\r\n% computing the US continental divide as a vehicle for exploring data\r\n% import, image display scaling, the watershed transform, label matrices,\r\n% local and regional minima, binary image manipulation, boundary tracing,\r\n% and visualization techniques.\r\n%\r\n% For this last post, I thought it would useful to gather together all the\r\n% computational steps in one short script, starting with data import and\r\n% finishing with the visualization.\r\n\r\n% Import the tiles e10g and f10g.\r\ndata_size = [6000 10800 1];  % The data has 1 band.\r\nprecision = 'int16=>int16';  % Read 16-bit signed ints into a int16 array.\r\nheader_bytes = 0;\r\ninterleave = 'bsq';          % Band sequential. Not critical for 1 band.\r\nbyte_order = 'ieee-le';\r\nE = multibandread('e10g', data_size, precision, header_bytes, ...\r\n    interleave, byte_order);\r\nF = multibandread('f10g', data_size, precision, header_bytes, ...\r\n    interleave, byte_order);\r\nEF = [E, F];  \r\n\r\n% Crop the data.\r\ndem = EF(1:4000, 6000:14500);\r\n\r\n% Form an ocean mask.\r\nocean_mask = dem == -500;\r\n\r\n% Modify the ocean mask so it contains only the two connected components on\r\n% the left and right side.\r\n[M, N] = size(dem);\r\nocean_mask = bwselect(ocean_mask, [1 N], [1 1]);\r\n\r\n% Modify the DEM so that its only regional minima are the ocean_mask\r\n% pixels.\r\ndem_modified = imimposemin(dem, ocean_mask);\r\n\r\n% Compute the watershed transform of the modified DEM.\r\nL = watershed(dem_modified);\r\n\r\n% Visualize the result. First, convert the output of watershed to a color\r\n% image.\r\npacific = [0.2 1 0.2];\r\natlantic = [0.3 0.3 1];\r\nridge = [1 0 0];\r\nrgb = label2rgb(L, [pacific; atlantic], ridge);\r\n\r\n% Superimpose the colored watershed image over the DEM.\r\nimshow(dem, [-500 3000], 'InitialMagnification', 'fit')\r\nhold on\r\nh = imshow(rgb);\r\nset(h, 'AlphaData', 0.2);\r\n\r\n% Trace the watershed ridge line and superimpose it as a thick red line.\r\nb = bwboundaries(L == 0, 4); \r\nb1 = b{1};  % There's only 1 boundary found here - the ridge line.\r\nx = b1(:,2);\r\ny = b1(:,1);\r\nplot(x, y, 'r', 'LineWidth', 2);\r\n\r\n%%\r\n% That's it! I hope you enjoyed this series.\r\n%\r\n% Do you have your own ideas for fun computation and visualization using\r\n% DEM data?  Please comment.\r\n\r\n%%\r\n% _About this Series_ \r\n%\r\n% <https:\/\/blogs.mathworks.com\/steve\/category\/continental-divide\/ This\r\n% series> explores the problem of computing the location of the \r\n% continental divide for the United States.  The divide separates the\r\n% Atlantic and Pacific Ocean catchment basins for the North American\r\n% continent.\r\n%\r\n% As an algorithm development problem, computing the divide lets us\r\n% explore aspects of data import and visualization, manipulating binary\r\n% image masks, label matrices, regional minima, and the watershed\r\n% transform.\r\n%\r\n% * Part 1 - Introduction. Data import and display. |multibandread|,\r\n% |imshow|. \r\n%\r\n% * Part 2 - Watershed transform. |watershed|, |label2rgb|.\r\n%\r\n% * Part 3 - Regional minima. |imerode|, |imregionalmin|.\r\n%\r\n% * Part 4 - Ocean masks. binary image manipulation, |bwselect|.\r\n%\r\n% * Part 5 - Minima imposition. |imimposemin|.\r\n%\r\n% * Part 6 - Computing and visualizing the divide. |watershed|,\r\n% |label2rgb|, |bwboundaries|. \r\n%\r\n% * Part 7 - Putting it all together. One script that does everything, from\r\n% data import through computation and visualization of the divide.\r\n%\r\n% Data credit: _GLOBE Task Team and others (Hastings, David A., Paula K.\r\n% Dunbar, Gerald M. Elphingstone, Mark Bootz, Hiroshi Murakami, Hiroshi\r\n% Maruyama, Hiroshi Masaharu, Peter Holland, John Payne, Nevin A. Bryant,\r\n% Thomas L. Logan, J.-P. Muller, Gunter Schreier, and John S. MacDonald),\r\n% eds., 1999. The Global Land One-kilometer Base Elevation (GLOBE) Digital\r\n% Elevation Model, Version 1.0. National Oceanic and Atmospheric\r\n% Administration, National Geophysical Data Center, 325 Broadway, Boulder,\r\n% Colorado 80305-3328, U.S.A. Digital data base on the World Wide Web (URL:\r\n% http:\/\/www.ngdc.noaa.gov\/mgg\/topo\/globe.html) and CD-ROMs._\r\n##### SOURCE END ##### 8ff4d8cabad4478d86f611a4b546bdc7\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   Today is the final post in my continental divide series. In earlier posts I have used the problem of computing the US continental divide as a vehicle for exploring data import,\r\n      image... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2009\/06\/05\/continental-divide-7-all-steps\/\">read more >><\/a><\/p>","protected":false},"author":42,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[19],"tags":[88,418,90,148,152,535,68,190,150],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/263"}],"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=263"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/263\/revisions"}],"predecessor-version":[{"id":3637,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/263\/revisions\/3637"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=263"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=263"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=263"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}