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文件,因此熟悉各种点云数据的解析格式是很有必要的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

myenjoy_1

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

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

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

打赏作者

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

抵扣说明:

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

余额充值