{"id":138,"date":"2007-05-31T07:34:44","date_gmt":"2007-05-31T11:34:44","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/2007\/05\/31\/upslope-area-part-4\/"},"modified":"2019-10-23T12:56:54","modified_gmt":"2019-10-23T16:56:54","slug":"upslope-area-part-4","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2007\/05\/31\/upslope-area-part-4\/","title":{"rendered":"Upslope area &#8211; Plateau detection"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p>In my <a href=\"https:\/\/blogs.mathworks.com\/steve\/2007\/05\/18\/upslope-area-part-3\/\">previous upslope area post<\/a>, I showed this graphic of pixel flow around North Pond in Milford, Massachusetts:\r\n   <\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2007\/May\/upslope_part3_07.png\"> <\/p>\r\n   <p>Notice that pixels in the pond have no arrows.  In fact, the <tt>pixelFlow<\/tt> M-file I showed previously is unable to compute a pixel flow direction or magnitude for these pixels because they are in\r\n      a flat region, or <i>plateau<\/i>. More specifically, <tt>pixelFlow<\/tt> returns a flow direction of <tt>NaN<\/tt> for any pixel that has no downhill neighbor.\r\n   <\/p>\r\n   <p>There is a simple way to find all such pixels using morphological erosion. Erosion with a flat structuring element is equivalent\r\n      to a local minimum operator.  If the minimum value of a pixel and its neighbors equals the pixel itself, then we'll call it\r\n      a plateau pixel.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">s = load(<span style=\"color: #A020F0\">'upslope_sample_dem'<\/span>);\r\npond = s.Zm(140:280, 160:230);\r\nplateaus = imerode(pond, ones(3,3)) == pond;\r\nimshow(plateaus, <span style=\"color: #A020F0\">'InitialMagnification'<\/span>, <span style=\"color: #A020F0\">'fit'<\/span>)\r\ntitle(<span style=\"color: #A020F0\">'Plateaus'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/138\/upslope_area_4_01.png\"> <p>I'd like to classify plateaus into three types:<\/p>\r\n   <div>\r\n      <ul>\r\n         <li>plateaus with downhill but no uphill neighbors; these are called <i>regional maxima<\/i><\/li>\r\n         <li>plateaus with uphill but no downhill neighbors; these are called <i>regional minima<\/i><\/li>\r\n         <li>plateaus with both uphill and downhill neighbors<\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <p>The Image Processing Toolbox has functions that find regional maxima and regional minima:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">max_plateaus = imregionalmax(pond);\r\nimshow(max_plateaus, <span style=\"color: #A020F0\">'InitialMagnification'<\/span>, <span style=\"color: #A020F0\">'fit'<\/span>)\r\ntitle(<span style=\"color: #A020F0\">'Max plateaus'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/138\/upslope_area_4_02.png\"> <pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">min_plateaus = imregionalmin(pond);\r\nimshow(min_plateaus, <span style=\"color: #A020F0\">'InitialMagnification'<\/span>, <span style=\"color: #A020F0\">'fit'<\/span>)\r\ntitle(<span style=\"color: #A020F0\">'Min plateaus'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/138\/upslope_area_4_03.png\"> <p>We can now identify pixels in plateaus with both uphill and downhill neighbors by using logical operators:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">mid_plateaus = plateaus &amp; ~(max_plateaus | min_plateaus);\r\nimshow(mid_plateaus, <span style=\"color: #A020F0\">'InitialMagnification'<\/span>, <span style=\"color: #A020F0\">'fit'<\/span>)\r\ntitle(<span style=\"color: #A020F0\">'Mid plateaus'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/138\/upslope_area_4_04.png\"> <p>Next time I'll work on a procedure for calculating flow directions for these different kinds of plateau pixels.<\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_33eeb25ad41144fca8fc13275ba6145b() {\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='33eeb25ad41144fca8fc13275ba6145b ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 33eeb25ad41144fca8fc13275ba6145b';\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 2007 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_33eeb25ad41144fca8fc13275ba6145b()\"><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.4<br><\/p>\r\n<\/div>\r\n<!--\r\n33eeb25ad41144fca8fc13275ba6145b ##### SOURCE BEGIN #####\r\n%%\r\n% In my <https:\/\/blogs.mathworks.com\/steve\/2007\/05\/18\/upslope-area-part-3\/ \r\n% previous upslope area post>, I showed this graphic of pixel flow around\r\n% North Pond in Milford, Massachusetts:\r\n%\r\n% <<https:\/\/blogs.mathworks.com\/images\/steve\/2007\/May\/upslope_part3_07.png>>\r\n%\r\n% Notice that pixels in the pond have no arrows.  In fact, the |pixelFlow|\r\n% M-file I showed previously is unable to compute a pixel flow direction or\r\n% magnitude for these pixels because they are in a flat region, or\r\n% _plateau_. More specifically, |pixelFlow| returns a flow direction of\r\n% |NaN| for any pixel that has no downhill neighbor.\r\n%\r\n% There is a simple way to find all such pixels using morphological\r\n% erosion. Erosion with a flat structuring element is equivalent to a local\r\n% minimum operator.  If the minimum value of a pixel and its neighbors\r\n% equals the pixel itself, then we'll call it a plateau pixel.\r\n\r\ns = load('upslope_sample_dem');\r\npond = s.Zm(140:280, 160:230);\r\nplateaus = imerode(pond, ones(3,3)) == pond;\r\nimshow(plateaus, 'InitialMagnification', 'fit')\r\ntitle('Plateaus')\r\n\r\n%%\r\n% I'd like to classify plateaus into three types:\r\n%\r\n% * plateaus with downhill but no uphill neighbors; these are called\r\n% _regional maxima_\r\n% * plateaus with uphill but no downhill neighbors; these are called\r\n% _regional minima_\r\n% * plateaus with both uphill and downhill neighbors\r\n%\r\n% The Image Processing Toolbox has functions that find regional maxima and\r\n% regional minima:\r\n\r\nmax_plateaus = imregionalmax(pond);\r\nimshow(max_plateaus, 'InitialMagnification', 'fit')\r\ntitle('Max plateaus')\r\n\r\n%%\r\nmin_plateaus = imregionalmin(pond);\r\nimshow(min_plateaus, 'InitialMagnification', 'fit')\r\ntitle('Min plateaus')\r\n\r\n%%\r\n% We can now identify pixels in plateaus with both uphill and downhill \r\n% neighbors by using logical operators:\r\n\r\nmid_plateaus = plateaus & ~(max_plateaus | min_plateaus);\r\nimshow(mid_plateaus, 'InitialMagnification', 'fit')\r\ntitle('Mid plateaus')\r\n\r\n%%\r\n% Next time I'll work on a procedure for calculating flow directions\r\n% for these different kinds of plateau pixels.\r\n\r\n##### SOURCE END ##### 33eeb25ad41144fca8fc13275ba6145b\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   In my previous upslope area post, I showed this graphic of pixel flow around North Pond in Milford, Massachusetts:\r\n   \r\n    \r\n   Notice that pixels in the pond have no arrows.  In fact, the... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2007\/05\/31\/upslope-area-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":[9],"tags":[246,364,366,36,362,52],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/138"}],"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=138"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/138\/revisions"}],"predecessor-version":[{"id":3534,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/138\/revisions\/3534"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=138"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=138"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}