{"id":261,"date":"2009-05-22T07:00:53","date_gmt":"2009-05-22T11:00:53","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/2009\/05\/22\/continental-divide-5-imposemin\/"},"modified":"2019-10-28T15:33:48","modified_gmt":"2019-10-28T19:33:48","slug":"continental-divide-5-imposemin","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2009\/05\/22\/continental-divide-5-imposemin\/","title":{"rendered":"Locating the US continental divide, part 5 &#8211; Minima imposition"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p>Today, in part 5 of my <a href=\"https:\/\/blogs.mathworks.com\/steve\/category\/continental-divide\/\">series<\/a> on computing the US continental divide, we'll look at a technique called <i>minima imposition<\/i>. Previously we looked at data import and display, the watershed transform and label matrices, different kinds of minima,\r\n      and manipulating binary images to form an ocean mask.\r\n   <\/p>\r\n   <p>In part 3, we found that our DEM data set for the continental US contained more than half a million regional minima, each\r\n      of which was turned into a catchment basin by the watershed transform. We need a way to get just two catchment basins.\r\n   <\/p>\r\n   <p><i>Minima imposition<\/i> is technique that modifies surface heights, pushing some height values upward so that the resulting surface has regional\r\n      minima only at specified locations.  Minima imposition performs this modification while preserving the local height ordering\r\n      relationships between neighboring pixels.\r\n   <\/p>\r\n   <p>Here's a one-dimensional example to illustrate.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">a = [1 2 3 4 3 4 3 2 1]<\/pre><pre style=\"font-style:oblique\">\r\na =\r\n\r\n     1     2     3     4     3     4     3     2     1\r\n\r\n<\/pre><p>The vector <tt>a<\/tt> has three different regional minima locations:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">imregionalmin(a)<\/pre><pre style=\"font-style:oblique\">\r\nans =\r\n\r\n     1     0     0     0     1     0     0     0     1\r\n\r\n<\/pre><p>Let's make a mask representing just two locations where we want to have regional minima:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">mask = [1 0 0 0 0 0 0 0 1]<\/pre><pre style=\"font-style:oblique\">\r\nmask =\r\n\r\n     1     0     0     0     0     0     0     0     1\r\n\r\n<\/pre><p>This mask says we only want regional minima at each end of the vector, so we want the regional minima in the middle to go\r\n      away somehow. We do this using the Image Processing Toolbox function <tt>imimposemin<\/tt>.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">imimposemin(a, mask)<\/pre><pre style=\"font-style:oblique\">\r\nans =\r\n\r\n      -Inf    2.0030    3.0030    4.0030    4.0030    4.0030    3.0030    2.0030      -Inf\r\n\r\n<\/pre><p>The new result has regional minima only at the desired locations.  The local minima in the middle of the vector has been pushed\r\n      up to the level of the surround elements.  (As I write this, I am noticing that the small fractional bump that <tt>imimposemin<\/tt> gives to all the middle elements seems possibly unnecessary. This is something I will investigate at another time.)\r\n   <\/p>\r\n   <p>In my previous post I created a binary image representing an ocean mask:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">s = load(<span style=\"color: #A020F0\">'continental_divide'<\/span>);\r\ndem = s.dem_cropped;\r\nocean_mask = s.ocean_mask;\r\nimshow(ocean_mask, <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\/2009\/continental_divide_part_5_minima_imposition_01.jpg\"> <p>I can use <tt>imimposemin<\/tt>, together with the <tt>ocean_mask<\/tt> image, to modify the DEM so that the two oceans are the <b>only<\/b> regional minima.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">dem_modified = imimposemin(dem, ocean_mask);<\/pre><p>It doesn't look much different:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">imshow(dem_modified, [-500 3000], <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\/2009\/continental_divide_part_5_minima_imposition_02.jpg\"> <p>But we can verify that now there are only two regional minima:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">regional_minima_labeled = bwlabel(imregionalmin(dem_modified));\r\nmax(regional_minima_labeled(:))<\/pre><pre style=\"font-style:oblique\">\r\nans =\r\n\r\n     2\r\n\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">imshow(label2rgb(regional_minima_labeled), <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\/2009\/continental_divide_part_5_minima_imposition_03.jpg\"> <p>I'll add the modified DEM to the data I've been collecting in continental_divide.mat:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">save <span style=\"color: #A020F0\">continental_divide<\/span> <span style=\"color: #A020F0\">dem_modified<\/span> <span style=\"color: #A020F0\">-append<\/span><\/pre><p>Next time I'll perform the final step in computing the continental divide and show one way to visualize it using transparency.<\/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_05cb77041a8545869c5021f100fa31ff() {\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='05cb77041a8545869c5021f100fa31ff ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 05cb77041a8545869c5021f100fa31ff';\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_05cb77041a8545869c5021f100fa31ff()\"><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\n05cb77041a8545869c5021f100fa31ff ##### SOURCE BEGIN #####\r\n%%\r\n% Today, in part 5 of my\r\n% <https:\/\/blogs.mathworks.com\/steve\/category\/continental-divide\/ series> on \r\n% computing the US continental divide, we'll look at a technique called\r\n% _minima imposition_. Previously we looked at data import and display, the\r\n% watershed transform and label matrices, different kinds of minima, and\r\n% manipulating binary images to form an ocean mask.\r\n%\r\n% In part 3, we found that our DEM data set for the continental US\r\n% contained more than half a million regional minima, each of which was\r\n% turned into a catchment basin by the watershed transform. We need a way\r\n% to get just two catchment basins.\r\n%\r\n% _Minima imposition_ is technique that modifies surface heights, pushing\r\n% some height values upward so that the resulting surface has regional\r\n% minima only at specified locations.  Minima imposition performs this\r\n% modification while preserving the local height ordering relationships\r\n% between neighboring pixels.\r\n%\r\n% Here's a one-dimensional example to illustrate.\r\n\r\na = [1 2 3 4 3 4 3 2 1]\r\n\r\n%%\r\n% The vector |a| has three different regional minima locations:\r\n\r\nimregionalmin(a)\r\n\r\n%%\r\n% Let's make a mask representing just two locations where we want to have\r\n% regional minima:\r\n\r\nmask = [1 0 0 0 0 0 0 0 1]\r\n\r\n%%\r\n% This mask says we only want regional minima at each end of the vector, so\r\n% we want the regional minima in the middle to go away somehow. We do this\r\n% using the Image Processing Toolbox function |imimposemin|.\r\n\r\nimimposemin(a, mask)\r\n\r\n%%\r\n% The new result has regional minima only at the desired locations.  The\r\n% local minima in the middle of the vector has been pushed up to the level\r\n% of the surround elements.  (As I write this, I am noticing that the small\r\n% fractional bump that |imimposemin| gives to all the middle elements\r\n% seems possibly unnecessary. This is something I will investigate at\r\n% another time.)\r\n%\r\n% In my previous post I created a binary image representing an ocean mask:\r\n\r\ns = load('continental_divide');\r\ndem = s.dem_cropped;\r\nocean_mask = s.ocean_mask;\r\nimshow(ocean_mask, 'InitialMagnification', 'fit')\r\n\r\n%%\r\n% I can use |imimposemin|, together with the |ocean_mask| image, to modify\r\n% the DEM so that the two oceans are the *only* regional minima.\r\n\r\ndem_modified = imimposemin(dem, ocean_mask);\r\n\r\n%%\r\n% It doesn't look much different:\r\n\r\nimshow(dem_modified, [-500 3000], 'InitialMagnification', 'fit')\r\n\r\n%%\r\n% But we can verify that now there are only two regional minima:\r\n\r\nregional_minima_labeled = bwlabel(imregionalmin(dem_modified));\r\nmax(regional_minima_labeled(:))\r\n\r\n%%\r\nimshow(label2rgb(regional_minima_labeled), 'InitialMagnification', 'fit')\r\n\r\n%%\r\n% I'll add the modified DEM to the data I've been collecting in\r\n% continental_divide.mat:\r\n\r\nsave continental_divide dem_modified -append\r\n\r\n%%\r\n% Next time I'll perform the final step in computing the continental divide\r\n% and show one way to visualize it using transparency.\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 ##### 05cb77041a8545869c5021f100fa31ff\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   Today, in part 5 of my series on computing the US continental divide, we'll look at a technique called minima imposition. Previously we looked at data import and display, the watershed transform... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2009\/05\/22\/continental-divide-5-imposemin\/\">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":[166,148,366,36,152,362,122,553],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/261"}],"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=261"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/261\/revisions"}],"predecessor-version":[{"id":3633,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/261\/revisions\/3633"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=261"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=261"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=261"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}