基于libLAS库读取LAS格式点云数据

基于libLAS库将las格式的点云文件转化为pcd格式点云并进行显示!

1.示例代码:

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <liblas/liblas.hpp>
#include <pcl/io/io.h>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/visualization/common/common.h>


using namespace std;

int main()
{
//打开LAS格式点云文件
std::ifstream ifs("高速公路.las", std::ios::in | std::ios::binary);
liblas::ReaderFactory f;
liblas::Reader reader = f.CreateWithStream(ifs);

//读取LAS文件信息头
liblas::Header const& header = reader.GetHeader();
double maxX = header.GetMaxX();
double minX = header.GetMinX();
double maxY = header.GetMaxY();
double minY = header.GetMinY();
double maxZ = header.GetMaxZ();
double minZ = header.GetMinZ();
int nbPoints = header.GetPointRecordsCount();
string signature = header.GetFileSignature();

cout << "maxX: " << maxX << endl;
cout << "minX: " << minX << endl;
cout << "maxY: " << maxY << endl;
cout << "minY: " << minY << endl;
cout << "maxZ: " << maxZ << endl;
cout << "minZ: " << minZ << endl;
cout << "点个数: " << maxX << endl;
cout << "signature: " << signature << endl;

pcl::PointCloud<pcl::PointXYZRGB> cloud;
cloud.width = nbPoints;
cloud.height = 1;
cloud.is_dense = false;
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();

// 获取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++;
}

//显示彩色点云
boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer(new pcl::visualization::PCLVisualizer("3D Viewer"));
viewer->setBackgroundColor(255, 255, 255);
viewer->addPointCloud(cloud.makeShared(), "clound");
while (!viewer->wasStopped())
{
viewer->spinOnce(100);
boost::this_thread::sleep(boost::posix_time::microseconds(100000));
}

//输出pcd格式点云
pcl::io::savePCDFileASCII("高速公路.pcd", cloud);

system("pause");
return (0);
}

2.实验结果:

读取las点云并显示

3.备注:

1).参考:点云库PCL从入门到精通 书中配套案例(该链接中第三章目录下含有编译好的libLAS库,可直接下载使用)。
2).依赖库:PCL1.8、libLAS。

-------------本文结束感谢您的阅读-------------