PCL 点云可视化-转自张智胜
#include <iostream>
#include <pcl/point_types.h>
#include <pcl/point_cloud.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/point_types.h>
#include <pcl/point_cloud.h>
#include <pcl/visualization/pcl_visualizer.h>
int main(int argc, char** argv)
{
//定义一个点云cloud
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
{
//定义一个点云cloud
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
cloud->width = 1000;
cloud->height = 1;
cloud->resize(cloud->width * cloud->height);
cloud->height = 1;
cloud->resize(cloud->width * cloud->height);
for (size_t i = 0; i < cloud->size(); ++i)
{
cloud->at(i).x = 1024 * rand() / (RAND_MAX + 1.0f);
cloud->at(i).y = 1024 * rand() / (RAND_MAX + 1.0f);
cloud->at(i).z = 1024 * rand() / (RAND_MAX + 1.0f);
}
//显示类
pcl::visualization::PCLVisualizer viewer("cloud viewer");
{
cloud->at(i).x = 1024 * rand() / (RAND_MAX + 1.0f);
cloud->at(i).y = 1024 * rand() / (RAND_MAX + 1.0f);
cloud->at(i).z = 1024 * rand() / (RAND_MAX + 1.0f);
}
//显示类
pcl::visualization::PCLVisualizer viewer("cloud viewer");
//设置窗口背景颜色,范围0-1
viewer.setBackgroundColor(0, 0, 0);
viewer.setBackgroundColor(0, 0, 0);
//添加坐标轴
viewer.addCoordinateSystem(1000);
//往窗口添加点云,第二个参数位点云ID,添加多个点云时必须写ID
viewer.addPointCloud(cloud, "cloude");
viewer.addCoordinateSystem(1000);
//往窗口添加点云,第二个参数位点云ID,添加多个点云时必须写ID
viewer.addPointCloud(cloud, "cloude");
//修改点云后可以使用下面的函数更新点云
//viewer.updatePointCloud(cloud, "cloud");
//viewer.updatePointCloud(cloud, "cloud");
//从窗口删除点云,可以使用下面函数
//viewer.removePointCloud("cloud");
//viewer.removePointCloud("cloud");
//添加点云后,通过点云的ID来设置显示大小
viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 5, "cloud");
viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 5, "cloud");
//重置相机,将点云显示到窗口
viewer.resetCamera();
viewer.resetCamera();
while (!viewer.wasStopped())//要想让自己所创窗口一直显示
{
viewer.spinOnce();
}
return 0;
}
{
viewer.spinOnce();
}
return 0;
}
华丽丽的分割线下方是一个点云的颜色显示(附图)
---------------------------------------------------------------------------------------------------------------------------------
#include <iostream>
#include <pcl/point_types.h>
#include <pcl/point_cloud.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/point_types.h>
#include <pcl/point_cloud.h>
#include <pcl/visualization/pcl_visualizer.h>
int main(int argc, char** argv)
{
//定义一个点云cloud
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
{
//定义一个点云cloud
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
cloud->width = 1000;
cloud->height = 1;
cloud->resize(cloud->width * cloud->height);
cloud->height = 1;
cloud->resize(cloud->width * cloud->height);
for (size_t i = 0; i < cloud->size(); ++i)
{
cloud->at(i).x = 1024 * rand() / (RAND_MAX + 1.0f);
cloud->at(i).y = 1024 * rand() / (RAND_MAX + 1.0f);
cloud->at(i).z = 1024 * rand() / (RAND_MAX + 1.0f);
}
{
cloud->at(i).x = 1024 * rand() / (RAND_MAX + 1.0f);
cloud->at(i).y = 1024 * rand() / (RAND_MAX + 1.0f);
cloud->at(i).z = 1024 * rand() / (RAND_MAX + 1.0f);
}
//显示类
pcl::visualization::PCLVisualizer viewer("Cloud Viewer");
pcl::visualization::PCLVisualizer viewer("Cloud Viewer");
//设置窗口背景颜色,范围为0-1
viewer.setBackgroundColor(0, 0, 0);
viewer.setBackgroundColor(0, 0, 0);
//添加坐标轴
viewer.addCoordinateSystem(1000);
viewer.addCoordinateSystem(1000);
//设置不同的i来切换不同的颜色显示
int i = 2;
int i = 2;
if (i == 0)
{
//设置颜色为绿色
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> singleColor(cloud, 0, 255, 0);
{
//设置颜色为绿色
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> singleColor(cloud, 0, 255, 0);
//往窗口添加点云并设置颜色
viewer.addPointCloud(cloud, singleColor, "cloud");
}
else if (i == 1)
{
//随机设置一个颜色
pcl::visualization::PointCloudColorHandlerRandom<pcl::PointXYZ> randomColor(cloud);
viewer.addPointCloud(cloud, singleColor, "cloud");
}
else if (i == 1)
{
//随机设置一个颜色
pcl::visualization::PointCloudColorHandlerRandom<pcl::PointXYZ> randomColor(cloud);
//往窗口添加点云并设置颜色
viewer.addPointCloud(cloud, randomColor, "cloud");
}
else if (i == 2)
{
//根据点云里某个字段大小设置颜色
pcl::visualization::PointCloudColorHandlerGenericField<pcl::PointXYZ> fildColor(cloud, "x");
viewer.addPointCloud(cloud, randomColor, "cloud");
}
else if (i == 2)
{
//根据点云里某个字段大小设置颜色
pcl::visualization::PointCloudColorHandlerGenericField<pcl::PointXYZ> fildColor(cloud, "x");
//往窗口添加点云并设置颜色
viewer.addPointCloud(cloud, fildColor, "cloud");
}
viewer.addPointCloud(cloud, fildColor, "cloud");
}
//添加点云后,通过点云ID来设置显示大小
viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 5, "cloud");
viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 5, "cloud");
//重置相机,将点云显示到窗口
viewer.resetCamera();
viewer.resetCamera();
while (!viewer.wasStopped())
{
viewer.spinOnce();
}
{
viewer.spinOnce();
}
return (0);
}
}
--------------------------------------------------------------------------------------------------------------------------------
结果图
//添加发现的代码
#include <iostream>
#include <pcl/point_types.h>
#include <pcl/point_cloud.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/point_types.h>
#include <pcl/point_cloud.h>
#include <pcl/visualization/pcl_visualizer.h>
int main(int argc, char** argv)
{
//定义一个点云cloud
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
{
//定义一个点云cloud
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
cloud->width = 1000;
cloud->height = 1;
cloud->resize(cloud->width * cloud->height);
cloud->height = 1;
cloud->resize(cloud->width * cloud->height);
for (size_t i = 0; i < cloud->size(); ++i)
{
cloud->at(i).x = 1024 * rand() / (RAND_MAX + 1.0f);
cloud->at(i).y = 1024 * rand() / (RAND_MAX + 1.0f);
cloud->at(i).z = 1024 * rand() / (RAND_MAX + 1.0f);
}
{
cloud->at(i).x = 1024 * rand() / (RAND_MAX + 1.0f);
cloud->at(i).y = 1024 * rand() / (RAND_MAX + 1.0f);
cloud->at(i).z = 1024 * rand() / (RAND_MAX + 1.0f);
}
//显示类
pcl::visualization::PCLVisualizer viewer("Cloud Viewer");
pcl::visualization::PCLVisualizer viewer("Cloud Viewer");
//设置窗口背景颜色,范围为0-1
viewer.setBackgroundColor(0, 0, 0);
viewer.setBackgroundColor(0, 0, 0);
//添加坐标轴
viewer.addCoordinateSystem(1000);
viewer.addCoordinateSystem(1000);
//设置不同的i来切换不同的颜色显示
int i = 2;
int i = 2;
if (i == 0)
{
//设置颜色为绿色
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> singleColor(cloud, 0, 255, 0);
{
//设置颜色为绿色
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> singleColor(cloud, 0, 255, 0);
//往窗口添加点云并设置颜色
viewer.addPointCloud(cloud, singleColor, "cloud");
}
else if (i == 1)
{
//随机设置一个颜色
pcl::visualization::PointCloudColorHandlerRandom<pcl::PointXYZ> randomColor(cloud);
viewer.addPointCloud(cloud, singleColor, "cloud");
}
else if (i == 1)
{
//随机设置一个颜色
pcl::visualization::PointCloudColorHandlerRandom<pcl::PointXYZ> randomColor(cloud);
//往窗口添加点云并设置颜色
viewer.addPointCloud(cloud, randomColor, "cloud");
}
else if (i == 2)
{
//根据点云里某个字段大小设置颜色
pcl::visualization::PointCloudColorHandlerGenericField<pcl::PointXYZ> fildColor(cloud, "x");
viewer.addPointCloud(cloud, randomColor, "cloud");
}
else if (i == 2)
{
//根据点云里某个字段大小设置颜色
pcl::visualization::PointCloudColorHandlerGenericField<pcl::PointXYZ> fildColor(cloud, "x");
//往窗口添加点云并设置颜色
viewer.addPointCloud(cloud, fildColor, "cloud");
}
//定义一个点云法线,这里赋值为一个固定值
pcl::PointCloud<pcl::Normal>::Ptr normals(new pcl::PointCloud<pcl::Normal>);
viewer.addPointCloud(cloud, fildColor, "cloud");
}
//定义一个点云法线,这里赋值为一个固定值
pcl::PointCloud<pcl::Normal>::Ptr normals(new pcl::PointCloud<pcl::Normal>);
normals->width = 1000;
normals->height = 1;
normals->resize(normals->width * normals->height);
normals->height = 1;
normals->resize(normals->width * normals->height);
for (size_t i = 0; i < normals->size(); ++i)
{
normals->at(i).normal_x = 1;
normals->at(i).normal_y = 0;
normals->at(i).normal_z = 0;
}
//往窗口添加法线,每10个点显示1个法线长度为50
viewer.addPointCloudNormals<pcl::PointXYZ, pcl::Normal>(cloud, normals, 10, 50, "normals");
{
normals->at(i).normal_x = 1;
normals->at(i).normal_y = 0;
normals->at(i).normal_z = 0;
}
//往窗口添加法线,每10个点显示1个法线长度为50
viewer.addPointCloudNormals<pcl::PointXYZ, pcl::Normal>(cloud, normals, 10, 50, "normals");
//添加点云后,通过点云ID来设置显示大小
viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 5, "cloud");
//重置相机,将点云显示到窗口
viewer.resetCamera();
viewer.resetCamera();
while (!viewer.wasStopped())
{
viewer.spinOnce();
}
{
viewer.spinOnce();
}
return (0);
}
}
----------------------------------------------------------------------------------------------------------------------------------