pointcloud to image conversion
Hello everyone, I am converting pointcloud to image and I can't view it in RVIZ. Am I converting it into image properly? My code is as follows:
#include <ros/ros.h>
#include <pcl/point_cloud.h>
#include <pcl_conversions/pcl_conversions.h>
#include <sensor_msgs/PointCloud2.h>
#include <sensor_msgs/Image.h>
main (int argc, char **argv)
{
ros::init (argc, argv, "pcl_create");
ros::NodeHandle nh;
ros::Publisher pcl_pub = nh.advertise<sensor_msgs::PointCloud2> ("pcl_output", 1);
ros::Publisher image_pub = nh.advertise<sensor_msgs::Image> ("/image_topic", 30);
pcl::PointCloud<pcl::PointXYZRGB> cloud;
sensor_msgs::PointCloud2 output;
sensor_msgs::Image image;
// Fill in the cloud data
cloud.width = 10000;
cloud.height = 1;
cloud.points.resize(cloud.width * cloud.height);
for (size_t i = 0; i < cloud.points.size (); ++i)
{
cloud.points[i].x = 1024 * rand () / (RAND_MAX + 1.0f);
cloud.points[i].y = 1024 * rand () / (RAND_MAX + 1.0f);
cloud.points[i].z = 1024 * rand () / (RAND_MAX + 1.0f);
cloud.points[i].r = 255;
cloud.points[i].g = 255;
cloud.points[i].b = 255;
}
//Convert the cloud to ROS message
pcl::toROSMsg(cloud, output);
pcl::toROSMsg (cloud, image); //convert the cloud
output.header.frame_id = "odom";
image.header.frame_id = "odom";
ros::Rate loop_rate(1);
while (ros::ok())
{
pcl_pub.publish(output);
image_pub.publish (image); //publish our cloud image
ros::spinOnce();
loop_rate.sleep();
}
return 0;
}
Rviz output is as follows:
Were you able to figure out how to fix this? I have a similar issue and I am unable to convert the sensor msgs back to cv_image using cvbridge because of this. I am using ROS 1 Melodic