话不多说直接上代码
#include <pcl/point_cloud.h>
#include <pcl/octree/octree_pointcloud_changedetector.h>
#include <iostream>
#include <vector>
#include <ctime>
pcl::PointCloud<pcl::PointXYZI>::Ptr dynamic_obj_detec(pcl::PointCloud<pcl::PointXYZI>::Ptr cloud_before, pcl::PointCloud<pcl::PointXYZI>::Ptr cloud_now){
std::cout<<"has data "<<cloud_now->size()<<" "<<cloud_before->size()<<std::endl;
pcl::PointCloud<pcl::PointXYZI>::Ptr cloud_fliter(new pcl::PointCloud<pcl::PointXYZI>());
srand((unsigned int) time (NULL));
// Octree resolution - side length of octree voxels
float resolution = 0.2f;//20cm范围内如果前后两帧占用概率变换则认为是动态障碍物
// Instantiate octree-based point cloud change detection class
pcl::octree::OctreePointCloudChangeDetector<pcl::PointXYZI> octree (resolution);
// Add points from cloudA to octree
octree.setInputCloud (cloud_before);
octree.addPointsFromInputCloud ();
// Switch octree buffers: This resets octree but keeps previous tree structure in memory.
//交换八叉树缓存,但是cloudA对应的八叉树结构仍在内存中
octree.switchBuffers ();
// Add points from cloudB to octree
octree.setInputCloud (cloud_now);
octree.addPointsFromInputCloud ();
std::vector<int> newPointIdxVector;
// Get vector of point indices from octree voxels which did not exist in previous buffer
octree.getPointIndicesFromNewVoxels (newPointIdxVector);
// Output points
std::cout << "Output from getPointIndicesFromNewVoxels:" << std::endl;
std::cout<<newPointIdxVector.size()<<std::endl;
for (std::size_t i = 0; i < newPointIdxVector.size (); ++i){
cloud_now->points[newPointIdxVector[i]] = NULL;
}
std::cout << "Output from getPointIndicesFromNewVoxels:" << std::endl;
return cloud_now;
}
第一张为滤除后的效果,第二张为未滤除的