PCL把两个点云合并成一个点云

我先把一个人脸分成两半,然后拼接配准之后如左图所示,合并之后如右图所示,保存输出,这里的合并不涉及点云融合,只是两个点云相加
点云模型下载链接在另一篇文章里PCL点云配准官方教程
PCL把两个点云合并成一个点云_第1张图片

#include 
#include 
#include   //可视化头文件

typedef pcl::PointXYZ PointT;
typedef pcl::PointCloud<PointT> PointCloudT;

int
main(int argc, char** argv)
{
	// Objects for storing the point clouds.
	pcl::PointCloud<pcl::PointXYZRGBA>::Ptr cloudA(new pcl::PointCloud<pcl::PointXYZRGBA>);
	pcl::PointCloud<pcl::PointXYZRGBA>::Ptr cloudB(new pcl::PointCloud<pcl::PointXYZRGBA>);
	pcl::PointCloud<pcl::PointXYZRGBA>::Ptr cloudC(new pcl::PointCloud<pcl::PointXYZRGBA>);



	// 读取两个点云文件
	if (pcl::io::loadPLYFile<pcl::PointXYZRGBA>("C:\\Users\\fhlhc\\Desktop\\left.ply", *cloudA) != 0)
	{
		return -1;
	}
	if (pcl::io::loadPLYFile<pcl::PointXYZRGBA>("C:\\Users\\fhlhc\\Desktop\\right.ply", *cloudB) != 0)
	{
		return -1;
	}

	// Create cloud "C", with the points of both "A" and "B".点云A、B合并生成C
	*cloudC = (*cloudA) + (*cloudB);
	// Now cloudC->points.size() equals cloudA->points.size() + cloudB->points.size().

	    // //可视化
    pcl::visualization::PCLVisualizer viewer("合并两个点云");
    // 创建两个独立垂直观察视点
    int v1(0);

    int v2(1);
    viewer.createViewPort(0.0, 0.0, 0.5, 1.0, v1);
    viewer.createViewPort(0.5, 0.0, 1.0, 1.0, v2);

	// 设置背景颜色
    viewer.setBackgroundColor(0.5, 0, 0.5, v1);
    viewer.setBackgroundColor(0,0.5,0.5, v2);
  
	//A点云绿色
	pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZRGBA> src_h(cloudA, 0, 255, 0);
	//B点云蓝色
	pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZRGBA> tgt_h(cloudB, 0, 0, 255);
	//C点云红色
	pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZRGBA> final_h(cloudC, 255, 0, 0);

	//viewer.setBackgroundColor(255, 255, 255);
	viewer.addPointCloud(cloudA, src_h, "source cloud",v1);
	viewer.addPointCloud(cloudB, tgt_h, "target cloud",v1);
	viewer.addPointCloud(cloudC, final_h, "result cloud",v2);

	//  设置相机的坐标和方向(在终端初始观察的位置)
    viewer.setCameraPosition(50, 50, 50, 0.589847, 0.921947, -0.256907, 0);

	pcl::io::savePCDFileASCII("C:\\Users\\fhlhc\\Desktop\\cloudC.pcd", *cloudC);  //保存点云C
	while (!viewer.wasStopped())
			{
				viewer.spinOnce(100);
				boost::this_thread::sleep(boost::posix_time::microseconds(100000));
			}
}

你可能感兴趣的:(PCL,点云,自动驾驶)