PCL:Visualizer 可视化❤️多视口显示
Posted 没事就要敲代码
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PCL:Visualizer 可视化❤️多视口显示相关的知识,希望对你有一定的参考价值。
1 双视口可视化
1.1 代码实现
#include <iostream>
#include <pcl\\io\\pcd_io.h>
#include <pcl\\visualization\\pcl_visualizer.h>
using namespace std;
int main()
{
//------------------------------------ 加载点云 ---------------------------------
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZRGB>);
pcl::PCDReader reader;
if (reader.read("tree.pcd", *cloud) < 0)
{
PCL_ERROR("\\a->点云文件不存在!\\n");
system("pause");
return -1;
}
cout << "->加载了 " << cloud->points.size() << " 个数据点" << endl;
//==============================================================================
//----------------------------- Visualizer 可视化 ------------------------------
///必选
boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer(new pcl::visualization::PCLVisualizer("Visualizer_Viewer")); //创建视窗对象,定义标题栏名称“3D Viewer”
//-----视口1-----
int v1(0);
viewer->createViewPort(0.0, 0.0, 0.5, 1.0, v1); //设置第一个视口在X轴、Y轴的最小值、最大值,取值在0-1之间
viewer->setBackgroundColor(0, 0, 0, v1);
viewer->addText("RGB", 10, 10, "v1_text", v1);
viewer->addPointCloud<pcl::PointXYZRGB>(cloud, "cloud1", v1);
//-----视口2-----
int v2(0);
viewer->createViewPort(0.5, 0.0, 1.0, 1.0, v2);
viewer->setBackgroundColor(0.3, 0.3, 0.3, v2);
viewer->addText("green", 10, 10, "v2 text", v2);
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZRGB> single_color(cloud, 0, 255, 0);
viewer->addPointCloud<pcl::PointXYZRGB>(cloud, single_color, "cloud2", v2);
//-----设置相关属性(可选)-----
viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2, "cloud1", v1);
viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "cloud2", v2);
///必选
while (!viewer->wasStopped())
{
viewer->spinOnce(100);
boost::this_thread::sleep(boost::posix_time::microseconds(100000));
}
//==============================================================================
return 0;
}
1.2 结果展示
1.3 关键代码解释
- viewer->createViewPort()的五个参数依次为视口在X坐标的最小值、Y坐标的最小值、X坐标的最大值、Y坐标的最大值、视口ID
...
int v1(0);
viewer->createViewPort (0.0, 0.0, 0.5, 1.0, v1);
...
2 多视口可视化
在知道 viewer->createViewPort() 的五个参数的具体意义之后,那么显示多个视口就很容易了,只要正确设置视口范围即可。
可视化视窗坐标系如下图所示:
2.1 代码实现
#include <iostream>
#include <pcl\\io\\pcd_io.h>
#include <pcl\\visualization\\pcl_visualizer.h>
using namespace std;
int main()
{
//------------------------------------ 加载点云 ---------------------------------
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZRGB>);
pcl::PCDReader reader;
if (reader.read("tree.pcd", *cloud) < 0)
{
PCL_ERROR("\\a->点云文件不存在!\\n");
system("pause");
return -1;
}
cout << "->加载了 " << cloud->points.size() << " 个数据点" << endl;
//==============================================================================
//----------------------------- Visualizer 可视化 ------------------------------
///必选
boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer(new pcl::visualization::PCLVisualizer("Multiple_Viewports")); //创建视窗对象,定义标题栏名称“3D Viewer”
//-----视口1-----
int v1(0);
viewer->createViewPort(0.0, 0.0, 0.33, 0.67, v1);
viewer->setBackgroundColor(0, 0, 0, v1);
viewer->addText("RGB", 10, 10, "v1 text", v1);
viewer->addPointCloud<pcl::PointXYZRGB>(cloud, "sample_cloud1", v1);
//-----视口2-----
int v2(0);
viewer->createViewPort(0.67, 0.0, 1.0, 0.67, v2);
viewer->setBackgroundColor(0.3, 0.3, 0.3, v2);
viewer->addText("green", 10, 10, "v2 text", v2);
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZRGB> single_color2(cloud, 0, 255, 0);
viewer->addPointCloud<pcl::PointXYZRGB>(cloud, single_color2, "sample cloud2", v2);
//-----视口3-----
int v3(0);
viewer->createViewPort(0.33, 0.67, 1.0, 1.0, v3);
viewer->setBackgroundColor(0.3, 0.0, 0.0, v3);
viewer->addText("blue", 10, 10, "v3 text", v3);
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZRGB> single_color3(cloud, 0, 0, 255);
viewer->addPointCloud<pcl::PointXYZRGB>(cloud, single_color3, "sample cloud3", v3);
//-----视口4-----
int v4(0);
viewer->createViewPort(0.0, 0.33, 0.33, 1.0, v4);
viewer->setBackgroundColor(0.0, 0.3, 0.0, v4);
viewer->addText("red", 10, 10, "v4 text", v4);
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZRGB> single_color4(cloud, 255, 0, 0);
viewer->addPointCloud<pcl::PointXYZRGB>(cloud, single_color4, "sample cloud4", v4);
//-----视口5-----
int v5(0);
viewer->createViewPort(0.33, 0.33, 0.67, 0.67, v5);
viewer->setBackgroundColor(0.0, 0.0, 0.3, v5);
viewer->addText("yellow", 10, 10, "v5 text", v5);
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZRGB> single_color5(cloud, 255, 255, 0);
viewer->addPointCloud<pcl::PointXYZRGB>(cloud, single_color5, "sample cloud5", v5);
///必选
while (!viewer->wasStopped())
{
viewer->spinOnce(100);
boost::this_thread::sleep(boost::posix_time::microseconds(100000));
}
//==============================================================================
return 0;
}
2.2 结果展示
相关链接
以上是关于PCL:Visualizer 可视化❤️多视口显示的主要内容,如果未能解决你的问题,请参考以下文章
PCL:如何更新 pcl::visualizer 中的法线云?