How to convert the depth map to 3D point clouds?

d19911222 picture d19911222 · Apr 1, 2018 · Viewed 10.2k times · Source

When I convert depth map to 3D point cloud, I found there is a term called scaling factor. Can anyone give me some idea what scaling factor actually is. Is there any relationship between scaling factor and focal length. The code is as follows:

import argparse
import sys
import os
from PIL import Image

focalLength = 938.0
centerX = 319.5
centerY = 239.5
scalingFactor = 5000

def generate_pointcloud(rgb_file,depth_file,ply_file):

    rgb = Image.open(rgb_file)
    depth = Image.open(depth_file).convert('I')

    if rgb.size != depth.size:
        raise Exception("Color and depth image do not have the same 
resolution.")
    if rgb.mode != "RGB":
        raise Exception("Color image is not in RGB format")
    if depth.mode != "I":
        raise Exception("Depth image is not in intensity format")


    points = []    
    for v in range(rgb.size[1]):
        for u in range(rgb.size[0]):
            color = rgb.getpixel((u,v))
            Z = depth.getpixel((u,v)) / scalingFactor
            print(Z)
            if Z==0: continue
            X = (u - centerX) * Z / focalLength
            Y = (v - centerY) * Z / focalLength
            points.append("%f %f %f %d %d %d 0\n"% 

Answer

taketwo picture taketwo · Apr 2, 2018

In this context "scaling factor" refers to the relation between depth map units and meters; it has nothing to do with the focal length of the camera.

Depth maps are typically stored in 16-bit unsigned integers at millimeter scale, thus to obtain Z value in meters, the depth map pixels need to be divided by 1000. You have a somewhat unconventional scaling factor of 5000, meaning that the unit of your depth maps is 200 micrometers.