【C++】pcl中的简单点云可视化

【C++】pcl中的简单点云可视化

简单点云可视化

点云视窗类CloudViewer是简单显示点云的可视化工具,可以让用户用尽可能少的代码查看点云。点云视窗类不能应用于多线程应用程序中

  1. 类CloudViewer定义在pcl/visualization/cloud_viewer.h文件中:
/** \brief Construct a cloud viewer, with a window name.
  * \param window_name This is displayed at the top of the window
  */
CloudViewer (const std::string& window_name);
  • 带一个字符串类型的参数,是可视化窗口的名字
  1. 类CloudViewer的成员函数showCloud定义:
/** \brief Show a cloud, with an optional key for multiple clouds.
  * \param[in] cloud XYZ point cloud
  * \param[in] cloudname a key for the point cloud, use the same name if you would like to overwrite the existing cloud.
  */
void
showCloud (const MonochromeCloud::ConstPtr &cloud, const std::string& cloudname = "cloud");
  • 第一个参数:一个XYZ类型的点云
  • 第二个参数:设置一个名字,相同名字的点云将覆盖之前的点云
  1. 使用回调函数时,被调用函数按如下方式声明:
/** Visualization callable function, may be used for running things on the UI thread.
  */
using VizCallable = std::function<void (pcl::visualization::PCLVisualizer&)>;
  1. 类CloudViewer的成员函数runOnVisualizationThreadOnce定义:
/** \brief Run a callbable object on the UI thread. This will run once and be removed
  * @param x Use boost::ref(x) for a function object that you would like to not copy
  */
void
runOnVisualizationThreadOnce (VizCallable x);
  • 在主函数中只执行一次回调函数
  1. 类CloudViewer的成员函数runOnVisualizationThread定义:
/** \brief Run a callbable object on the UI thread. Will persist until removed
  * @param x Use boost::ref(x) for a function object that you would like to not copy
  * \param key The key for the callable -- use the same key to overwrite.
  */
void
runOnVisualizationThread (VizCallable x, const std::string& key = "callable");
  • 在主函数中持续执行回调函数
  1. 类CloudViewer的成员函数wasStopped定义:
/** \brief Check if the gui was quit, you should quit also
  * \param millis_to_wait This will request to "spin" for the number of milliseconds, before exiting.
  * \return true if the user signaled the gui to stop
  */
bool
wasStopped (int millis_to_wait = 1);
  • 如果可视化窗口关闭,返回True
  1. 类PCLVisualizer定义在pcl/visualization/pcl_visualizer.h文件中:
/** \brief PCL Visualizer constructor.
  * \param[in] name the window name (empty by default)
  * \param[in] create_interactor if true (default), create an interactor, false otherwise
  */
PCLVisualizer (const std::string &name = "", const bool create_interactor = true);
  1. 类PCLVisualizer的成员函数setBackgroundColor定义:
/** \brief Set the viewport's background color.
  * \param[in] r the red component of the RGB color
  * \param[in] g the green component of the RGB color
  * \param[in] b the blue component of the RGB color
  * \param[in] viewport the view port (default: all)
  */
void
setBackgroundColor (const double &r, const double &g, const double &b, int viewport = 0);
  • 通过RGB三个通道设置视窗的背景颜色,范围归一化到(0到1)
  1. 类PCLVisualizer的成员函数addSphere定义:
/** \brief Add a sphere shape from a point and a radius
  * \param[in] center the center of the sphere
  * \param[in] radius the radius of the sphere
  * \param[in] id the sphere id/name (default: "sphere")
  * \param[in] viewport (optional) the id of the new viewport (default: 0)
  */
template <typename PointT> bool
addSphere (const PointT &center, double radius, const std::string &id = "sphere",
           int viewport = 0);
  • 用点和半径添加一个球体
  • 第一个参数:球体的球心
  • 第二个参数:球体的半径
  • 第三个参数:球体的id/name
  • 第四个参数:视窗id,默认为0
  1. 类PCLVisualizer的成员函数removeShape定义:
/** \brief Removes an added shape from screen (line, polygon, etc.), based on a given ID
  * \note This methods also removes PolygonMesh objects and PointClouds, if they match the ID
  * \param[in] id the shape object id (i.e., given on \a addLine etc.)
  * \param[in] viewport view port from where the Point Cloud should be removed (default: all)
  */
bool
removeShape (const std::string &id = "cloud", int viewport = 0);
  • 在指定视窗(参数二)中移走指定目标(参数一)
  1. 类PCLVisualizer的成员函数addText定义:
/** \brief Add a text to screen
  * \param[in] text the text to add
  * \param[in] xpos the X position on screen where the text should be added
  * \param[in] ypos the Y position on screen where the text should be added
  * \param[in] id the text object id (default: equal to the "text" parameter)
  * \param[in] viewport the view port (default: all)
  */
bool
addText (const std::string &text,
         int xpos, int ypos,
         const std::string &id = "", int viewport = 0);
  • 在指定视窗(参数五)里添加文字(ID为参数四)

完整程序:

#include <pcl/visualization/cloud_viewer.h> //点云视窗类:CloudViewer头文件声明
#include <iostream>
#include <pcl/io/io.h>
#include <pcl/io/pcd_io.h>
    
int user_data;

void 
viewerOneOff (pcl::visualization::PCLVisualizer& viewer) // 设置回调函数viewerOneOff
{
    viewer.setBackgroundColor (0.0, 1.0, 0.0); // 设置背景颜色
    pcl::PointXYZ o; 
    o.x = 1.0; 
    o.y = 0;
    o.z = 0;
    viewer.addSphere (o, 0.25, "sphere", 0); // 添加一个球体
    std::cout << "i only run once" << std::endl;
    
}
    
void 
viewerPsycho (pcl::visualization::PCLVisualizer& viewer) // 设置回调函数viewerPsycho
{
    static unsigned count = 0;
    std::stringstream ss;
    ss << "Once per viewer loop: " << count++;
    viewer.removeShape ("text", 0); // 移走旧文字
    viewer.addText (ss.str(), 200, 300, "text", 0); // 添加新文字
    user_data++;
}
    
int 
main ()
{
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>); // 创建一个PointCloud<PointXYZ> boost共享指针,并进行实例化为cloud
    pcl::io::loadPCDFile ("my_point_cloud.pcd", *cloud); // 读取点云文件
    pcl::visualization::CloudViewer viewer("Cloud Viewer"); // 创建一个可视化窗口

    viewer.showCloud(cloud); // 在窗口中显示点云

    viewer.runOnVisualizationThreadOnce (viewerOneOff); // 调用一次viewerOnOff

    viewer.runOnVisualizationThread (viewerPsycho); // 持续调用viewerPsycho
    
    while (!viewer.wasStopped ())
    {
        user_data++;
    }
    return 0;
}

程序执行结果:
在这里插入图片描述

结语

如果您有修改意见或问题,欢迎留言或者通过邮箱和我联系。
手打很辛苦,如果我的文章对您有帮助,转载请注明出处。

发布了57 篇原创文章 · 获赞 19 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/Zhang_Chen_/article/details/101124875
今日推荐