1、从PCD文件中读取点云数据
#include
#include
#include
int main(int argc,char** argv)
{
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
if(pcl::io::loadPCDFile<pcl::PointXYZ>("test_pcd.pcd",*cloud)==-1)//*打开点云文件
{
PCL_ERROR("Couldn't read file test_pcd.pcd\n");
return(-1);
}
std::cout<<"Loaded "
<<cloud->width*cloud->height
<<" data points from test_pcd.pcd with the following fields: "
<<std::endl;
for(size_t i=0;i<cloud->points.size();++i)
std::cout<<" "<<cloud->points[i].x
<<" "<<cloud->points[i].y
<<" "<<cloud->points[i].z<<std::endl;
std::cout<<"cloud width: "
<<cloud->width<<std::endl;
std::cout<<"cloud height: "
<<cloud->height<<std::endl;
system("pause");
return(0);
}
#include
#include
#include
int main(int argc,char**argv)
{
pcl::PointCloud<pcl::PointXYZ> cloud;
// 创建点云
cloud.width=5;
cloud.height=1;
cloud.is_dense=false;
cloud.points.resize(cloud.width*cloud.height);
for(size_t i=0;i<cloud.points.size();++i)
{
cloud.points[i].x=1024*rand()/(RAND_MAX+1.0f);
cloud.points[i].y=1024*rand()/(RAND_MAX+1.0f);
cloud.points[i].z=1024*rand()/(RAND_MAX+1.0f);
if(i==1)
{
cloud.points[i].x=521;
}
}
pcl::io::savePCDFileASCII("test_pcd.pcd",cloud);
std::cerr<<"Saved "<<cloud.points.size()<<" data points to test_pcd.pcd."<<std::endl;
for(size_t i=0;i<cloud.points.size();++i)
std::cerr<<" "<<cloud.points[i].x<<" "<<cloud.points[i].y<<" "<<cloud.points[i].z<<std::endl;
system("pause");
return(0);
}
3、点云的字段拼接
要求两个点集中的点的数目必须一样
#include
#include
#include
int main(int argc,char** argv)
{
//if(argc!=2)
//{
// std::cerr<<"please specify command line arg '-f' or '-p'"<
// exit(0);
//}
argv[1] = "-f";
pcl::PointCloud<pcl::PointXYZ> cloud_a,cloud_b,cloud_c;
pcl::PointCloud<pcl::Normal> n_cloud_b;
pcl::PointCloud<pcl::PointNormal> p_n_cloud_c;
// 创建点云
cloud_a.width=5;//a的点个数为5
cloud_a.height=cloud_b.height=n_cloud_b.height=1;//设置为无序点云
cloud_a.points.resize(cloud_a.width*cloud_a.height);
if(strcmp(argv[1],"-p")==0)
{
cloud_b.width=3;
cloud_b.points.resize(cloud_b.width*cloud_b.height);
}
else
{
n_cloud_b.width=5;
n_cloud_b.points.resize(n_cloud_b.width*n_cloud_b.height);
}
for(size_t i=0;i<cloud_a.points.size();++i)
{
cloud_a.points[i].x=1024*rand()/(RAND_MAX+1.0f);
cloud_a.points[i].y=1024*rand()/(RAND_MAX+1.0f);
cloud_a.points[i].z=1024*rand()/(RAND_MAX+1.0f);
}
if(strcmp(argv[1],"-p")==0)
{
for(size_t i=0;i<cloud_b.points.size();++i)
{
cloud_b.points[i].x=1024*rand()/(RAND_MAX+1.0f);
cloud_b.points[i].y=1024*rand()/(RAND_MAX+1.0f);
cloud_b.points[i].z=1024*rand()/(RAND_MAX+1.0f);
}
}
else
{
for(size_t i=0;i<n_cloud_b.points.size();++i)
{
n_cloud_b.points[i].normal[0]=1024*rand()/(RAND_MAX+1.0f);
n_cloud_b.points[i].normal[1]=1024*rand()/(RAND_MAX+1.0f);
n_cloud_b.points[i].normal[2]=1024*rand()/(RAND_MAX+1.0f);
}
}
std::cerr<<"Cloud A: "<<std::endl;
for(size_t i=0;i<cloud_a.points.size();++i)
std::cerr<<" "<<cloud_a.points[i].x<<" "<<cloud_a.points[i].y<<" "<<cloud_a.points[i].z<<std::endl;
std::cerr<<"Cloud B: "<<std::endl;
if(strcmp(argv[1],"-p")==0)
{
for(size_t i=0;i<cloud_b.points.size();++i)
std::cerr<<" "<<cloud_b.points[i].x<<" "<<cloud_b.points[i].y<<" "<<cloud_b.points[i].z<<std::endl;
}
else
{
for(size_t i=0;i<n_cloud_b.points.size();++i)
std::cerr<<" "<<n_cloud_b.points[i].normal[0]<<" "<<n_cloud_b.points[i].normal[1]<<" "<<n_cloud_b.points[i].normal[2]<<std::endl;
}
//拷贝点云数据
if(strcmp(argv[1],"-p")==0)
{
cloud_c=cloud_a;
cloud_c+=cloud_b;
std::cerr<<"Cloud C: "<<std::endl;
for(size_t i=0;i<cloud_c.points.size();++i)
std::cerr<<" "<<cloud_c.points[i].x<<" "<<cloud_c.points[i].y<<" "<<cloud_c.points[i].z<<" "<<std::endl;
}
else
{
pcl::concatenateFields(cloud_a,n_cloud_b,p_n_cloud_c);
std::cerr<<"Cloud C: "<<std::endl;
for(size_t i=0;i<p_n_cloud_c.points.size();++i)
{
std::cerr<<" "<<
p_n_cloud_c.points[i].x<<" "<<p_n_cloud_c.points[i].y<<" "<<p_n_cloud_c.points[i].z<<" "<<
p_n_cloud_c.points[i].normal[0]<<" "<<p_n_cloud_c.points[i].normal[1]<<" "<<p_n_cloud_c.points[i].normal[2]<<std::endl;
}
}
system("pause");
return(0);
}
#include
#include
#include
int main(int argc,char** argv)
{
//if(argc!=2)
//{
// std::cerr<<"please specify command line arg '-f' or '-p'"<
// exit(0);
//}
argv[1] = "-p";
pcl::PointCloud<pcl::PointXYZ> cloud_a,cloud_b,cloud_c;
pcl::PointCloud<pcl::Normal> n_cloud_b;
pcl::PointCloud<pcl::PointNormal> p_n_cloud_c;
// 创建点云
cloud_a.width=5;//a的点个数为5
cloud_a.height=cloud_b.height=n_cloud_b.height=1;//设置为无序点云
cloud_a.points.resize(cloud_a.width*cloud_a.height);
if(strcmp(argv[1],"-p")==0)
{
cloud_b.width=3;
cloud_b.points.resize(cloud_b.width*cloud_b.height);
}
else
{
n_cloud_b.width=5;
n_cloud_b.points.resize(n_cloud_b.width*n_cloud_b.height);
}
for(size_t i=0;i<cloud_a.points.size();++i)
{
cloud_a.points[i].x=1024*rand()/(RAND_MAX+1.0f);
cloud_a.points[i].y=1024*rand()/(RAND_MAX+1.0f);
cloud_a.points[i].z=1024*rand()/(RAND_MAX+1.0f);
}
if(strcmp(argv[1],"-p")==0)
{
for(size_t i=0;i<cloud_b.points.size();++i)
{
cloud_b.points[i].x=1024*rand()/(RAND_MAX+1.0f);
cloud_b.points[i].y=1024*rand()/(RAND_MAX+1.0f);
cloud_b.points[i].z=1024*rand()/(RAND_MAX+1.0f);
}
}
else
{
for(size_t i=0;i<n_cloud_b.points.size();++i)
{
n_cloud_b.points[i].normal[0]=1024*rand()/(RAND_MAX+1.0f);
n_cloud_b.points[i].normal[1]=1024*rand()/(RAND_MAX+1.0f);
n_cloud_b.points[i].normal[2]=1024*rand()/(RAND_MAX+1.0f);
}
}
std::cerr<<"Cloud A: "<<std::endl;
for(size_t i=0;i<cloud_a.points.size();++i)
std::cerr<<" "<<cloud_a.points[i].x<<" "<<cloud_a.points[i].y<<" "<<cloud_a.points[i].z<<std::endl;
std::cerr<<"Cloud B: "<<std::endl;
if(strcmp(argv[1],"-p")==0)
{
for(size_t i=0;i<cloud_b.points.size();++i)
std::cerr<<" "<<cloud_b.points[i].x<<" "<<cloud_b.points[i].y<<" "<<cloud_b.points[i].z<<std::endl;
}
else
{
for(size_t i=0;i<n_cloud_b.points.size();++i)
std::cerr<<" "<<n_cloud_b.points[i].normal[0]<<" "<<n_cloud_b.points[i].normal[1]<<" "<<n_cloud_b.points[i].normal[2]<<std::endl;
}
//拷贝点云数据
if(strcmp(argv[1],"-p")==0)
{
cloud_c=cloud_a;
cloud_c+=cloud_b;
std::cerr<<"Cloud C: "<<std::endl;
for(size_t i=0;i<cloud_c.points.size();++i)
std::cerr<<" "<<cloud_c.points[i].x<<" "<<cloud_c.points[i].y<<" "<<cloud_c.points[i].z<<" "<<std::endl;
}
else
{
pcl::concatenateFields(cloud_a,n_cloud_b,p_n_cloud_c);
std::cerr<<"Cloud C: "<<std::endl;
for(size_t i=0;i<p_n_cloud_c.points.size();++i)
{
std::cerr<<" "<<
p_n_cloud_c.points[i].x<<" "<<p_n_cloud_c.points[i].y<<" "<<p_n_cloud_c.points[i].z<<" "<<
p_n_cloud_c.points[i].normal[0]<<" "<<p_n_cloud_c.points[i].normal[1]<<" "<<p_n_cloud_c.points[i].normal[2]<<std::endl;
}
}
system("pause");
return(0);
}
#include
#include
#include
#include
#include
using namespace pcl;
using namespace pcl::io;
int main (int argc, char** argv)
{
pcl::PLYReader reader;
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
reader.read<pcl::PointXYZ>("testply.ply", *cloud);
pcl::io::savePCDFile("testpcd.pcd", *cloud );
pcl::visualization::CloudViewer viewer ("Cloud Viewer"); //Cloud Viewer 是显示窗口栏的名称
viewer.showCloud(cloud);
while (!viewer.wasStopped ())
{
}
return 0;
}
5、pcd转ply文件并显示
#include //输入输出流头文件
#include //打开关闭pcd类定义头文件
#include //所有点类型定义头文件
#include //打开关闭ply类定义头文件
#include
#include
int main(int argc, char** argv)
{
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
if (pcl::io::loadPCDFile<pcl::PointXYZ>("bun.pcd", *cloud) == -1) //加载文件
{
PCL_ERROR("Couldn't read file \n");
system("PAUSE");
return (-1);
}
pcl::visualization::CloudViewer viewer ("Cloud Viewer"); //Cloud Viewer 是显示窗口栏的名称
viewer.showCloud(cloud);
//显示点云数量
std::cout << "point number: "
<< cloud->width * cloud->height
<< std::endl;
std::string filename("bun1.ply");
pcl::PLYWriter writer;
writer.write("bun1.ply", *cloud); //保存文件
system("PAUSE");
return (0);
}