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

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

username: ptrblck
cooked: <p>Usually you would have to project the point cloud onto an image plane using the focal lengths and center position. <a href="https://stackoverflow.com/a/46845240" rel="nofollow noopener">Here</a> is some code for the PCL.</p>

username: c91628b816a93eaa4325
cooked: <p>I have implemented it on C++.<br>
But I don’t know how to implement it in Python. Can I use PCL in a python environment?</p>

username: ptrblck
cooked: <p>Apparently there are some PCL Python packages:</p>
<ul>
<li><a href="https://pypi.org/project/pclpy/" rel="nofollow noopener">pclpy</a></li>
<li><a href="https://github.com/strawlab/python-pcl" rel="nofollow noopener">python-pcl</a></li>
</ul>

username: c91628b816a93eaa4325
cooked: <p>I implemented it on python.</p>
<pre><code class="lang-auto">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
        continue
    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)
img_depth_file.convert('RGB').save(os.path.join(output_path,pcd_name+str(pcd_number)+'_depth_image.png'))
</code></pre>
<p>refer : <a href="https://stackoverflow.com/questions/42430479/how-to-extract-depth-information-from-the-3d-point-cloud-data?fbclid=IwAR37ukkHd2_dp0tV8g-oRiXGHguSHbXG8YbmMwn8ZPeACxkAU2jXbPnmpc8" rel="nofollow noopener">https://stackoverflow.com/questions/42430479/how-to-extract-depth-information-from-the-3d-point-cloud-data?fbclid=IwAR37ukkHd2_dp0tV8g-oRiXGHguSHbXG8YbmMwn8ZPeACxkAU2jXbPnmpc8</a></p>

username: mjjdick
cooked: <aside class="onebox whitelistedgeneric">
  <header class="source">
      <img src="https://github.githubassets.com/favicons/favicon.svg" class="site-icon" width="16" height="16">
      <a href="https://github.com/weidezhang/pypcd_python3.6/tree/ebd6096e9c0778e5cd159430b2ef7335b2e55660" target="_blank" rel="nofollow noopener">GitHub</a>
  </header>
  <article class="onebox-body">
    <img src="https://avatars0.githubusercontent.com/u/455617?s=400&amp;v=4" class="thumbnail onebox-avatar" width="60" height="60">

<h3><a href="https://github.com/weidezhang/pypcd_python3.6/tree/ebd6096e9c0778e5cd159430b2ef7335b2e55660" target="_blank" rel="nofollow noopener">weidezhang/pypcd_python3.6</a></h3>

<p>pypcd for python3 changes . Contribute to weidezhang/pypcd_python3.6 development by creating an account on GitHub.</p>


  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>

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