How do I read a pcd (point cloud data) file using python and convert point cloud to depth image?

I now have a pcd profile, I hope I can read it using python.
But I have not learned anything about pcl. I don’t know how to convert this data into a depth image.
Hope someone can help me with it.
I am using version python 3.6. The data set is Cornell’s grasp dataset.

Usually you would have to project the point cloud onto an image plane using the focal lengths and center position. Here is some code for the PCL.

I have implemented it on C++.
But I don’t know how to implement it in Python. Can I use PCL in a python environment?

Apparently there are some PCL Python packages:

I implemented it on python.

import os
import math
import numpy as np
from PIL import Image

pcd_path = "input_path"
pcd_name = "pcd"
pcd_mat = ".txt"
pcd_number = 1000

with open(pcd_path+ pcd_name+str(pcd_number) + pcd_mat, "r") as pcd_file:
    lines = [line.strip().split(" ") for line in pcd_file.readlines()]

img_height = 480
img_width = 640
is_data = False
min_d = 0
max_d = 0
img_depth = np.zeros((img_height, img_width), dtype='f8')
for line in lines:
    if line[0] == 'DATA':  # skip the header
        is_data = True
    if is_data:
        d = max(0., float(line[2]))
        i = int(line[4])
        col = i % img_width
        row = math.floor(i / img_width)
        img_depth[row, col] = d
        min_d = min(d, min_d)
        max_d = max(d, max_d)

max_min_diff = max_d - min_d

def normalize(x):
    return 255 * (x - min_d) / max_min_diff
normalize = np.vectorize(normalize, otypes=[np.float])
img_depth = normalize(img_depth)
img_depth_file = Image.fromarray(img_depth)

refer :

I found this python file and test ok, you can use this file to load pcd file just like pcl

1 Like