How does the below calculation help to create a PNG from a floating point depth map?

What is the effect of max_val * (depth - depth_min) / (depth_max - depth_min) in the below function?

def write_depth(path, depth, bits=2):
"""Write depth map to pfm and png file.

Args:
path (str): filepath without extension
depth (array): depth
"""
write_pfm(path + ".pfm", depth.astype(np.float32))

depth_min = depth.min()
depth_max = depth.max()

max_val = (2**(8*bits))-1

if depth_max - depth_min > np.finfo("float").eps:
out = max_val * (depth - depth_min) / (depth_max - depth_min)
else:
out = np.zeros(depth.shape, dtype=depth.type)

if bits == 1:
cv2.imwrite(path + ".png", out.astype("uint8"))
elif bits == 2:
cv2.imwrite(path + ".png", out.astype("uint16"))

return

My question is not about syntax but about the reason or the effect of that calculation?

1. What effect on the array does subtracting the depth_min have? Other than changing the mean and median. Why do this step?
2. What effect on the array does multiplying by max_val have?
3. What effect on the array does dividing by the range have?
4. Why do this instead of just converting each floating point to an integer?

I have seen the output of this function, which is a depth map where lighter gray pixels denote close objects and darker pixels denote far away objects.

• it is a normalisation factor, to take values in the range [depth_min, depth_max] and project them to values in the range [0, max_value] uniformly Jan 27 '21 at 10:00