I'm having a depth image, say `z = f(x, y)`

with `(x, y)`

in pixels. I want to calculate the normal vector at each pixel to create the normal map.

I've been trying the approach at Calculate surface normals from depth image using neighboring pixels cross product. In this approach, basically the "input" to the cross product is two tangent vectors, which are the gradients `(dz/dx)`

and `(dy/dx)`

. These two tangent vectors will form the tangent plane at point `(x, y, f(x, y))`

, and then the cross product will find the normal vector to this plane.

However, I don't understand why the normal vector to this plane (3D plane `(x, y, f(x, y))`

will also be the normal vector to the plane in world coordinates that I'm trying to find. Is there any assumption here? How can this approach be used to find the normal vector at each pixel?

That is almost by definition of the normal to a surface. The normal is locally perpendicular to the tangent plane. And the cross-product of two non-collinear vectors is a vector that is perpendicular to the two vectors. That is why the cross-product of two non-collinear vectors of the tangent plane is perpendicular to that tangent plane. And thus it is along the normal direction.