# Reconstructing a height map from a normal map

2014-08-20 Permalink

At times it happens that we have the normal map but not the height/displacement map that was used to create it. For some rendering techniques, like parallax mapping, it is the height map which is needed. In addition, having the height map is beneficial for editing the normal map.

Recovering the height map boils down to integrating the normal map. This is what is suggested in multiple places around the web (e.g. here, here and here), but it is not explained how to do this in practice.

The problem is that the normal map at hand is typically encoded with low precision, 8-bits per channel usually, and might be compressed with a lossy algorithm afterwards. In the end, the line integrals along closed curves aren’t zero, and so the end result significantly depends on the curves along which we integrate. In fact, no matter how we integrate, we end up with significant noise in the output.

## Through deconvolution

Denote by f(x, y) the height map and n(x, y) = (n_{x}, n_{y}, 1) the normal map that was calculated by some finite difference scheme, that is by convolving the original height map with the kernels d_{x} and d_{y}. For the sake of boundary condition, we assume that both f and n are periodic of periods w and h. We seek a function f which minimizes:

^{−1}( ǁd

_{x}∗ f − n

_{x}ǁ

^{2}+ ǁd

_{y}∗ f − n

_{y}ǁ

^{2})

Taking the Fourier transform F, D_{x}, D_{y}, N_{x}, N_{y} of f, d_{x}, d_{y}, n_{x}, n_{y} respectively, and applying Parseval's theorem:

^{−1}( ǁD

_{x}F − N

_{x}ǁ

^{2}+ ǁD

_{y}F − N

_{y}ǁ

^{2})

The last expression can be minimized by each F(x,y) independently. The minimum is achieved by:

_{x}|

^{2}+ |D

_{y}|

^{2})

^{−1}(conj(D

_{x}) N

_{x}+ conj(D

_{y}) N

_{y})

For when |D_{x}|^{2} + |D_{y}|^{2} = 0, E is independent of F(x,y), and so we can choose any value of the later. In such cases we seek the minimum norm solution, which is achieved by setting F = 0 at these points.

Applying the inverse Fourier transform to F leads to the minimum error, minimum norm solution f.

## Implementation

A command line utility implementing the above is available at debump.c.