Is there a pcl library for python?

Issue #65 resolved
Ganesan Rajaraman created an issue

import pcl throws below error

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named pcl

Issue 17 is showing usage of pcl in C++. How do we achieve the same thing in Python?

Comments (4)

  1. Chris Osterwood

    Ganesan,

    It appears there is a python PCL package here:
    https://strawlab.github.io/python-pcl/
    https://github.com/strawlab/python-pcl

    But I have never used it, so tell you how to convert from Python ROS objects into the PCL data types using it.

    When we use Python with ROS, we generally write out PLY files directly from Python code as it is very simple file format. PCD files have a similar structure, so you may find that an acceptable approach.

    To convert from a ROS message to a Python data structure, you can use code like what I have included below. Note that this exact code excerpt is untested, and relies on a internal library we have for creating PLY files -- but it may be a useful starting point for you.

    import struct
    
    try:
        import rospy
        import rosbag
    except:
        raise Exception("Error importing ROS. Source the ROS environment" \
                       +" in the workspace where the MultiSense stack is located")
    
    def process_camera_points2(bag_file_path):
    
        file_count = 0
        color_point = False
    
        bag = rosbag.Bag(bag_file_path)
        topics = ['/multisense/image_points2_color', '/multisense/image_points2']
    
        for topic, msg, t in bag.read_messages(topics=topics):
            file_count += 1
    
            header = {}
            header['seq']   = msg.header.seq
            header['secs']  = msg.header.stamp.secs
            header['nsecs'] = msg.header.stamp.nsecs
    
            if msg.is_bigendian:
                if msg.fields[3].name == 'rgb':
                    color_point = True
                    fmt = '>3fBBBB'
                else:
                    fmt = '>3fB'
            else:
                if msg.fields[3].name == 'rgb':
                    color_point = True
                    fmt = '<3fBBBB'
                else:
                    fmt = '<3fB'
    
            data = []
    
            for idx in range(msg.width):
                point_data = msg.data[msg.point_step*idx:msg.point_step*(idx+1)]
    
                if color_point:
                    x, y, z, b, g, r, _ = struct.unpack(fmt, point_data)
                else:
                    x, y, z, i = struct.unpack(fmt, point_data)
    
                # Skip invalid range points (0,0,0) -- sometimes the first points2 topic is all zeros
                if (x == 0.0 and y == 0.0 and z == 0.0):
                    continue
    
                if color_point:
                    data.append([x,y,z, int(r), int(g), int(b)])
                else:
                    # Scale intensity to range of 0.0 <> 1.0
                    data.append([x,y,z,float(i)/256.0])
    
            ply.write("cloud_%d.ply" % file_count, data, header)
    
  2. Log in to comment