简体   繁体   English

PCL:可视化点云

[英]PCL: Visualize a point cloud

I'm trying to visualize a point cloud using PCL CloudViewer.我正在尝试使用PCL CloudViewer 可视化点云。 The problem is that I'm quite new to C++ and I have found two tutorials first demonstrating the creation of PointCloud and second demonstrating the visualization of a PointCloud.问题是我对 C++ 很陌生,我找到了两个教程, 第一个演示了 PointCloud 的创建, 第二个演示了 PointCloud 的可视化。 However, I'm not able to combine these two tutorials.但是,我无法将这两个教程结合起来。

Here is what I have come up with:这是我想出的:

#include <iostream>

#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/visualization/cloud_viewer.h>

int main (int argc, char** argv)
{
  pcl::PointCloud<pcl::PointXYZ> cloud;
  
  // Fill in the cloud data
  cloud.width    = 5;
  cloud.height   = 1;
  cloud.is_dense = false;
  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);
  }

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

  while (!viewer.wasStopped ())
  {
  }

  return (0);
}

but that even do not compile:但即使不编译:

error: no matching function for call to   
‘pcl::visualization::CloudViewer::showCloud(pcl::PointCloud<pcl::PointXYZ>&)’

Your error message tells you what you need to do:您的错误消息告诉您需要做什么:

error: no matching function for call to ‘pcl::visualization::CloudViewer::showCloud(pcl::PointCloud<pcl::PointXYZ>&)’

So go to the documentation for CloudViewer and see what arguments this member function takes: http://docs.pointclouds.org/1.5.1/classpcl_1_1visualization_1_1_cloud_viewer.html因此,转到 CloudViewer 的文档并查看此成员函数采用哪些参数: http : //docs.pointclouds.org/1.5.1/classpcl_1_1visualization_1_1_cloud_viewer.html

There we see that it takes a const MonochromeCloud::ConstPtr &cloud not the raw reference that you are passing in. This is a typedef of a smart pointer from boost:在那里我们看到它需要一个const MonochromeCloud::ConstPtr &cloud而不是您传入的原始引用。 这是来自 boost 的智能指针的 typedef:

typedef boost::shared_ptr<const PointCloud<PointT> > pcl::PointCloud< PointT >::ConstPtr

So when you create your cloud you need to wrap it in one of these smart pointers instead of making it a local variable.因此,当您创建云时,您需要将其包装在这些智能指针之一中,而不是使其成为局部变量。 Something like (untested):类似的东西(未经测试):

pcl::MonochromeCloud::ConstPtr cloud(new pcl::PointCloud<pcl::PointXYZ>());

Then, when you pass in the variable cloud, it will have the correct type and you won't get the error that you report.然后,当您传入变量 cloud 时,它将具有正确的类型,并且您不会收到您报告的错误。 You will also have to change your cloud.foo s to cloud->foo s.您还必须将您的cloud.foo s 更改为cloud.foo cloud->foo s。

Looking at the second example you give, they do this as well:看看你给出的第二个例子,他们也这样做:

pcl::PointCloud<pcl::PointXYZRGBA>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZRGBA>);

To give the answer straight away:直接给出答案:

pcl::PointCloud<pcl::PointXYZRGB>::Ptr ptrCloud(&cloud);

Then put in ptrCloud in the viewer, it's what it expects:然后在查看器中放入ptrCloud,这是它所期望的:

viewer.showCloud (ptrCloud);

If anybody else is just searching how to do this in ROS it can be simply done using:如果其他人只是在 ROS 中搜索如何执行此操作,则可以简单地使用:

#include <ros/ros.h>
#include <pcl_ros/point_cloud.h>
#include <pcl/point_types.h>
#include <pcl/visualization/cloud_viewer.h>
#include <sensor_msgs/PointCloud2.h>
#include <pcl_conversions/pcl_conversions.h>
#include <iostream>
#include <pcl/common/common_headers.h>
#include <pcl/features/normal_3d.h>
#include <pcl/io/pcd_io.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/console/parse.h>

typedef pcl::PointCloud<pcl::PointXYZ> PointCloud;

class cloudHandler
{
public:
    cloudHandler():viewer("Cloud Viewer")
    {
        pcl_sub = nh.subscribe("/camera/depth_registered/points", 10, &cloudHandler::cloudCB, this);
        viewer_timer = nh.createTimer(ros::Duration(0.1), &cloudHandler::timerCB,this);
    }

    void cloudCB(const sensor_msgs::PointCloud2 &input)
    {
        pcl::PointCloud<pcl::PointXYZRGB> cloud;
        pcl::fromROSMsg (input, cloud);
        viewer.showCloud(cloud.makeShared());
    }

    void timerCB(const ros::TimerEvent&)
    {
        if(viewer.wasStopped())
            {
                ros::shutdown();
            }
    }

protected:
    ros::NodeHandle nh;
    ros::Subscriber pcl_sub;
    pcl::visualization::CloudViewer viewer;
    ros::Timer viewer_timer;    
};

int main(int argc, char** argv)
{
    ros::init(argc, argv, "pcl_visualize");
    cloudHandler handler;
    ros::spin();
    return 0;
}

The includes could probably be cleaned more :)包括可能会被清理更多:)

The tutorial for CloudViewer pcl cloudviewer tutorial http://pointclouds.org/documentation/tutorials/cloud_viewer.php#cloud-viewer defines the point cloud as follows: CloudViewer pcl cloudviewer 教程http://pointclouds.org/documentation/tutorials/cloud_viewer.php#cloud-viewer教程定义点云如下:

pcl::PointCloud<pcl::PointXYZRGB>**::Ptr** cloud;

But you have written:但是你写了:

pcl::PointCloud<pcl::PointXYZ> cloud;

So try passing the cloud as &cloud instead of cloud, or declare it as a pointer.因此,尝试将云作为 &cloud 而不是 cloud 传递,或者将其声明为指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM