Search code examples
matlabmatrixangletopography

Calculate angle of matrix with topography for each triangle


I have a matrix with data of a topography, let's say several hills. I want to have information of the angle of each data point to the vertical line. Here are two examples:

  1. If I consider a place near the foot of the hill that is totally flat, I have a degree of 90° (90° to the vertical line).
  2. If I am at the steepest point of the hill, I have a lower angle of let's say 50°.

To compute this I guess I have to to connect all the topography data so that (at least) three near pixels form triangle. After that I have to compute the angle(s) of this triangle.

Can I use a existing algorithm?


Solution

  • If your heightmap is a matrix A then you could approximate the the gradient components of every inner point (without the egdes) by

      Xgrad = (A(2:end-1,3:end)-A(2:end-1,1:end-2))/2;
      Ygrad = (A(3:end,2:end-1)-A(1:end-2,2:end-1))/2;
    

    The angulars will then be

      deg = (pi/2 - atan(sqrt(Xgrad.^2 + Ygrad.^2),1))/pi*180;
    

    Depending on your heightmap, differentiating numerically can produce "fuzzy" results. Maybe you have to do some blur-filtering in order to produce smoother gradients.