This is more a theoretical question than practical, and I'm referring to 3x3 convolution kernels with online demo and the first comment after it.

I have a raster elevation model and want to turn it into slope/gradient using convolution. The article gives me kernels to use for horizontal gradient detection and vertical gradient detection.

I use them and so have two convoluted DEMs; one with horizontal gradient and one with vertical. To get the full effect I must merge them together.

The comment underneath the article suggests that to merge these I should use the equation:

pixel = SQRT(pixel_convolved_along_x ^ 2 + pixel_convolved_along_y ^ 2)

I do that and the result looks good. In fact I compare it to a slope generated directly by a slope-creation tool and the results look identical.

So I have a process that works and a result I'm happy with - I just don't understand why that equation is used to merge cells. It looks like Pythagoras to me (a=sqrt(b^2+c^2)).

So why that and why not simply average the two cells using (a+b)/2?

Can anyone help me understand this?

1 Answer 1


Okay - you have the horizontal and vertical gradients. Based on those you want to calculate the total gradient. Consider it as a triangle, with horizontal on one axis, and vertical on the other. The total gradient is then calculated by Pythagoras, which you noted yourself.

Now, consider a case where you have a low vertical gradient (for example 5) and a high horizontal gradient (for example 70). With the average approach, the combined gradient would be "medium" in size (75/2 = 37.5) , which seems wrong, as you want the total gradient. With the Pythagoras / triangle view of things, the total gradient is 70.1

Viewing it as triangles, gives you are more representative value of the slope allowing for the dominating angle in the above example to "shine". All in all, spatial data is often well represented by the principles of trigonometry, as triangles (and friends) can with relative ease be used to construct an applicable simplification of the real world.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.