随笔 - 1762  文章 - 0  评论 - 109  阅读 - 431万

Python,C++中点云 .las转.pcd

(39条消息) Python,C++中点云 .las转.pcd_程序媛一枚~的博客-CSDN博客

1. Python .las转.pcd

复制代码
# -*- coding: utf-8 -*-
# 读取las文件 并保留为 XYZI格式的pcd文件

import pcl  # 调用pcl保留pcd文件
from laspy.file import File  # las文件读取
import numpy as np  # np数组处理
import time  # 计算耗时

# las读取转为 pcd的cloud形式,只保留 XYZI
def getCloud():
    file = r"D:/pcd/1001140020191217.las"
    f = File(file, mode='r')
    inFile = np.vstack((f.x, f.y, f.z, f.intensity)).transpose()
    cloud = pcl.PointCloud_PointXYZI()
    cloud.from_array(np.array(inFile, dtype=np.float32))
    f.close()

    return cloud


def main():
    end1 = time.time()
    cloud = getCloud()
    pcl.save(cloud, r"D:/pcd/1001140020191217_las2pcd.pcd")
    end2 = time.time()
    print("las2pcd 耗时:%.2f秒" % (end2 - end1))
    print('-------endl----------')


if __name__ == '__main__':
    main()
复制代码

2. C++ .las转.pcd

用到liblas库,需要安装好PCL1.8.1

start las2pcd.exe D:/pcd/1001140020191217.las D:/pcd/1001140020191217_las2pcd.pcd

复制代码
#include <iostream>
#include <cstdlib>
#include <liblas/liblas.hpp>
#include <pcl/io/io.h>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>

using namespace std;

int main (int argc, char** argv)
{
    std::ifstream ifs(argv[1], std::ios::in | std::ios::binary); // 打开las文件
    liblas::ReaderFactory f;
    liblas::Reader reader = f.CreateWithStream(ifs); // 读取las文件

    unsigned long int nbPoints=reader.GetHeader().GetPointRecordsCount();//获取las数据点的个数

    pcl::PointCloud<pcl::PointXYZI> cloud;
    cloud.width    = nbPoints;    //保证与las数据点的个数一致    
    cloud.height   = 1;            
    cloud.is_dense = true;
    cloud.points.resize (cloud.width * cloud.height);

    int i=0;                
    /* uint16_t r1, g1, b1;    
    int r2, g2, b2;            
    uint32_t rgb;        */    

    while(reader.ReadNextPoint()) 
    {
        // 获取las数据的x,y,z信息
        cloud.points[i].x = (reader.GetPoint().GetX());
        cloud.points[i].y = (reader.GetPoint().GetY());
        cloud.points[i].z = (reader.GetPoint().GetZ());
        cloud.points[i].intensity = (reader.GetPoint().GetIntensity());
        
        //获取las数据的r,g,b信息
        /*r1 = (reader.GetPoint().GetColor().GetRed());
        g1 = (reader.GetPoint().GetColor().GetGreen());
        b1 = (reader.GetPoint().GetColor().GetBlue()); 
        r2 = ceil(((float)r1/65536)*(float)256);
        g2 = ceil(((float)g1/65536)*(float)256);
        b2 = ceil(((float)b1/65536)*(float)256);
        rgb = ((int)r2) << 16 | ((int)g2) << 8 | ((int)b2);
        cloud.points[i].rgb = *reinterpret_cast<float*>(&rgb);*/
        i++; 
    }
  
    pcl::io::savePCDFileASCII (argv[2], cloud);//存储为pcd类型文件
    return (0);
}
复制代码

 

posted on   一杯明月  阅读(820)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
历史上的今天:
2021-10-27 多维随机变量
2020-10-27 Ubuntu中拷贝文件或目录的方法
2020-10-27 .npy文件读取
2019-10-27 ppt演讲者视图不可用的解决办法
2018-10-27 matlab unique()函数
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示