Rosanswers logo


I am currently trying to extract the world co-ordinates from a pointcloud reconstructed scene. I am currently using ROS-Hydro.

I have followed these two as my reference. http://answers.ros.org/question/98011/how-to-convert-pclpointcloud2-to-pointcloudt-in-hydro/ http://wiki.ros.org/pcl_ros

I am subscribing to the pointcloud message generated by the proc stereo_img_proc.

My code is something like this.

#include <ros/ros.h>
#include <sensor_msgs/PointCloud2.h>
#include <pcl/conversions.h> 
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <opencv/highgui.h>
#include <cv_bridge/cv_bridge.h>
#include <iostream>
#include <pcl/io/pcd_io.h>
#include <boost/foreach.hpp>
#include <pcl/PCLPointCloud2.h>
#include <pcl_conversions/pcl_conversions.h>

void cloud_cb (const sensor_msgs::PointCloud2 pc)
pcl::PCLPointCloud2 pcl_pc;
pcl_conversions::toPCL(pc, pcl_pc);
pcl::PointCloud<pcl::PointXYZ> cloud;
pcl::fromPCLPointCloud2(pcl_pc, cloud);
for(int i=0;i<cloud.height;i++)  
    for(int j=0; j<cloud.height;j++)    
         BOOST_FOREACH (const pcl::PointXYZ& pt, cloud.points);


I want to have the depth value for every pixel and then publish it in form of a sensor_msgs::Image message. Please guide me.


I have made some modifications.

for(int j=0; j<cloud.points.size();j++)    
    float x = cloud.points[j].x;
    float y = cloud.points[j].y;
    float z = cloud.points[j].z;

where x,y,z are the world co-ordinates as extracted from cloud. Will this work??

Originally posted by m1ckey on ROS Answers with karma: 21 on 2014-09-11

Post score: 2


1 Answer 1


Rosanswers logo

The 'world coordinates' of a pointcloud are represented in the transform data. If your pointcloud is in the sensor frame, and you have a tf transform (link) available to a map or world frame, you can use the tf library to transform the pointcloud into the desired frame, which will transform the positions of the points in the pointcloud.

The image you're talking about sounds like a projection of the pointcloud down to a 2D plane, which you can do by iterating over every point in the cloud (which you're already doing) and applying some linear algebra (look into the Eigen library). To actually build the image you'll probably find OpenCV and this conversion useful, however you could build the sensor_msgs/Image directly.

EDIT: Found this projection API in PCL as well which should make it easier.

Originally posted by paulbovbel with karma: 4518 on 2014-09-14

This answer was ACCEPTED on the original site

Post score: 1

Original comments

Comment by m1ckey on 2014-09-16:

Actually my point is I want to extract the depth or Z coordinate from a PCL and in place of the intensity value of the pixel I want to replace the same with the Z co-ordinate value.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.