Steve on Image Processing

July 27th, 2007

Upslope area - Recursive calculation procedure

So far in this series I've talked about calculating pixel flow directions and handling plateaus, but I haven't yet discussed the actual upslope area calculation. The Tarboton paper presents a recursive formulation of the upslope area calculation (from page 313):

  Procedure DPAREA(i, j)
    if AREA(i, j) is known
      then
        no action
      else
        AREA(i, j) = 1 (the area of a single pixel)
        for each neighbor (location in, jn)
          p = proportion of neighbor (in, jn) that drains to
              pixel (i, j) based on angle
          if (p > 0) then
            call DPAREA(in, jn) (this is the recursive call to
                 calculate the area for the neighbor)
            AREA(i, j) = AREA(i, j) + p x AREA(in, jn)
  Return

I had to read the surrounding text a couple of times to figure out exactly how the author intends to calculate p. Consider the flow direction for a particular neighbor, (in, jn). If the flow direction points directly at pixel (i, j), then the corresponding weight p is 1. In other words, pixel (i, j) gets all of the flow from (in, jn). If the flow direction is pi/4 or greater away from the direction toward (i, j), then the weight p is 0. Pixel (i, j) gets none of the flow from (in, jn). In between an angular difference of 0 and pi/4, the weight p varies proportionally between 1 and 0.

For example, consider the computation of upslope area for pixel number 5 in this set of 9 pixels:

   1 4 7
   2 5 8
   3 6 9

If the flow direction for pixel 2 is zero radians, its flow is pointing directly at pixel 5, so the corresponding weight is 1.0. If the flow direction for pixel 8 is pi/4 radians, then its flow is pointing directly at pixel 4. The corresponding weight for pixel 5 is 0.0. If the flow direction is pi/8 radians, its flow is pointing between pixels 5 and 4, and the weight for the pixel 5 computation is 0.5.

It also took me a few minutes to figure out how to compute the radial difference between two angles, properly taking into account the 2*pi periodicity of angles. (For example, the radial difference between pi/8 and 2*pi - pi/8 should be pi/4.) Here's what I came up with:

   radial_difference = abs(mod(theta1 - theta2 + pi), 2*pi) - pi)

(Does anyone else have a better way to compute this quantity?) To calculate the weight for a given neighbor of pixel (i,j), first determine the angle that points directly from that neighbor to the pixel (i, j). Call that angle theta_c. Then find the flow direction for that neighbor; call it theta_f. Now compute the radial difference:

   radial_difference = abs(mod(theta_c - theta_f + pi), 2*pi) - pi)

And finally compute the weight:

   p = max(1 - (radial_difference * 4 / pi), 0)

So, are we ready to start writing a recursive MATLAB function based on DPAREA above? Nope! I don't actually want to use a recursive formulation at all.

Here's a different way to think about it:

  AREA(i,j) = 1 + p1*AREA(i1,j1) + p2*AREA(i2,j2) + ... + p8*AREA(i8,j8)

If we write down this equation for each pixel in the image, we'll end up with a system of N linear equations in N unknowns. Although the system is very large (N-by-N, where N is the number of pixels), it is also very sparse, because each equation involves no more than nine of the unknowns.

So we are getting very close to calculating upslope area. We "just" have to set up an extremely large sparse system of equations and then solve it.

Next time I'll tackle that.


Get the MATLAB code

Published with MATLAB® 7.4

6 Responses to “Upslope area - Recursive calculation procedure”

  1. nikzad babaii replied on :

    Dear Steve Eddins,
    Thanks for your algorithm description.
    Could you please let me know how can I find the Matlab Code for your algorithm(finding pixel directions)?
    wishes
    nikzad babaii

  2. Steve replied on :

    Nikzad—See part 2 of this series for the pixel flow direction code. When I finish the series, I’ll put the relevant code onto MATLAB Central.

  3. Priti replied on :

    Hi Steve..I have a quick question not related to the topic you have posted…I m currently working on Content Based Retrieval of Videos….and I m looking for implemetation of SR trees/R trees/R* trees/SS trees or any spatial data structures in MATLAB as my source code for feature extraction is in MATLAB…I could find only C++ libraries available for that…I m not sure if there is any way to import that to MATLAB and use it or are there any coded versions available in MATLAB itself….Thanks in advance…Priti

  4. Mark Andrews replied on :

    If by “better” you mean “more expensive” then on my lazy days I do this:

    diff = abs( phase( exp(i*theta1)/exp(i*theta2) ) );

    :-)

  5. Steve replied on :

    Mark—I like it! :-)

  6. Steve replied on :

    Priti—If you have the Image Processing Toolbox, you can find a kd-tree implementation in toolbox/images/images/private. Beyond that I don’t know. Check the MATLAB Central File Exchange.

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.

  • Sana: hi steve, could you explain to me how i would be able to use the dir function, to do a loop through a directory...
  • Nishtha: Sir, I have preprocessed the image in following steps: [1] adaptive histogram equalization [2] thresholding...
  • Kristof: I also strongly support the idea. I have just recently bumped into the problem that im2single was not...
  • Steve: David—I’ m glad you found it useful!
  • David Lalejini: I found your example very useful for finding connected nodes in a large set of input pairs. I start...
  • tommy: Dear Steve, I have a question,please if you are kind to help me regarding the accumulator array dimensions of...
  • Steve: Abc—I don’t know how to distinguish the faces. You might try posting your question in the MATLAB...
  • Manju: well if we have a few ovals within each other like in a cell how do we measure the distance from the center...
  • Steve: Manju—What do you mean? How is each region defined?
  • Manju: if we have 2-3 regions within each other how do we measure the regions of each one?

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