PCL—点云加载、可视化

用于单个显示、多个显示的头文件<visual_.h>

visual_.h

#pragma once
#include <iostream>
#include <thread>
#include <pcl/visualization/pcl_visualizer.h>
 
using namespace std::chrono_literals;
 
 
/*************************************************************************************************\
  如果一个程序中有多次窗口显示,可以用组合的方式实现,前边的都用这个,最后一个用showCloud_s0。
\*************************************************************************************************/
void showCloud_sn(pcl::PointCloud<pcl::PointXYZ>::ConstPtr cloud_xyz, std::string str)
{
    //std::string str;  //来区分不同的窗口
    pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer(str));
    viewer->setBackgroundColor(0, 0, 0);
    viewer->addPointCloud<pcl::PointXYZ>(cloud_xyz, str);
    viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, str);
    viewer->addCoordinateSystem(1.0, "global");
    printf("__show__%s\n", str.c_str());
    // no阻塞
}
 
/*
#include "visual_.h"
*/
 
 
/*************************************************************************************************\
应用范围:(1)程序中只显示1次,直接调用这个showCloud_s0。
          (2)程序中要多次显示,那么前边的显示用showCloud_sn,最后一次显示用showCloud_s0。(因为这个会一直阻塞运行)。
\*************************************************************************************************/
void showCloud_s0(pcl::PointCloud<pcl::PointXYZ>::ConstPtr cloud_xyz, std::string str)
{
    //std::string str = std::to_string(n);  //这个实际不用指定序号了。
 
    pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer(str));
    viewer->setBackgroundColor(0, 0, 0);
    viewer->addPointCloud<pcl::PointXYZ>(cloud_xyz, str);  viewer->updatePointCloud(cloud_xyz, window_name);
    viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, str);
    viewer->addCoordinateSystem(1.0, "global");  //默认//
    //
    //viewer->initCameraParameters();           //可不加//
    
 
    while (!viewer->wasStopped()) { viewer->spinOnce(10); }//阻塞
    //
}

读pcd点云文件、转换、显示

//读-转换-显示

#include <iostream>
#include <pcl/point_types.h>
#include <pcl/filters/passthrough.h>

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

#include "visual_.h"

int main(int argc, char** argv)
{

    pcl::PCLPointCloud2::Ptr cloud(new pcl::PCLPointCloud2());
    pcl::PCDReader reader;
    reader.read("D:/tool_s/PCL/pcl-pcl-1.11.1/test/office1.pcd", *cloud);    //读取点云到cloud中
    //
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_xyz(new pcl::PointCloud<pcl::PointXYZ>);
    pcl::fromPCLPointCloud2(*cloud, *cloud_xyz);
    showCloud_sn(cloud_xyz,"001");

    return 0;
}

读pcd点云文件直接显示

//读-显示

#include <iostream>
#include <pcl/point_types.h>
#include <pcl/filters/passthrough.h>

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

#include "visual_.h"

int main(int argc, char** argv)
{
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_xyz(new pcl::PointCloud<pcl::PointXYZ>);
    pcl::PCDReader reader;
    reader.read("D:/tool_s/PCL/pcl-pcl-1.11.1/test/five_people.pcd", *cloud_xyz);
    showCloud_sn(cloud_xyz,"002");
    return 0;
}

显示多个pcd点云文件

//多个显示

#include <iostream>
#include <pcl/point_types.h>
#include <pcl/filters/passthrough.h>

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

#include "visual_.h"

int main(int argc, char** argv)
{
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_xyz(new pcl::PointCloud<pcl::PointXYZ>);
    pcl::PCDReader reader;
    reader.read("D:/tool_s/PCL/pcl-pcl-1.11.1/test/office1.pcd", *cloud_xyz);
    showCloud_sn(cloud_xyz, "0");
    showCloud_s0(cloud_xyz, "1");

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值