在PCL中使用VoxelGrid滤波器对点云进行滤波

#include 
#include 
#include 
#include 
#include 
#include 


int user_data;
void
viewerOneOff(pcl::visualization::PCLVisualizer& viewer)
{
	viewer.setBackgroundColor(1.0, 0.5, 1.0);
	pcl::PointXYZ o;
	o.x = 1.0;
	o.y = 0;
	o.z = 0;
	viewer.addSphere(o, 0.25, "sphere", 0);
	std::cout << "i only run once" << std::endl;

}

void
viewerPsycho(pcl::visualization::PCLVisualizer& viewer)
{
	static unsigned count = 0;
	std::stringstream ss;
	ss << "Once per viewer loop: " << count++;
	viewer.removeShape("text", 0);
	viewer.addText(ss.str(), 200, 300, "text", 0);
	//FIXME: possible race condition here:
	user_data++;
}
int
main(int argc, char** argv)
{
	
	pcl::PointCloud::Ptr cloud(new pcl::PointCloud);
	pcl::PointCloud::Ptr cloud_filtered(new pcl::PointCloud);
	// 填入点云数据
	pcl::io::loadPCDFile("table_scene_lms400.pcd", *cloud);
	
	// 创建滤波器对象
	pcl::VoxelGrid sor;
	sor.setInputCloud(cloud);
	sor.setLeafSize(0.01f, 0.01f, 0.01f);
	sor.filter(*cloud_filtered);
	std::cerr << "PointCloud after filtering: " << cloud_filtered->width * cloud_filtered->height
		<< " data points (" << pcl::getFieldsList(*cloud_filtered) << ").";

	
	
	
	pcl::visualization::CloudViewer viewer("Cloud Viewer");
	//showCloud函数是同步的,在此处等待直到渲染显示为止
	viewer.showCloud(cloud_filtered);
	//该注册函数在可视化时只调用一次
	viewer.runOnVisualizationThreadOnce(viewerOneOff);
	//该注册函数在渲染输出时每次都调用
	viewer.runOnVisualizationThread(viewerPsycho);
	while (!viewer.wasStopped())
	{
		//在此处可以添加其他处理
		user_data++;
	}

	
	
	return (0);
}

你可能感兴趣的:(PCL学习)