用于单个显示、多个显示的头文件<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;
}