• 两点云求差集和交集


    这里两点云的差集指从点云1中删除属于点云2的点得到的点集,交集指既属于点云1又属于点云2的点集。

    两点云求差集

    基于kd-tree搜索的方法较快速,当然也可以暴力搜索。思路如下:
    step1 在点云2建立kd-tree,设置容忍误差(搜索半径)
    step2 遍历点云1中的点,记录下到点云2中的点的距离小于搜索半径的点的索引
    step3 方法一:将点云1中不在索引中的点保存下来作为结果点云3
    方法二:直接对点云1操作,删除索引
    建议使用方法一,代码实现更简单,而且速度较快(erase操作会使迭代器位置改变,影响效率);除非要对原点云操作用方法二。

    #include 
    #include 
    #include 
    #include 
    #include  
    
    
    int main(int argc, char** argv)
    {
    	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud1(new pcl::PointCloud<pcl::PointXYZ>);
    	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud2(new pcl::PointCloud<pcl::PointXYZ>);
    
    	pcl::io::loadPCDFile("bunny1.pcd", *cloud1);
    	pcl::io::loadPCDFile("bunny2.pcd", *cloud2);
    
    	pcl::search::KdTree<pcl::PointXYZ>::Ptr kdtree(new pcl::search::KdTree<pcl::PointXYZ>);
    	kdtree->setInputCloud(cloud2);
    
    	std::vector<int> pointIdxRadiusSearch;
    	std::vector<float> pointRadiusSquaredDistance;
    	std::vector<int> indices;
    	float radius = 0.0001;
    
    	for (size_t i = 0; i < cloud1->size(); i++)
    	{
    		if (kdtree->radiusSearch(cloud1->points[i], radius, pointIdxRadiusSearch, pointRadiusSquaredDistance) > 0)
    		{
    			indices.push_back(i);
    		}
    	}
    
    	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud3(new pcl::PointCloud<pcl::PointXYZ>);
    	for (size_t i = 0; i < cloud1->size(); i++)
    	{
    		if (find(indices.begin(), indices.end(), i) == indices.end())
    			cloud3->push_back(cloud1->points[i]);
    	}
    
    	//for (size_t i = 0; i < indices.size(); i++)
    	//{
    	//	cloud1->erase(cloud1->begin() + indices[i] - i);
    	//}
    
    	std::cout << "cloud1 has " << cloud1->size() << " points" << std::endl;
    	std::cout << "cloud2 has " << cloud2->size() << " points" << std::endl;
    	std::cout << "cloud3 has " << cloud3->size() << " points" << std::endl;
    	pcl::io::savePCDFile("cloud3.pcd", *cloud3);
    
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51

    两点云求交集

    #include 
    #include 
    #include 
    #include 
    #include  
    
    
    int main(int argc, char** argv)
    {
    	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud1(new pcl::PointCloud<pcl::PointXYZ>);
    	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud2(new pcl::PointCloud<pcl::PointXYZ>);
    
    	pcl::io::loadPCDFile("bunny1.pcd", *cloud1);
    	pcl::io::loadPCDFile("bunny2.pcd", *cloud2);
    
    	pcl::search::KdTree<pcl::PointXYZ>::Ptr kdtree(new pcl::search::KdTree<pcl::PointXYZ>);
    	kdtree->setInputCloud(cloud2);
    
    	std::vector<int> pointIdxRadiusSearch;
    	std::vector<float> pointRadiusSquaredDistance;
    	std::vector<int> indices;
    	float radius = 0.0001;
    
    	for (size_t i = 0; i < cloud1->size(); i++)
    	{
    		if (kdtree->radiusSearch(cloud1->points[i], radius, pointIdxRadiusSearch, pointRadiusSquaredDistance) > 0)
    		{
    			indices.push_back(i);
    		}
    	}
    
    	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud3(new pcl::PointCloud<pcl::PointXYZ>);
    	for (size_t i = 0; i < cloud1->size(); i++)
    	{
    		if (find(indices.begin(), indices.end(), i) != indices.end())
    			cloud3->push_back(cloud1->points[i]);
    	}
    
    	std::cout << "cloud1 has " << cloud1->size() << " points" << std::endl;
    	std::cout << "cloud2 has " << cloud2->size() << " points" << std::endl;
    	std::cout << "cloud3 has " << cloud3->size() << " points" << std::endl;
    	pcl::io::savePCDFile("cloud3.pcd", *cloud3);
    
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    cloud1 (bunny.pcd裁掉耳朵)
    在这里插入图片描述
    cloud2 (bunny.pcd裁掉尾巴)
    在这里插入图片描述

    cloud1-cloud2
    在这里插入图片描述

    cloud1∩cloud2
    在这里插入图片描述

  • 相关阅读:
    基于关联规则的网络信息安全风险度量分析模型
    二叉搜索树+二叉进阶oj
    Python数据分析实战-使用装饰器为函数增加异常处理功能(附源码和实现效果)
    聊聊我的试用期总结(不知名公司算法工程师
    Linux系统命令——帮助命令、文件权限命令
    从微服务基本概念到核心组件-通过一个实例来讲解和分析
    Google Earth Engine ——Landsat 5 和Landsat1-4影像集合
    2310D导入c部分可用
    【Unity入门计划】用双血条方法控制伤害区域减血速度
    【T3】畅捷通T3采购管理模块反结账,提示:本年数据已经结转,不能取消结账。
  • 原文地址:https://blog.csdn.net/taifyang/article/details/127834570