Ask Your Question
6

Conversion from sensor_msgs::PointCloud2 to pcl::PointCloud<T>

asked 2014-03-06 23:04:47 -0500

madmage gravatar image

updated 2014-03-07 05:05:38 -0500

dornhege gravatar image

Hi all, I am digging deep in this forum, but I'm getting lost. I am using ROS Groovy and receiving a sensor_msgs::PointCloud2 from a depth_image_proc nodelet and I want to process it using PCL 1.7. None of the solutions found in this forum are working for me, i.e., I miss some function prototype of toPCL, fromROSMsg, etc. functions.

I would like to do something like:

void CloudViewerPlugin::pointcloudCallback(const sensor_msgs::PointCloud2::ConstPtr& msg) {
    pcl::PointCloud<pcl::PointXYZ> cloud;
    pcl::PCLPointCloud2 pcl_pc;
    pcl_conversions::toPCL(*msg, pcl_pc);
    pcl::fromPCLPointCloud2(pcl_pc, cloud);

or

void CloudViewerPlugin::pointcloudCallback(const sensor_msgs::PointCloud2::ConstPtr& msg) {
    pcl::PointCloud<pcl::PointXYZ> cloud;
    pcl::fromROSMsg(*msg, cloud);

but I found no function prototype to do this.

edit retag flag offensive close merge delete

Comments

Can you try to directly subscribe to a pcl pointcloud? There might be some pcl_ros magic going on that allows you to do so.

dornhege gravatar imagedornhege ( 2014-03-06 23:18:15 -0500 )edit

Hi Christian, I already tried that solution, but: /opt/ros/groovy/include/ros/message_traits.h:121: error: ‘__s_getMD5Sum’ is not a member of ‘pcl::PointCloud<pcl::pointxyz>’ on the subscribe line (I'm following http://wiki.ros.org/pcl_ros)

madmage gravatar imagemadmage ( 2014-03-07 01:17:44 -0500 )edit

Sorry, I hadn't #included the right files, however, now there are two errors: /opt/ros/groovy/include/pcl_ros/point_cloud.h:176: error: no matching function for call to ‘createMapping(std::vector<sensor_msgs::pointfield_<std::allocator<void> > >&, pcl::MsgFieldMap&)’ and: /opt/ros/groovy/include/ros/serialization.h:134: error: ‘struct pcl::PCLHeader’ has no member named ‘deserialize’

madmage gravatar imagemadmage ( 2014-03-07 01:23:19 -0500 )edit

3 Answers

Sort by » oldest newest most voted
13

answered 2014-12-07 18:03:51 -0500

bzr gravatar image

Here's the code that successfully transforms my sensor_msgs::PointCloud2::ConstPtr to a pcl::PointCloud<pcl::pointxyz>::Ptr

#include <pcl_conversions/pcl_conversions.h>
#include <pcl/point_types.h>
#include <pcl/PCLPointCloud2.h>
#include <pcl/conversions.h>
#include <pcl_ros/transforms.h>

 void cloud_cb(const boost::shared_ptr<const sensor_msgs::PointCloud2>& input){

    pcl::PCLPointCloud2 pcl_pc2;
    pcl_conversions::toPCL(*input,pcl_pc2);
    pcl::PointCloud<pcl::PointXYZ>::Ptr temp_cloud(new pcl::PointCloud<pcl::PointXYZ>);
    pcl::fromPCLPointCloud2(pcl_pc2,*temp_cloud);
    //do stuff with temp_cloud here
    }

Some of the includes may be unnecessary ....

edit flag offensive delete link more

Comments

and what if i want to go the other way around? from pointXYZ to sensor_msgs? do i still have to go through pcl::PointCloud2? Can you please write the code for that.

mimpho gravatar imagemimpho ( 2015-05-30 10:30:47 -0500 )edit

Have you tested this?Because I did exactly what you have written and the temp_cloud is utterly broken.When I pass it to any PCL function that works with pcl::PointCloud<pcl::pointxyz>::Ptr I get an immediate segmentation fault.The cloud I'm receiving is fine plus no errors during the build process.

rbaleksandar gravatar imagerbaleksandar ( 2015-08-19 10:49:08 -0500 )edit

CMakeFiles/listener.dir/src/listener.cpp.o: In function `void pcl::detail::FieldMapper<pcl::pointxyz>::operator()<pcl::fields::x>()': listener.cpp:(.text._ZN3pcl6detail11FieldMapperINS_8PointXYZEEclINS_6fields1xEEEvv[_ZN3pcl6detail11FieldMapperINS_8PointXYZEEclINS_6fields1xEEEvv]+0x2a3): undefined r

rasoo gravatar imagerasoo ( 2016-06-28 06:24:50 -0500 )edit

eference to pcl::console::print(pcl::console::VERBOSITY_LEVEL, char const*, ...)' CMakeFiles/listener.dir/src/listener.cpp.o: In functionvoid pcl::detail::FieldMapper<pcl::pointxyz>::operator()<pcl::fields::y>()':

rasoo gravatar imagerasoo ( 2016-06-28 06:25:23 -0500 )edit

listener.cpp:(.text._ZN3pcl6detail11FieldMapperINS_8PointXYZEEclINS_6fields1yEEEvv[_ZN3pcl6detail11FieldMapperINS_8PointXYZEEclINS_6fields1yEEEvv]+0x2a3): undefined reference to `pcl::console::print(pcl::console::VERBOSITY_LEVEL, char const*, ...)'

rasoo gravatar imagerasoo ( 2016-06-28 06:25:41 -0500 )edit

CMakeFiles/listener.dir/src/listener.cpp.o: In function `void pcl::detail::FieldMapper<pcl::pointxyz>::operator()<pcl::fields::z>()': listener.cpp:(.text._ZN3pcl6detail11FieldMapperINS_8PointXYZEEclINS_6fields1zEEEvv[_ZN3pcl6detail11FieldMapperINS_8PointXYZEEclINS_6fields1zEEEvv]+0x2a3): undefined r

rasoo gravatar imagerasoo ( 2016-06-28 06:25:56 -0500 )edit

Heyii, I am also trying to do the similar things. But I am getting the above errors. Sorry for the inconvenience. I am pretty new with ROS and PCL.Can you please help me? What kind of error these are showing.. ?

rasoo gravatar imagerasoo ( 2016-06-28 06:30:20 -0500 )edit
2

Here is a project of mine. I'm actually going to convert this to a more generic package and perhaps even propose to include it in ROS since there are not out-of-the-box, easy-to-use packages.

rbaleksandar gravatar imagerbaleksandar ( 2016-06-28 09:45:51 -0500 )edit
0

answered 2017-04-19 09:26:10 -0500

zubair gravatar image
pcl::PCLPointCloud2 pcl_pc2;
pcl_conversions::toPCL(*cloud_msg,pcl_pc2);
pcl::PointCloud<pcl::PointXYZ>::Ptr pt_cloud(new pcl::PointCloud<pcl::PointXYZ>);
pcl::fromPCLPointCloud2(pcl_pc2,*pt_cloud);

hi what i can do with pt_cloud to get the x y and z values of my point cloud ??

please help

edit flag offensive delete link more

Comments

sorry a bit late: You would access x y z like:

 float x = pt_cloud->points[p].x;
 float y = pt_cloud->points[p].y;
 float z = pt_cloud->points[p].z;

// do something with x, y, z

Wi55@m gravatar imageWi55@m ( 2017-10-25 04:11:17 -0500 )edit

What's p here? A loop variable, or?

Jägermeister gravatar imageJägermeister ( 2019-05-23 06:43:20 -0500 )edit
0

answered 2018-04-26 05:11:24 -0500

lile gravatar image

Hi everyone: when i use the code below: oid cloud_cb(const boost::shared_ptr<const sensor_msgs::pointcloud2="">& input){

pcl::PCLPointCloud2 pcl_pc2;
pcl_conversions::toPCL(*input,pcl_pc2);
pcl::PointCloud<pcl::PointXYZ>::Ptr temp_cloud(new pcl::PointCloud<pcl::PointXYZ>);
pcl::fromPCLPointCloud2(pcl_pc2,*temp_cloud);
//do stuff with temp_cloud here
}

i got the error: #error USE_ROS setup requires PCL to compile against ROS message headers, which is now deprecated

what it means, thanks?

edit flag offensive delete link more

Comments

i have found the problem. in CMakeLists.txt, just commit this line:

#add_definitions("-DUSE_ROS")

and them it works

lile gravatar imagelile ( 2018-04-26 06:01:32 -0500 )edit

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

Stats

Asked: 2014-03-06 23:04:47 -0500

Seen: 43,533 times

Last updated: Apr 19 '17