# Sobel filter for normals from height field?

This topic is 4743 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Anyone know how this works? I see how a Sobel filter works for edge detection but I'm not so clear on how to generate normals with it.

##### Share on other sites
You just generate an X and Z gradient (assuming Y is up) and then normalize.

normal.x = sobel_u( pos );normal.y = scale;normal.z = sobel_v( pos );normalize( normal )

"sobel_u()" looks something like:
 1  0 -1  3  0 -3 1  0 -1

"sobel_v()" looks something like:
 1  3  1  0  0  0-1 -3 -1

You may have to negate the matrices depending on your local coordinate systems.

##### Share on other sites
Quote:
 Original post by ScythenAnyone know how this works?I see how a Sobel filter works for edge detection but I'm not so clear on how to generate normals with it.

Then you don't know how it really works for edge detection either. You can paramatise (is that the right word?) the general PDE's into difference equations. This can be expressed as a convolution of a window over the image, who's parameters are the coefficients of the terms of the difference equation. I can provide the full maths when I get home from work if you like.

Also, IIRC the standard Sobel kernel has a centre weight of 2, not 3. Not that it really matters though.

##### Share on other sites
An explanation of the maths would be great.
I think the code I was looking at was optimized leaving out parts of the original equations.

##### Share on other sites
Sobel operator are a kind of 2D filter kernel. So its mathematics is that of 2D discrete convolution
r(x,y) := ∑i,j p(x-i,y-j) * k(i,j)
(if I recall right; please verify it before use) what means that the kernel k is layed over the original image p at the location (x,y), so that one kernel element covers one pixel. Then the "pixelwise" sum over i,j defines the result r at the location (x,y).

In general the one Sobel operator lets remain horizonzal edges, while the other lets remain vertical edges, both in form of (signed) bitmaps. The amplitude of the resulting "pixels" depend in some way on the sharpness of the edges. Looking at both values together one could understand that at a normal w.r.t. to the standard normal straight high.

EDIT: Look here
http://mathworld.wolfram.com/Convolution.html
for a nice animation of (not discrete but continuos) convolution.

##### Share on other sites
Quote:
 Original post by haegarrSobel operator are a kind of 2D filter kernel. So its mathematics is that of 2D discrete convolutionr(x,y) := ∑i,j p(x-i,y-j) * k(i,j)(if I recall right; please verify it before use) what means that the kernel k is layed over the original image p at the location (x,y), so that one kernel element covers one pixel. Then the "pixelwise" sum over i,j defines the result r at the location (x,y).In general the one Sobel operator lets remain horizonzal edges, while the other lets remain vertical edges, both in form of (signed) bitmaps. The amplitude of the resulting "pixels" depend in some way on the sharpness of the edges. Looking at both values together one could understand that at a normal w.r.t. to the standard normal straight high.

While this explains the idea of convolution on an image, it doesn't explain how the sobel operator works. I should be home in about an hour, so I'll post the maths then - which is really quite simple. That's if I can find my notes on it that is [wink].

##### Share on other sites
If you need source, you can find it in ATI's RenderMonkey ...

- Wolf

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 14
• 14
• 45
• 22
• 27
• ### Forum Statistics

• Total Topics
634044
• Total Posts
3015211
×