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 2022-10-27 19:40  一杯明月  阅读(704)  评论(0编辑  收藏  举报