Skip to content

Instantly share code, notes, and snippets.

@taogashi
Last active April 8, 2022 12:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taogashi/3df54bd061e67d526bbb09937814e82a to your computer and use it in GitHub Desktop.
Save taogashi/3df54bd061e67d526bbb09937814e82a to your computer and use it in GitHub Desktop.
PCL pointcloud static background subtraction

OctreePointCloud类型的setInputCloud方法只是赋值,不会做任何reset。

inline void setInputCloud (const PointCloudConstPtr &cloud_arg,
             const IndicesConstPtr &indices_arg = IndicesConstPtr ())
{
	input_ = cloud_arg;
	indices_ = indices_arg;
}

所以,方法addPointsFromInputCloud可以重复调用。
OctreePointCloudDensity用于计算空间网格的密度。 实际上只是对叶节点进行计数。它的container类型是OctreePointCloudDensityContainer。

/** \brief Read input data. Only an internal counter is increased.
  */
void addPointIndex (int)
{
	point_counter_++;
}

这个函数的调用路径是:OctreePointCloud::addPointsFromInputCloud->OctreePointCloud::addPointIdx->LeafContainerT::addPointIdx
因此,可以逐帧累加,静止的背景所在的网格点的密度会越来越高。对于新的点云,只要检查每个点对应的密度,就可以判断这个点是前景还是背景。

PointCloudColor::const_iterator cloud_it = cloud->begin();
PointCloudColor::const_iterator cloud_it_end = cloud->end();
for (; cloud_it != cloud_it_end; ++cloud_it) {
	pcl::PointXYZRGBA pt = *cloud_it;
	uint64_t density = oct.getVoxelDensityAtPoint(*cloud_it);
	if (density > 2) {
		pt.rgba = 0xFFFF0000;
	} else if (density > 1) {
		pt.rgba = 0xFF00FF00;
	} else {
		pt.rgba = 0xFF0000FF;
	}
	display_cloud->points.push_back(pt);
}

伪代码:

read cloud;
octree_density.add(new cloud);
update density threshold;
for point in new cloud:
  if octree_density.density_at(point) > threshold:
    remove point from cloud;
@WestbrookZero
Copy link

WestbrookZero commented Jan 20, 2021

试了分割效果不好,density 打印出来的值不是4万多就是1万多,我设置的octree分辨率是128。设大设小都不行。我的深度相机分辨率是320x288。

@T800GHB
Copy link

T800GHB commented Apr 2, 2022

使用八叉树对背景建好模型之后,这个八叉树的数据结构如何保存呢?
我是用了serializeTree和deserializeTree的方法,但是不成功。
想问一下,你这块是怎么操作的?

@taogashi
Copy link
Author

taogashi commented Apr 8, 2022

好久没用PCL了,不清楚八叉树是否可以直接保存,但点云肯定是可以的。所以可以尝试把八叉树里面的点云保存下来,下次再用点云构建八叉树。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment