{"id":259,"date":"2009-05-08T07:00:50","date_gmt":"2009-05-08T11:00:50","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/2009\/05\/08\/continental-divide-3-regmin\/"},"modified":"2019-10-28T15:31:47","modified_gmt":"2019-10-28T19:31:47","slug":"continental-divide-3-regmin","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2009\/05\/08\/continental-divide-3-regmin\/","title":{"rendered":"Locating the US continental divide,  part 3 &#8211; Regional minima"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p>In the previous post in this <a href=\"https:\/\/blogs.mathworks.com\/steve\/category\/continental-divide\/\">series<\/a> on computing the US continental divide, we used the <tt>watershed<\/tt> function to find catchment basins in a DEM (digital elevation model). The result was more than half a million catchment basins,\r\n      most of which were tiny and looked something like this:\r\n   <\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2009\/continental_divide_part_2_watershed_03.png\"> <\/p>\r\n   <p>Why so many?<\/p>\r\n   <p>To answer this question, let's look at a different kinds of <i>minima<\/i> in image processing.  A <i>local minimum<\/i> is usually defined as a pixel that is less than or equal to all its neighbors.  In a one-dimensional example:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">a = [3 2 1 1 1 2 3 4 3 5 6]<\/pre><pre style=\"font-style:oblique\">\r\na =\r\n\r\n     3     2     1     1     1     2     3     4     3     5     6\r\n\r\n<\/pre><p>the third, fourth, fifth, and nineth elements are local minima.  A convenient way to find local minima is to use morphological\r\n      erosion:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">imerode(a, [1 1 1]) == a<\/pre><pre style=\"font-style:oblique\">\r\nans =\r\n\r\n     0     0     1     1     1     0     0     0     1     0     0\r\n\r\n<\/pre><p>Now find the local minima in this example:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">b = [2 1 1 2 3 4 4 4 5 6]<\/pre><pre style=\"font-style:oblique\">\r\nb =\r\n\r\n     2     1     1     2     3     4     4     4     5     6\r\n\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">imerode(b, [1 1 1]) == b<\/pre><pre style=\"font-style:oblique\">\r\nans =\r\n\r\n     0     1     1     0     0     0     1     1     0     0\r\n\r\n<\/pre><p>Now, if you are thinking about the watershed transform and identifying catchment basins, I think you'll agree there's an important\r\n      distinction between the two groups of local minima (the 1-valued elements and the 4-valued elements) found in <tt>b<\/tt>.  Water would not accumulate on the flat spot, or plateau, represented by the 4-valued elements; it would instead flow downhill\r\n      to the left.\r\n   <\/p>\r\n   <p>That thought leads us to the definition of a <i>regional minimum<\/i>: a connected group of pixels from which you cannot travel further downhill without going uphill first.  The 1-valued elements\r\n      in <tt>b<\/tt> form a regional minima, but the 4-valued elements in <tt>b<\/tt> do not.\r\n   <\/p>\r\n   <p>The Image Processing Toolbox function <tt>imregionalmin<\/tt> locates regional minima for you:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">imregionalmin(b)<\/pre><pre style=\"font-style:oblique\">\r\nans =\r\n\r\n     0     1     1     0     0     0     0     0     0     0\r\n\r\n<\/pre><p>Let's connect that back to the watershed transform. Every regional minimum is a collection point for water flowing downhill.\r\n      Thus, there's a one-to-one correspondence between regional minima and catchment basins found by the watershed transform.\r\n   <\/p>\r\n   <p>Here are all the regional minima pixels in the zoomed-in portion of the DEM that we looked at previously.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #228B22\">% The file continental_divide.mat was created in an earlier post.<\/span>\r\ns = load(<span style=\"color: #A020F0\">'continental_divide'<\/span>);\r\ndem = s.dem_cropped;\r\nreg_min = imregionalmin(dem);\r\nimshow(reg_min, <span style=\"color: #A020F0\">'InitialMagnification'<\/span>, <span style=\"color: #A020F0\">'fit'<\/span>);\r\naxis([5580 5670 2060 2100])<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2009\/continental_divide_part_3_regional_minima_01.jpg\"> <p>We can count the number of regional minima by doing connected-component labeling on the output of <tt>imregionalmin<\/tt>.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">reg_min_labeled = bwlabel(reg_min);\r\nmax(reg_min_labeled(:))<\/pre><pre style=\"font-style:oblique\">\r\nans =\r\n\r\n      540976\r\n\r\n<\/pre><p>That's the same as the number of catchment basins we found in the previous post.<\/p>\r\n   <p>We're on our way to computing just two catchment basins, one for the Pacific Ocean and one for the Atlantic Ocean.  Next time\r\n      we'll take a closer look at the ocean pixels in this data set.\r\n   <\/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_4b72ae540c5347b5afde3bbb557dc3cd() {\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='4b72ae540c5347b5afde3bbb557dc3cd ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 4b72ae540c5347b5afde3bbb557dc3cd';\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_4b72ae540c5347b5afde3bbb557dc3cd()\"><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\n4b72ae540c5347b5afde3bbb557dc3cd ##### SOURCE BEGIN #####\r\n%%\r\n% In the previous post in this \r\n% <https:\/\/blogs.mathworks.com\/steve\/category\/continental-divide\/ series> on\r\n% computing the US continental divide, we used the |watershed| function to\r\n% find catchment basins in a DEM (digital elevation model). The result was\r\n% more than half a million catchment basins, most of which were tiny and\r\n% looked something like this:\r\n%\r\n% <<https:\/\/blogs.mathworks.com\/images\/steve\/2009\/continental_divide_part_2_watershed_03.png>>\r\n%\r\n% Why so many?\r\n%\r\n% To answer this question, let's look at a different kinds of _minima_ in\r\n% image processing.  A _local minimum_ is usually defined as a pixel that\r\n% is less than or equal to all its neighbors.  In a one-dimensional\r\n% example:\r\n\r\na = [3 2 1 1 1 2 3 4 3 5 6]\r\n\r\n%%\r\n% the third, fourth, fifth, and nineth elements are local minima.  A\r\n% convenient way to find local minima is to use morphological erosion:\r\n\r\nimerode(a, [1 1 1]) == a\r\n\r\n%%\r\n% Now find the local minima in this example:\r\n\r\nb = [2 1 1 2 3 4 4 4 5 6]\r\n\r\n%%\r\n\r\nimerode(b, [1 1 1]) == b\r\n\r\n%%\r\n% Now, if you are thinking about the watershed transform and identifying\r\n% catchment basins, I think you'll agree there's an important distinction\r\n% between the two groups of local minima (the 1-valued elements and the\r\n% 4-valued elements) found in |b|.  Water would not accumulate on the flat\r\n% spot, or plateau, represented by the 4-valued elements; it would instead\r\n% flow downhill to the left.\r\n%\r\n% That thought leads us to the definition of a _regional minimum_: a\r\n% connected group of pixels from which you cannot travel further downhill\r\n% without going uphill first.  The 1-valued elements in |b| form a regional\r\n% minima, but the 4-valued elements in |b| do not.\r\n%\r\n% The Image Processing Toolbox function |imregionalmin| locates regional\r\n% minima for you:\r\n\r\nimregionalmin(b)\r\n\r\n%%\r\n% Let's connect that back to the watershed transform. Every regional\r\n% minimum is a collection point for water flowing downhill. Thus, there's a\r\n% one-to-one correspondence between regional minima and catchment basins\r\n% found by the watershed transform.\r\n%\r\n% Here are all the regional minima pixels in the zoomed-in portion of the\r\n% DEM that we looked at previously.\r\n\r\n% The file continental_divide.mat was created in an earlier post.\r\ns = load('continental_divide'); \r\ndem = s.dem_cropped;\r\nreg_min = imregionalmin(dem);\r\nimshow(reg_min, 'InitialMagnification', 'fit');\r\naxis([5580 5670 2060 2100])\r\n\r\n%%\r\n% We can count the number of regional minima by doing connected-component\r\n% labeling on the output of |imregionalmin|.\r\n\r\nreg_min_labeled = bwlabel(reg_min);\r\nmax(reg_min_labeled(:))\r\n\r\n%%\r\n% That's the same as the number of catchment basins we found in the\r\n% previous post.\r\n%\r\n% We're on our way to computing just two catchment basins, one for the\r\n% Pacific Ocean and one for the Atlantic Ocean.  Next time we'll take a\r\n% closer look at the ocean pixels in this data set.\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 ##### 4b72ae540c5347b5afde3bbb557dc3cd\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   In the previous post in this series on computing the US continental divide, we used the watershed function to find catchment basins in a DEM (digital elevation model). The result was more than... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2009\/05\/08\/continental-divide-3-regmin\/\">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":[50,166,246,366,36,362,122],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/259"}],"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=259"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/259\/revisions"}],"predecessor-version":[{"id":3627,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/259\/revisions\/3627"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=259"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=259"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=259"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}