Python,C++中点云 .las转.pcd
生活随笔
收集整理的這篇文章主要介紹了
Python,C++中点云 .las转.pcd
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Python las 讀取可以參考之前的博客,用laspy 或者 pylas
今天主要解決Python、C++ 的las轉pcd文件
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 clouddef 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);
}
總結
以上是生活随笔為你收集整理的Python,C++中点云 .las转.pcd的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 不孕不育输卵管远端粘连
- 下一篇: 停在那里是哪首歌啊?