ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Performing ICP between Pointclouds in C++

asked 2013-04-09 07:43:52 -0600

mdegges gravatar image

updated 2014-01-28 17:16:07 -0600

ngrennan gravatar image


I'm trying to create a script that takes in 2 bag files (fixed_frame and camera_frame), converts the ROS sensor_msgs/PointCloud2 data to pcl/PointCloud data, performs ICP between the pointcloud in fixed_frame and all the pointclouds in camera_frame, and publishes the results. Here is my code so far:

// Includes listed here

ros::Publisher _pub;
pcl::PointCloud<pcl::PointXYZ> cloud_in; 

void fixed_frame_cb (const sensor_msgs::PointCloud2ConstPtr& pc2_msg) {

  // Convert the sensor_msgs/PointCloud2 data to pcl/PointCloud
  pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_in(
                           new pcl::PointCloud<pcl::PointXYZ>
  pcl::fromROSMsg ( *pc2_msg, *cloud_in );


void camera_frame_cb (const sensor_msgs::PointCloud2ConstPtr& next_pc2_msg) {

  // Convert the sensor_msgs/PointCloud2 data to pcl/PointCloud
  pcl::PointCloud<pcl::PointXYZ>::Ptr cloud2_in(
                           new pcl::PointCloud<pcl::PointXYZ>
  pcl::fromROSMsg ( *next_pc2_msg, *cloud2_in );

  // Perform ICP
  pcl::IterativeClosestPoint<pcl::PointXYZ, pcl::PointXYZ> icp;
  pcl::PointCloud<pcl::PointXYZ> Final;

  // Convert the pcl/PointCloud to sensor_msgs/PointCloud2
  sensor_msgs::PointCloud2 output;
  pcl::toROSMsg( *cloud2_in, output );
  // Publish the results
  _pub.publish( output );


int main (int argc, char** argv) {

  // Initialize ROS
  ros::init (argc, argv, "my_script");
  ros::NodeHandle n( "~" );

  // Create ROS subscriber for fixed_frame topic
  ros::Subscriber sub = n.subscribe(

  // Create ROS subscriber for camera_frame topic
  ros::Subscriber sub2 = n.subscribe(

  // Create ROS publisher for transformed pointcloud
  _pub = n.advertise<sensor_msgs::PointCloud2>(

  // Spin
  ros::spin ();

I'm getting a lot of errors when I run 'make'. New to ros and c++, so I think these might just be syntax problems. Can anyone point me in the right direction? Thanks in advance.

Make errors:

mdegges@ubuntu:~/ros_workspace/iml_ros/my_script$ make mkdir -p bin cd build && cmake -Wdev -DCMAKE_TOOLCHAIN_FILE=/opt/ros/groovy/share/ros/core/rosbuild/rostoolchain.cmake .. [rosbuild] Building package my_script -- Using CATKIN_DEVEL_PREFIX: /home/mdegges/ros_workspace/iml_ros/my_script/build/devel -- Using CMAKE_PREFIX_PATH: /opt/ros/groovy -- This workspace overlays: /opt/ros/groovy -- Found gtest sources under '/usr/src/gtest': gtests will be built -- catkin 0.5.63 [rosbuild] Including /opt/ros/groovy/share/roscpp/rosbuild/roscpp.cmake [rosbuild] Including /opt/ros/groovy/share/rospy/rosbuild/rospy.cmake -- Configuring done -- Generating done CMake Warning: Manually-specified variables were not used by the project:


-- Build files have been written to: /home/mdegges/ros_workspace/iml_ros/my_script/build cd build && make make[1]: Entering directory /home/mdegges/ros_workspace/iml_ros/my_script/build' make[2]: Entering directory /home/mdegges/ros_workspace/iml_ros/my_script/build' make[3]: Entering directory /home/mdegges/ros_workspace/iml_ros/my_script/build' make[3]: Leaving directory /home/mdegges/ros_workspace/iml_ros/my_script/build' [ 0%] Built target rospack_genmsg_libexe make[3]: Entering directory /home/mdegges/ros_workspace/iml_ros/my_script/build' make[3]: Leaving directory /home/mdegges/ros_workspace/iml_ros/my_script/build' [ 0%] Built target rosbuild_precompile make[3]: Entering directory /home/mdegges/ros_workspace/iml_ros/my_script/build' make[3]: Leaving directory /home/mdegges/ros_workspace/iml_ros/my_script/build' make[3]: Entering directory `/home/mdegges/ros_workspace/iml_ros/my_script/build' [100%] Building CXX object CMakeFiles/my_script.dir/src/example.cpp.o /home/mdegges/ros_workspace/iml_ros/my_script/src/example.cpp: In function ‘void camera_frame_cb(const PointCloud2ConstPtr ...

edit retag flag offensive close merge delete


Please edit your question with the errors you're seeing. It is very hard to help you without seeing what the problem is.

Dan Lazewatsky gravatar image Dan Lazewatsky  ( 2013-04-09 08:28:38 -0600 )edit

Ok- I posted the make errors on pastie:

mdegges gravatar image mdegges  ( 2013-04-09 15:57:06 -0600 )edit

cannot open the page on pastie. why don't you just paste the error here? and you should also provide you CMakeLists.txt file

yangyangcv gravatar image yangyangcv  ( 2013-04-09 16:00:13 -0600 )edit

Sorry- edited the question with the errors included. The CMakeLists.txt file is the default, except I added a line at the bottom to make the script executable.

mdegges gravatar image mdegges  ( 2013-04-09 18:58:21 -0600 )edit

1 Answer

Sort by » oldest newest most voted

answered 2013-04-11 12:29:00 -0600

mdegges gravatar image

Figured it out. I just had some syntax errors- for ex, when I defined the global variable 'cloud_in', I forgot to make it a pointer.

edit flag offensive delete link more


How did you get the two .bag files into your program? I tried rosbag play -l 2017-06-07-13-49-48.bag 2017-06-07-14-05-40.bag /camera/depth/points:=icp/input/raw But that doesn't work.

jrgen gravatar image jrgen  ( 2017-09-29 03:27:11 -0600 )edit

Question Tools

1 follower


Asked: 2013-04-09 07:43:52 -0600

Seen: 2,286 times

Last updated: Apr 09 '13