A better approach is to use the depth differenceinterpolated through a few pixels and clamped to some fixed value for the edge detection. Also not perfect, but will provide similar results and sharper textures.