- 1.如何获取pcd文件点云里点的格式,比如是pcl::PointXYZ还是pcl::PointXYZRGB等类型?
- 2.如何实现类似pcl::PointCloud::Ptr和pcl::PointCloud的两个类相互转换?
- 3.如何加快ASCII格式存储,也就是记事本打开可以看到坐标数据的pcd文件读取速度?
- 4.如何给pcl::PointXYZ类型的点云在显示时指定颜色?
- 5.如何查找点云的x,y,z的极值?
- 6.如何判断点云中的点为无效点,即坐标值为nan,以及如何将点设置为无效点?
- 7.如何将无效点从点云中移除?
- 8.如果知道需要保存点的序号,如何从原点云中拷贝点到新点云?
- 9.如何从点云里删除和添加点?
- 10.PointCloud和PCLPointCloud2类型如何相互转换?
https://blog.csdn.net/zhazhiqiang/article/details/52495872
针对大家在利用PCL开源进行开发过程中的常见问题,进行解答。本文会长期更新。
1.如何获取pcd文件点云里点的格式,比如是pcl::PointXYZ还是pcl::PointXYZRGB等类型?
| 1 2 3 4 5 6 7 8 9 10 11 12 |
#include <pcl/io/pcd_io.h>#include <pcl/point_types.h>#include <pcl/point_cloud.h>#include <pcl/PCLPointCloud2.h>pcl::PCLPointCloud2 cloud;pcl::PCDReader reader;reader.readHeader( "C:\fandisk.pcd" , cloud);for ( int i = 0; i < cloud.fields.size(); i++){` std::cout << cloud.fields[i].name;<br />}` |
|---|---|
2.如何实现类似pcl::PointCloud::Ptr和pcl::PointCloud的两个类相互转换?
| 1 2 3 4 5 6 7 8 |
#include <pcl/io/pcd_io.h>#include <pcl/point_types.h>#include <pcl/point_cloud.h>pcl::PointCloud<pcl::PointXYZ>::Ptr cloudPointer( new pcl::PointCloud<pcl::PointXYZ>);pcl::PointCloud<pcl::PointXYZ> cloud;cloud = *cloudPointer;cloudPointer = cloud.makeShared(); |
|---|---|
3.如何加快ASCII格式存储,也就是记事本打开可以看到坐标数据的pcd文件读取速度?
建议将pcd文件换成以Binary格式存储。
| 1 2 3 4 5 6 7 8 |
#include <pcl/io/pcd_io.h>#include <pcl/point_types.h>#include <pcl/point_cloud.h>#include <pcl/PCLPointCloud2.h>pcl::PointCloud<pcl::PointXYZ>::Ptr cloud( new pcl::PointCloud<pcl::PointXYZ>);pcl::io::loadPCDFile<pcl::PointXYZ>( "C:\office3-after1.pcd" , *cloud);pcl::io::savePCDFileBinary( "C:\office3-after21111.pcd" , *cloud); |
|---|---|
4.如何给pcl::PointXYZ类型的点云在显示时指定颜色?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <pcl/io/pcd_io.h>#include <pcl/point_types.h>#include <pcl/point_cloud.h>#include <pcl/visualization/pcl_visualizer.h>pcl::PointCloud<pcl::PointXYZ>::Ptr cloud( new pcl::PointCloud<pcl::PointXYZ>);pcl::io::loadPCDFile<pcl::PointXYZ>( "C:\office3-after21111.pcd" , *cloud);pcl::visualization::PCLVisualizer viewer( "pointcloud viewer" );pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> sig(cloud, 0, 234, 0);viewer.addPointCloud(cloud, sig, "cloud" );while (!viewer.wasStopped()){` viewer.spinOnce();<br />}` |
|---|---|
5.如何查找点云的x,y,z的极值?
| 1 2 3 4 5 6 7 8 |
#include <pcl/io/pcd_io.h>#include <pcl/point_types.h>#include <pcl/common/common.h>pcl::PointCloud<pcl::PointXYZ>::Ptr cloud;cloud = pcl::PointCloud<pcl::PointXYZ>::Ptr ( new pcl::PointCloud<pcl::PointXYZ>);pcl::io::loadPCDFile<pcl::PointXYZ> ( "your_pcd_file.pcd" , *cloud);pcl::PointXYZ minPt, maxPt;pcl::getMinMax3D (*cloud, minPt, maxPt); |
|---|---|
6.如何判断点云中的点为无效点,即坐标值为nan,以及如何将点设置为无效点?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <pcl/point_types.h>pcl::PointXYZ p_valid;p_valid.x = 0;p_valid.y = 0;p_valid.z = 0;std::cout << "Is p_valid valid? " << pcl::isFinite(p_valid) << std::endl;pcl::PointXYZ p_invalid;p_invalid.x = std::numeric_limits< float >::quiet_NaN();p_invalid.y = 0;p_invalid.z = 0;std::cout << "Is p_invalid valid? " << pcl::isFinite(p_invalid) << std::endl; |
|---|---|
7.如何将无效点从点云中移除?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include <pcl/point_cloud.h>#include <pcl/point_types.h>#include <pcl/filters/filter.h>typedef pcl::PointCloud<pcl::PointXYZ> CloudType;CloudType::Ptr cloud( new CloudType);cloud->is_dense = false ;CloudType::Ptr output_cloud( new CloudType);CloudType::PointType p_nan;p_nan.x = std::numeric_limits< float >::quiet_NaN();p_nan.y = std::numeric_limits< float >::quiet_NaN();p_nan.z = std::numeric_limits< float >::quiet_NaN();cloud->push_back(p_nan);CloudType::PointType p_valid;p_valid.x = 1.0f;cloud->push_back(p_valid);std::cout << "size: " << cloud->points.size() << std::endl;std::vector< int > indices;pcl::removeNaNFromPointCloud(*cloud, *output_cloud, indices);std::cout << "size: " << output_cloud->points.size() << std::endl; |
|---|---|
8.如果知道需要保存点的序号,如何从原点云中拷贝点到新点云?
| 1 2 3 4 5 6 7 8 9 10 |
#include <pcl/io/pcd_io.h>#include <pcl/common/impl/io.hpp>#include <pcl/point_types.h>#include <pcl/point_cloud.h>pcl::PointCloud<pcl::PointXYZ>::Ptr cloud( new pcl::PointCloud<pcl::PointXYZ>);pcl::io::loadPCDFile<pcl::PointXYZ>( "C:\office3-after21111.pcd" , *cloud);pcl::PointCloud<pcl::PointXYZ>::Ptr cloudOut( new pcl::PointCloud<pcl::PointXYZ>);std::vector< int > indexs = { 1, 2, 5 };pcl::copyPointCloud(*cloud, indexs, *cloudOut); |
|---|---|
9.如何从点云里删除和添加点?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include "stdafx.h"#include <pcl/io/pcd_io.h>#include <pcl/common/impl/io.hpp>#include <pcl/point_types.h>#include <pcl/point_cloud.h>pcl::PointCloud<pcl::PointXYZ>::Ptr cloud( new pcl::PointCloud<pcl::PointXYZ>);pcl::io::loadPCDFile<pcl::PointXYZ>( "C:\office3-after21111.pcd" , *cloud);pcl::PointCloud<pcl::PointXYZ>::iterator index = cloud->begin();cloud->erase(index); //删除第一个index = cloud->begin() + 5;cloud->erase(cloud->begin()); //删除第5个pcl::PointXYZ point = { 1, 1, 1 };//在索引号为5的位置1上插入一点,原来的点后移一位cloud->insert(cloud->begin() + 5, point);cloud->push_back(point); //从点云最后面插入一点std::cout << cloud->points[5].x; //输出1 |
|---|---|
如果删除的点太多建议用上面的方法拷贝到新点云,再赋值给原点云,如果要添加很多点,建议先resize,然后用循环向点云里的添加。
10.PointCloud和PCLPointCloud2类型如何相互转换?
| 1 2 3 4 5 6 7 8 |
#include <pcl/io/pcd_io.h>#include <pcl/point_types.h>pcl::PointCloud<pcl::PointXYZ>::Ptr cloud( new pcl::PointCloud<pcl::PointXYZ>);pcl::PCLPointCloud2 cloud2;pcl::io::loadPCDFile( "C:\office3-after21111.pcd" , cloud2);pcl::fromPCLPointCloud2(cloud2, *cloud);pcl::toPCLPointCloud2(*cloud, cloud2); |
|---|---|
