使用VoxelGrid滤波器对点云降采样,并使用PCLVisulizer进行显示

#include 
#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::PCLVisualizer viewer("PCLVisualizer");
	viewer.initCameraParameters();

	int v1(0);
	viewer.createViewPort(0.0, 0.0, 0.5, 1.0, v1);
	viewer.setBackgroundColor(1.0, 0.5, 1.0,v1);
	viewer.addText("Cloud before voxelgrid filtering", 10, 10,"v1 test", v1);
	pcl::visualization::PointCloudColorHandlerGenericField cloud_color(cloud, "intensity");
	viewer.addPointCloud(cloud, cloud_color, "cloud", v1);

	int v2(0);
	viewer.createViewPort(0.5, 0.0, 1.0, 1.0, v2);
	viewer.setBackgroundColor(1.0, 0.5, 1.0,v2);
	viewer.addText("Cloud after voxelgrid filtering", 10, 10, "v2 test", v2);
	pcl::visualization::PointCloudColorHandlerGenericField cloud_afterfilter_color(cloud_filtered, "intensity");
	viewer.addPointCloud(cloud_filtered, cloud_afterfilter_color, "cloud_filtered", v2);

	viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "cloud");
	viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "cloud_filtered");

	
	while (!viewer.wasStopped())
	{
		//在此处可以添加其他处理
		user_data++;
		viewer.spinOnce(100);
		boost::this_thread::sleep(boost::posix_time::microseconds(100000));
	}



	return (0);
}

使用VoxelGrid滤波器对点云降采样,并使用PCLVisulizer进行显示_第1张图片

使用VoxelGrid滤波器对点云降采样,并使用PCLVisulizer进行显示_第2张图片


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