这里两点云的差集指从点云1中删除属于点云2的点得到的点集,并集指既属于点云1又属于点云2的点集。
两点云求差集
基于kd-tree搜索的方法较快速,当然也可以暴力搜索。思路如下:
step1 在点云2建立kd-tree,设置容忍误差(搜索半径)
step2 遍历点云1中的点,记录下到点云2中的点的距离小于搜索半径的点的索引
step3 方法一:将点云1中不在索引中的点保存下来作为结果点云3
方法二:直接对点云1操作,删除索引点
建议使用方法一,代码实现更简单,而且速度较快(erase操作会使迭代器位置改变,影响效率);除非要对原点云操作用方法二。
#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/common/io.h>
#include <pcl/search/kdtree.h>
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;
}
两点云求并集
#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/common/io.h>
#include <pcl/search/kdtree.h>
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;
}
cloud1 (bunny.pcd裁掉耳朵)
cloud2 (bunny.pcd裁掉尾巴)
cloud1-cloud2
cloud1∩cloud2