• 两点云求差集和交集


    这里两点云的差集指从点云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
    在这里插入图片描述

  • 相关阅读:
    【原创】基于SSM的医院预约挂号系统(医院预约挂号系统毕设源代码)
    wsl2 执行exe文件提示 无法执行二进制文件:可执行文件格式错误
    Yii2安装遇到Loading composer repositories with package information
    电压放大器如何选择型号规格(电压放大器选型标准)
    【WALT】WALT入口 update_task_ravg() 代码详解
    await在python协程函数的使用
    论文阅读——GPT3
    【SQL server速成之路】触发器
    做抖店最主要核心是什么?运营技术重要吗?
    广州华资应届生Java面试2022-4-20
  • 原文地址:https://blog.csdn.net/taifyang/article/details/127834570