PCL- Las文件处理

前言

在对点云数据处理的时候,很多时候激光雷达扫描的文件不一定是.pcd文件,这个时候需要进行相关文件处理,如Las,laz,e57等文件格式,本文将介绍las文件的读写.

1.引入包

在对las文件进行解析,首先要安装相应的解析包,其解析包是libLas包,在mac,linux系统下,可通过源码进行安装,如下:

git clone https://github.com/libLAS/libLAS.git
cd libLAS
mkdir build
cd build
cmake ..
make
make install

通过上述命令将libLas包安装到系统中,修改项目中的CMakeLists.txt文件,引入该包如下:

find_package(libLAS REQUIRED)
include_directories(${libLAS_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} ${libLAS_LIBRARIES})

2.读las文件

template<typename PointT>
        LasScanDevice<PointT>::LasScanDevice(std::string  path):
        filePath(std::move(path)),istream(std::make_shared<std::ifstream>()){
            istream->open(filePath,std::ios::in);
            reader = std::make_shared<liblas::Reader>(*istream);
        }

        template<typename PointT>
        LasScanDevice<PointT>::~LasScanDevice() {
            istream->close();
        }

        template<typename PointT>
        void LasScanDevice<PointT>::readFromFile(pcl::PointCloud<PointT> &out) {
            liblas::Header header = reader->GetHeader();

            out.height = 1;
            out.width = header.GetPointRecordsCount();
            out.resize(out.height * out.width);

            int i = 0;

            while (reader->ReadNextPoint()){
                liblas::Point point = reader->GetPoint();
                liblas::Color color = point.GetColor();
                PointT pointT;

                out.points[i].x = point.GetX();
                out.points[i].y = point.GetY();
                out.points[i].z = point.GetZ();

                out.points[i].r = color.GetRed();
                out.points[i].g = color.GetGreen();
                out.points[i].b = color.GetBlue();

                i++;
            }

            reader->Reset();
        }

如上代码所示,首先在构造函数里面io流打开一个Las文件,在析构函数中将io流关闭。其中有函数readFromFile为读取Las文件流相关内容,并且将点云数据复制到cloud对象中去。
请添加图片描述

3.写Las文件

  auto filename = "";
  std::ofstream ofs(filename,ios::out|ios::binary);
 if (!ofs.is_open()){
            LOG(WARNING) << "Open File ERROR:" << filename;
        }
        liblas::Header header;

        Eigen::Vector4f minPt;
        Eigen::Vector4f maxPt;
        pcl::getMinMax3D<PointT>(*pointCloudPtr,minPt,maxPt);

        header.SetVersionMajor(1);
        header.SetVersionMinor(2);
        header.SetDataFormatId(liblas::ePointFormat3);
        header.SetScale(0.01,0.01,0.01);
        header.SetPointRecordsCount(pointCloudPtr->points.size());
        header.SetPointRecordsByReturnCount(0, pointCloudPtr->points.size());
        header.SetMax(maxPt[0], maxPt[1], maxPt[2]);
        header.SetMin(minPt[0], minPt[1], minPt[2]);

        liblas::Writer writer(ofs,header);
        liblas::Point point(&header);
        writer.SetHeader(header);

        for (auto i = 0;i<pointCloudPtr->size();i++) {
            double x = pointCloudPtr->points[i].x;
            double y = pointCloudPtr->points[i].y;
            double z = pointCloudPtr->points[i].z;
            point.SetColor(liblas::Color(pointCloudPtr->points[i].r,pointCloudPtr->points[i].g,pointCloudPtr->points[i].b));
            point.SetCoordinates(x,y,z);
            writer.WritePoint(point);
        }

上述代码中,先构建Las文件的Header,其中值得注意的地方是设定最大最小的边界,其中可通过PCL库里面getMinMax3D函数获取最大最小的边界,并将其设置到header中。后续即将点云中的点陆陆续续的写进Las文件中去。

总结

本文完成的Las文件的读写,点云数据在传递之中一般按照并非标准的.pcd文件,因此熟悉各种点云数据的解析格式是很有必要的。

### 如何使用PCLQt读取LAS文件 #### 背景介绍 Point Cloud Library (PCL) 是一个强大的开源库,用于处理三维点云数据。LAS 文件是一种常见的激光雷达数据存储格式,广泛应用于地理信息系统 (GIS) 和遥感领域。为了在 Qt 中集成 PCL 并实现 LAS 文件读取功能,需要完成以下几个方面的设置。 --- #### 安装与配置环境 1. **安装 PCL 库** 参考教程中的建议,在安装 PCL 时需注意路径命名规范[^1]。确保安装过程中不包含特殊字符或空格,以便于后续编译和链接操作顺利进行。 2. **CMake 配置项目** 打开 CMake 工具并按照以下步骤配置源码和构建目录: - 将 `source code` 设置为项目的根目录。 - 将 `build` 设置为目标输出目录。 - 勾选选项 `Grouped` 和 `Advanced` 后点击 `Configure` 按钮[^3]。 3. **Qt 环境准备** 在 Qt Creator 或其他 IDE 中创建一个新的 C++ 项目,并通过 `.pro` 文件引入必要的依赖项。以下是典型的 `.pro` 文件配置: ```plaintext QT += core gui widgets opengl CONFIG += c++17 # 添加 PCL 的头文件和库路径 INCLUDEPATH += /path/to/pcl/include LIBS += -L/path/to/pcl/lib \ -lpcl_common \ -lpcl_io \ -lpcl_visualization # 如果需要支持 OpenGL 渲染器,则额外添加以下内容 QMAKE_CXXFLAGS += -std=c++17 ``` --- #### 示例代码:读取 LAS 文件 下面是一个完整的示例程序,展示如何利用 PCLQt 实现 LAS 文件的加载与显示。 ```cpp #include <pcl/io/las_io.h> #include <pcl/point_types.h> #include <pcl/visualization/cloud_viewer.h> int main(int argc, char** argv) { // 创建 PointCloud 对象 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>); // 加载 LAS 文件 std::string filename = "example.las"; if (pcl::io::loadLASFile(filename, *cloud) == -1) { PCL_ERROR("Error loading LAS file.\n"); return (-1); } std::cout << "Loaded " << cloud->size() << " data points from the LAS file." << std::endl; // 显示点云 pcl::visualization::CloudViewer viewer("Simple Cloud Viewer"); viewer.showCloud(cloud); while (!viewer.wasStopped()) {} return 0; } ``` 上述代码实现了以下功能: - 使用 `pcl::io::loadLASFile()` 方法加载指定的 LAS 文件[^4]。 - 利用 `pcl::visualization::CloudViewer` 提供简单的可视化界面来查看点云数据。 --- #### 关键技术细节说明 1. **LAS 文件的支持** PCL 自带对 LAS 格式的解析能力,因此无需额外安装第三方插件即可直接调用相关接口函数[^4]。 2. **错误处理机制** 当尝试访问不存在或者损坏的 LAS 文件时,应当捕获异常并通过日志记录具体原因,从而提高系统的健壮性和用户体验质量。 3. **性能优化策略** 大型 LAS 数据集可能占用较多内存资源,为此推荐采用分块加载的方式逐步渲染目标区域内的子集而非一次性全部导入整个场景模型到 RAM 中运行计算任务。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

myenjoy_1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值