点云不更新

Point Cloud not updating

我是点云处理的新手。我正在尝试将激光雷达点云与已与激光雷达同步的相机捕获的图像数据对齐。作为第一步,我想使用下面的脚本渲染我的激光雷达点云但不更新它。 viewer-> spin() 似乎不起作用。任何帮助将不胜感激。

typedef pcl::PointCloud<pcl::PointXYZ> PointCloud;
static const std::string OPENCV_WINDOW = "Image window";
using namespace sensor_msgs;
using namespace message_filters;

void callback(const ImageConstPtr& image1, 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);
   pcl::visualization::CloudViewer viewer ("Simple Cloud Viewer");
   viewer.showCloud(temp_cloud);
   while (!viewer.wasStopped ())
   {
   }
 }
int main(int argc, char** argv)
{
  ros::init(argc, argv, "vision_node");
  ros::NodeHandle nh;
  message_filters::Subscriber<Image> image1_sub(nh, "/pylon_camera_node/image_rect_color", 1);
  message_filters::Subscriber<sensor_msgs::PointCloud2> lidar_sub(nh, "/os1_cloud_node/points", 1);
  typedef sync_policies::ApproximateTime<Image, PointCloud2> MySyncPolicy;
  Synchronizer<MySyncPolicy> sync(MySyncPolicy(10), image1_sub, lidar_sub);
  sync.registerCallback(boost::bind(&callback, _1, _2));
  ros::spin();
  return 0;
}

这是一个简化的示例,说明您的控件需要如何流动。它不包含与您拥有的图像的同步(我没有示例数据来测试它)。但我认为它告诉你你需要什么。正如评论中所说,关键是只创建一次查看器,然后在新数据到达时用它更新它。

#include <ros/ros.h>
#include <pcl/point_cloud.h>
#include <pcl_conversions/pcl_conversions.h>
#include <pcl/visualization/cloud_viewer.h>
#include <sensor_msgs/PointCloud2.h>

pcl::visualization::CloudViewer viewer ("Simple Cloud Viewer");

/** receives the point cloud from the subscriber and updates the viewer with it */
void callback(const sensor_msgs::PointCloud2ConstPtr& cloud_msg) {
  pcl::PCLPointCloud2* cloud = new pcl::PCLPointCloud2;
  pcl_conversions::toPCL(*cloud_msg, *cloud);
  pcl::PointCloud<pcl::PointXYZ>::Ptr temp_cloud(new pcl::PointCloud<pcl::PointXYZ>);
  pcl::fromPCLPointCloud2(*cloud, *temp_cloud);
  viewer.showCloud(temp_cloud);
}

int main(int argc, char** argv) {
  ros::init(argc, argv, "vision_node");
  ros::NodeHandle nh;
  ros::Subscriber sub = nh.subscribe("/points", 1, callback);
  ros::spin();
  return 0;
}