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

Performing ICP in C++ with two .bag files

asked 2017-09-19 02:49:29 -0500

jrgen gravatar image

updated 2017-09-19 10:52:40 -0500

bvbdort gravatar image

Hey there,

I have a problem with ROS. I want to perform the Iterative Closest Point Algorithm between to point clouds. Therefore I wrote the following code:

ros::Publisher pub;
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_in;

//Festlegung der fixed_frame_msg
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 );


//Festlegung des camera_frame_msg
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, "icp");
  ros::NodeHandle n( "~" );

  // Create ROS subscriber for fixed_frame topic
  ros::Subscriber sub = n.subscribe("input/raw",1,fixed_frame_cb);

  // Create ROS subscriber for camera_frame topic
  ros::Subscriber sub2 = n.subscribe("input/raw",1,camera_frame_cb);

  // Create ROS publisher for transformed pointcloud
  pub = n.advertise<sensor_msgs::PointCloud2>("transformed_point_cloud",1);

  // Spin
  ros::spin ();

Now I want to play the two bag-files with the following command:

rosbag play -l 2017-06-07-13-49-48.bag 2017-06-07-14-05-40.bag /camera/depth/points:=icp/input/raw

If I want to display the result in RViz, the display remains empty.

Does anyone know the problem and can help me? I am new to ROS and C++

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2020-12-29 07:09:38 -0500

ulashmetalcrush gravatar image

updated 2020-12-29 07:15:29 -0500

Have you checked if the topic is published via

rostopic hz your_topic_name.

Also make sure that the fields are not empty (Data part of the message).

I am asking this because there is a possibility that your topic is publishing but it isn't displayed in rviz. You are missing the 'header' part of the message which is quite crucial for displaying on rviz.

After the lines:

// Convert the pcl/PointCloud to sensor_msgs/PointCloud2
sensor_msgs::PointCloud2 output;
pcl::toROSMsg( *cloud2_in, output );

add the lines:

output.header.stamp = ros::Time::now();
output.header.frame_id = "fixed_frame"; // You can change this for your needs

and setting the fixed_frame in rviz to fixed_frame have to display your message if your message isn't empty.

Another possible issue might arise due to cloud_in container being empty. This can happen if camera_frame_cb is called before fixed_frame_cb(You are filling the container at the fixed_frame_cb). It might be a good idea to check the size of the data in the cloud_in container, or just keep a boolean to indicate that it is initialized.

Just a final note, you are converting your input to output at the lines below, I am guessing you want to publish aligned pointCloud.

pcl::toROSMsg( *cloud2_in, output );
edit flag offensive delete link more

Question Tools



Asked: 2017-09-19 02:49:29 -0500

Seen: 687 times

Last updated: Dec 29 '20