PCL:使用VoxelGrid filter对点云进行下采样
程序员文章站
2023-12-23 19:02:09
...
使用VoxelGrid filter对点云进行下采样,VoxelGrid可以堪称是个小的3D小方盒, 所有存在的点将以它们的质心近似(如,下采样)。这种方法比使用体素的中心近似慢,但它更准确地表示底层表面。
#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/filters/voxel_grid.h>
int
main (int argc, char** argv)
{
pcl::PCLPointCloud2::Ptr cloud (new pcl::PCLPointCloud2 ());
pcl::PCLPointCloud2::Ptr cloud_filtered (new pcl::PCLPointCloud2 ());
// Fill in the cloud data
pcl::PCDReader reader;
// Replace the path below with the path where you saved your file
reader.read ("table_scene_lms400.pcd", *cloud); // Remember to download the file first!
std::cerr << "PointCloud before filtering: " << cloud->width * cloud->height
<< " data points (" << pcl::getFieldsList (*cloud) << ").";
// Create the filtering object
pcl::VoxelGrid<pcl::PCLPointCloud2> 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::PCDWriter writer;
writer.write ("table_scene_lms400_downsampled.pcd", *cloud_filtered,
Eigen::Vector4f::Zero (), Eigen::Quaternionf::Identity (), false);
return (0);
}
这里就只写一次CMakeLists.txt,其他的使用PCL都是类似的
cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
project(voxelgrid_filter)
//add PCL dependency
find_package(PCL 1.7 REQUIRED)
include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITION})
add_executable(voxelgrid_filter main.cpp)
target_link_libraries(voxelgrid_filter ${PCL_LIBRARIES})
install(TARGETS voxelgrid_filter RUNTIME DESTINATION bin)
看到点云的数量几乎变成原来的十分之一。。。。
可以看到橙色是原来采样之前的点是比较密集的,蓝色的是采样后的点,就变得相对来说更加稀疏了,如果继续修改LeafSize,还可以进行更加稀疏的采样,当过于稀疏会使得点云信息减少,对后续的操作不利。