Steve on Image Processing

October 16th, 2007

Upslope area - handling NaNs

Since I originally posted my upslope toolbox to MATLAB Central back in August, I have heard from some experts about an issue related to NaNs in the DEM data. Specifically, some data sets record elevation only for DEM pixels within a particular catchment basin. Pixels outside the catchment basin typically are NaN. Some of my code doesn't handle NaN values in the DEM.

After some e-mail correspondence, I settled on this approach:

  • In the pixel flow direction computation, replace groups of NaN pixels that touch the border by a high value. (You can read about the technique for find such pixels in this post.) This replacement makes the flow direction on the watershed pixels point inward, toward the catchment basin.
  • When solving the flow matrix for upslope area, use 0 instead 1 on the right-hand side for exterior NaN pixels. This means that such pixels don't contribute any flow to the system.

Here's a simple example:

[x,y] = meshgrid(-3:3);
E = hypot(x,y);
E(abs(x) + abs(y) > 3) = NaN
E =

       NaN       NaN       NaN    3.0000       NaN       NaN       NaN
       NaN       NaN    2.2361    2.0000    2.2361       NaN       NaN
       NaN    2.2361    1.4142    1.0000    1.4142    2.2361       NaN
    3.0000    2.0000    1.0000         0    1.0000    2.0000    3.0000
       NaN    2.2361    1.4142    1.0000    1.4142    2.2361       NaN
       NaN       NaN    2.2361    2.0000    2.2361       NaN       NaN
       NaN       NaN       NaN    3.0000       NaN       NaN       NaN

R = dem_flow(E);
T = flow_matrix(E, R);
A = upslope_area(E, T)
A =

         0         0         0    1.0000         0         0         0
         0         0    1.0000    2.0000    1.0000         0         0
         0    1.0000    1.8112    4.1888    1.8112    1.0000         0
    1.0000    2.0000    4.1888   25.0000    4.1888    2.0000    1.0000
         0    1.0000    1.8112    4.1888    1.8112    1.0000         0
         0         0    1.0000    2.0000    1.0000         0         0
         0         0         0    1.0000         0         0         0

Notice that the upslope area at the lowest pixel is 25. This is the number of non-NaN pixels in the DEM.

I uploaded version 1.2 of the upslope toolbox, with these NaN-related changes, to the MATLAB Central File Exchange in early October. If you have data sets that use NaNs outside the catchment basin of interest, you might want to download the new version.


Get the MATLAB code

Published with MATLAB® 7.5

2 Responses to “Upslope area - handling NaNs”

  1. Wolfgang replied on :

    Hi Steve.

    Thanks for the great upslope area blog. I hope, geospatial analysis will always be an integral part in Matlab. In case you need another idea for a geo-related blog category, what about one about TOPOGRID, a method to grid elevation data from irregularly distributed elevation data in a hydrologically correct way. You’ll find out more about it in Hutchinson, M. F. (1989), J. of. Hydrology, 211-232. Like the upslope topic, it could be nicely handled in combination with the image processing toolbox.

    Best regards,
    Wolfgang

  2. Steve replied on :

    Wolfgang—Thanks for the suggestion!

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


Steve Eddins manages the Image & Geospatial development team at The MathWorks and coauthored Digital Image Processing Using MATLAB. He writes here about image processing concepts, algorithm implementations, and MATLAB.

  • Steve: Kezia—Try imrotate.
  • kezia: steve, how to perform rotation of structuring element by 15 degrees. kindly answer my question. thank u kezia...
  • Steve: Tasha—I only accept comments that are relevant to the particular blog post or are questions or comments...
  • Tasha: Steve,I send you a comment here but still didn’t get any reply yet.I did not see my comment posted here...
  • Steve: Carsten—Thanks for your input.
  • Carsten: Another vote for either imtranslate.m, or at least a blurb in the imtransform help why pure translation...
  • Loren Shure: If you look towards the end of the fftfilt program, you will see that there’s a check to see if...
  • Steve: Sonja—My imwritesize submission on the MATLAB Central File Exchange might be helpful. It was posted...
  • Steve: Grant—Sorry, but it won’t be for R2010a. That development deadline has already passed.
  • Sonja: My publisher is wanting images for a new book to be 300 dpi. Only 5 of the 19 images are 300, the rest are...

These postings are the author's and don't necessarily represent the opinions of The MathWorks.