Ask Your Question

Extracting the (x,y,z) coordinates from pointcloud2 data in Python

asked 2016-03-31 14:56:09 -0500


I'm in the process of using a stereo camera that generates a pointcloud2 sensor message. I can open it up in rviz, and view the pointcloud. now I want to try and use the uint8[] data for a system I'm working on. I have a Python subscription node that can subscribe to the proper topic as well as print the data inside the script. I just need to know how to get from this huge data string to a useable (x, y, z) format or numpy array in camera space for me to do something useful for it.

I did stumble across python-pcl by strawlab, but I'm not sure if that's what I need to use, or what?

Looking forward to some help!

Thanks in advance!

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted

answered 2018-08-28 16:54:05 -0500

saltus gravatar image

I know I'm a little late here, but a function is included in ros_numpy package for this exact purpose.

An example implementation:

import ros_numpy
xyz_array = ros_numpy.point_cloud2.get_xyz_points(pointcloud2_array)
edit flag offensive delete link more

answered 2016-04-02 07:19:12 -0500

lucasw gravatar image

import sensor_msgs.point_cloud2
for point in sensor_msgs.point_cloud2.read_points(msg, skip_nans=True):
            pt_x = point[0]
            pt_y = point[1]
            pt_z = point[2]
edit flag offensive delete link more


I belive x y and z cordinates are not same with the frame of the Camera, I am using this lines of code as below:

    number_of_sampling = 30
    x_total = 0
    y_total = 0
    z_total = 0
    counter = 0

    for point in sensor_msgs.point_cloud2.read_points(data, skip_nans=True):

        y_total += point[0]
        z_total += point[1]
        x_total += point[2]
##        print x_total, y_total, z_total      
        counter += 1
        if counter == number_of_sampling: break

    x_average = x_total / number_of_sampling
    y_average = -1*y_total / number_of_sampling
    z_average = -1*z_total / number_of_sampling

In other words 0 index is Y 1st index is Z and 2nd index is X

Meric gravatar image Meric  ( 2019-03-22 08:14:50 -0500 )edit

answered 2019-11-21 14:17:50 -0500

Markus gravatar image
 sudo apt install ros-kinetic-ros-numpy 

import ros_numpy

def callback_pcl(pc2_msg):
    xyz_array = ros_numpy.point_cloud2.pointcloud2_to_xyz_array(pc2_msg)
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2016-03-31 14:56:09 -0500

Seen: 13,064 times

Last updated: Nov 21 '19