可视化点云

7
我有一些从gpu::reprojectImageTo3D得来的3D点云数据,想要将其显示出来。如何将这个点云数据从OpenCV转换成sensor_msgs/PointCloud2格式?我不需要发布点云数据,只是用于调试可视化。可能可以像使用节点的图像那样显示它吗?例如使用pcl库?这样做是最佳选择,因为我的设备可能无法很好地处理RViz(基于网上的读数)。
1个回答

4
我最好的猜测是这样做,只需迭代遍历cv::mat并插入pcl以转换为消息,因为我没有找到直接转换的方法。
#include <ros/ros.h>
// point cloud headers
#include <pcl/point_cloud.h>
//Header which contain PCL to ROS and ROS to PCL conversion functions
#include <pcl_conversions/pcl_conversions.h>
//sensor_msgs header for point cloud2
#include <sensor_msgs/PointCloud2.h>
main (int argc, char **argv)
{
    ros::init (argc, argv, "pcl_create");
    ROS_INFO("Started PCL publishing node");
    ros::NodeHandle nh;
    //Creating publisher object for point cloud
    ros::Publisher pcl_pub = nh.advertise<sensor_msgs::PointCloud2> ("pcl_output", 1);
    //Creating a cloud object
    pcl::PointCloud<pcl::PointXYZ> cloud;
    //Creating a sensor_msg of point cloud
    sensor_msgs::PointCloud2 output;
    //Insert cloud data
    cloud.width  = 50000;
    cloud.height = 2;
    cloud.points.resize(cloud.width * cloud.height);
    //Insert random points on the clouds
    for (size_t i = 0; i < cloud.points.size (); ++i)
    {
        cloud.points[i].x = 512 * rand () / (RAND_MAX + 1.0f);
        cloud.points[i].y = 512 * rand () / (RAND_MAX + 1.0f);
        cloud.points[i].z = 512 * rand () / (RAND_MAX + 1.0f);
    }
    //Convert the cloud to ROS message
    pcl::toROSMsg(cloud, output);
    output.header.frame_id = "point_cloud";
    ros::Rate loop_rate(1);
    while (ros::ok())
    {
        //publishing point cloud data
        pcl_pub.publish(output);
        ros::spinOnce();
        loop_rate.sleep();
    }
    return 0;
}

这段代码片段来源于apprize

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接