LibLas读写LAS文件+降采样
前段时间课题组需要写一个点云数据降采样的小程序,要求读写内容都是LAS文件。本来CGAL可以做到,但是发现CGAL中写LAS文件的头文件好像有点问题,经过查阅资料决定用liblas解决。
踩了很多坑,好在最后问题终于解决了。这里把代码记录下来,以供后续查阅。
虽然采用的是pcl中的均匀采样,可是出来的结果貌似并不是很均匀,这个程序在release模式下运行没问题,但是debug模式总是会报一个错误,目前还没找到什么好的办法可以解决。。。。。。。不过目前的需求是能用就行~。
#include <liblas\liblas.hpp>
#include <fstream>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <pcl/filters/voxel_grid.h>
#include <pcl/keypoints/uniform_sampling.h>
#include <pcl/common/common.h>
#include <algorithm>
using namespace pcl;
void las2las(std::string fname)
{
//打开las文件
std::ifstream ifs(fname, std::ios::in | std::ios::binary);
liblas::ReaderFactory f;
liblas::Reader reader = f.CreateWithStream(ifs);
//读取las文件信息头
liblas::Header const& header = reader.GetHeader();
int nbPoints = header.GetPointRecordsCount();
//设置初始偏移量(这一步没有的话会报错,好像是一个什么内存上的问题,偏移量写入输出部分的文件头。保证两份数据在相同坐标体系下)
double x_setoff = header.GetOffsetX();
double y_setoff = header.GetOffsetY();
double z_setoff = header.GetOffsetZ();
//转换为pcl格式
pcl::PointCloud<pcl::PointXYZRGB>::Ptr in_cloud(new pcl::PointCloud<pcl::PointXYZRGB>);
in_cloud->resize(nbPoints);
int i = 0;