PCL体素化降采样 实现立方体体素可视化

PCL体素化降采样 实现立方体体素可视化

  • PCL库函数自带的VoxelGrid类能够实现对点云进行降采样。基本原理是对点云进行网格划分,落在每个小立方块区域中的点的重心就代表网格中的所有点。因此通过控制网格边长就能够控制降采样的点数。缺点在于不能指定降采样点数大小,只能通过调参逼近。
  • 具体的体素化代码实现不做介绍,可以参考以下博客:
    体素栅格滤波(下采样)
  • 以下代码功能是可视化立方体体素
  • 目前没有实现改变体素颜色,后期视使用情况增添(版本二)
    在这里插入图片描述
#include <thread>
#include <pcl/common/common_headers.h>
#include <pcl/features/normal_3d.h>
#include <pcl/visualization/pcl_visualizer.h>

using namespace std::chrono_literals;
using namespace std;
int main(int argc, char** argv) {
	pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer("HelloMyFirstVisualPCL"));
	viewer->setBackgroundColor(1, 1, 1);

	FILE*fp = NULL; fp = fopen("filename.txt", "r");	//2DpointDatas.txt
	if (!fp)
	{
		printf("打开文件失败!!\n");
		int m;
		cin >> m;
		exit(0);
	}
	float x = 0, y = 0, z = 0;
	int i = 0;
	
	while (!feof(fp))
	{
		float voxel = 0.82;
		i++;
		fscanf(fp, "%f %f %f", &x, &y, &z);
		Eigen::Vector3f center(floor(x / voxel)*voxel + voxel/2, floor(y / voxel)*voxel + voxel/2, floor(z / voxel)*voxel + voxel/2);
		Eigen::Quaternionf rotation(1, 0, 0, 0);
		string cube = "cube" + to_string(i);
		viewer->addCube(center, rotation, voxel, voxel, voxel, cube);
	}
	while (!viewer->wasStopped())
	{
		viewer->spinOnce(100);
		std::this_thread::sleep_for(100ms);
	}
	return 0;
}

版本二

  • 加入点线框和改变点和线框的颜色
  • 可以将两个读入改为一个此处就不修改了在这里插入图片描述
#include <thread>
#include <pcl/common/common_headers.h>
#include <pcl/features/normal_3d.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/io/pcd_io.h>  //文件输入输出

using namespace std::chrono_literals;
using namespace std;
int main(int argc, char** argv) {
	pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer("HelloMyFirstVisualPCL"));
	viewer->setBackgroundColor(1, 1, 1);

	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
	if (pcl::io::loadPCDFile<pcl::PointXYZ>("el.pcd", *cloud) == -1)
	{
		PCL_ERROR("Cloudn't read file!");
		return -1;
	}
	cout << "there are " << cloud->points.size() << " points before filtering." << endl;

	FILE*fp = NULL; fp = fopen("el.txt", "r");	//2DpointDatas.txt
	if (!fp)
	{
		printf("打开文件失败!!\n");
		int m;
		cin >> m;
		exit(0);
	}
	float x = 0, y = 0, z = 0;
	int i = 0;

	pcl::visualization::PointCloudColorHandlerGenericField<pcl::PointXYZ> fildColor(cloud, "z"); // 按照z字段进行渲染
	//viewer->addPointCloud(cloud);
	viewer->addPointCloud<pcl::PointXYZ>(cloud, fildColor, "sample cloud");
	while (!feof(fp))
	{
		float voxel = 1.85;
		i++;
		fscanf(fp, "%f %f %f", &x, &y, &z);
		string cube = "cube" + to_string(i);
		float x_min = floor(x / voxel)*voxel;
		float x_max = floor(x / voxel)*voxel + voxel;
		float y_min = floor(y / voxel)*voxel;
		float y_max = floor(y / voxel)*voxel + voxel;
		float z_min = floor(z / voxel)*voxel;
		float z_max = floor(z / voxel)*voxel + voxel;
		double r = 0.5, g=0.5, b =0.5;
		viewer->addCube(x_min, x_max, y_min, y_max, z_min, z_max, r, g, b, cube);
		viewer->setShapeRenderingProperties(pcl::visualization::PCL_VISUALIZER_REPRESENTATION, pcl::visualization::PCL_VISUALIZER_REPRESENTATION_WIREFRAME, cube);
	}
	while (!viewer->wasStopped())
	{
		viewer->spinOnce(100);
		std::this_thread::sleep_for(100ms);
	}
	return 0;
}

发布了27 篇原创文章 · 获赞 4 · 访问量 4524

猜你喜欢

转载自blog.csdn.net/SGL_LGS/article/details/103658312