Search code examples
image-processingcomputer-visiongeometrycomputational-geometry

Something weird is happening when computing surface normal map


I am using the following code to generate a surface normal map from a depth image:

for x in range(depth.shape[0]):
    for y in range(depth.shape[1]):
        try:
            dzdx=(depth[x+1,y,0]-depth[x-1,y,0])/2
            dzdy=(depth[x,y+1,0]-depth[x,y+1,0])/2
        except:
            dzdx=0
            dzdy=0
        sub=np.asarray([-dzdx,-dzdy,1])
        
        normals[x,y,:]=sub/np.linalg.norm(sub)

any thoughts as to what is going on?


Solution

  • You have a bug in the line:

    dzdy=(depth[x,y+1,0]-depth[x,y+1,0])/2
    

    it should be

    dzdy=(depth[x,y+1,0]-depth[x,y-1,0])/2