-
Notifications
You must be signed in to change notification settings - Fork 5
/
SegmentationTools.h
534 lines (463 loc) · 15 KB
/
SegmentationTools.h
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
#ifndef __SEGMENTATIONTOOLS__
#define __SEGMENTATIONTOOLS__
#include "BaseTools.h"
#include <math.h>
#include <pcl/point_cloud.h>
#include <pcl/kdtree/kdtree_flann.h>
#include <pcl/point_types.h>
#include <pcl/features/normal_3d.h>
namespace SegmentationTools
/************************************************************************/
/* 1.angularClassifier 2.Estimating the normals*/
/************************************************************************/
{
#pragma region angularClassifier
float getMinZvalueofRasterCell(const LiDARBaseTools::GrideIndex* pGrideIndex,
const int nrows,const int ncols,
const std::vector<LiDARBaseTools::LasPoint> &inputCloud,
const int i ,const int j)
/*
todo: get mini Z value of points in a Raster cell
date: 2015.3.9
author: JianPing([email protected])
see: Area-wide roof plane segmentation in airbone LiDAR
point clouds(2010)
*/
{
float minZ = inputCloud[pGrideIndex[i*ncols+j].ptIndex[0]].z;
float tempZ;
for (std::vector<int>::const_iterator it = pGrideIndex[i*ncols+j].ptIndex.begin();
it!=pGrideIndex[i*ncols+j].ptIndex.end();++it)
{
tempZ = inputCloud[*it].z;
if (minZ>tempZ)
{
minZ = tempZ;
}
}
return minZ;
}
bool CalculateEchoRatio(const LiDARBaseTools::GrideIndex* pGrideIndex,
const int nrows,const int ncols,
const int* flag,//地面点(0)非地面点
const std::vector<LiDARBaseTools::LasPoint> &inputCloud,
float* &sER_Raster,
float threshold = 5)
/*
todo: calculate the slope-adaptive Echo Ratio value, and
create a raster of float to store it
date: 2015.3.9
author: JianPing([email protected])
see: Area-wide roof plane segmentation in airbone LiDAR
point clouds(2010)
*/
{
std::cout<<"create slope-adaptive Echo Ratio Raster..."<<std::endl;
sER_Raster = new float[nrows*ncols];
int n3D(0),n2D(0);
float minZ;
for (int i = 0 ; i < nrows ; ++i)
{
for (int j = 0 ; j < ncols ; ++j)
{
n2D = pGrideIndex[i*ncols+j].ptIndex.size();
if (n2D == 0)//no data point in this cell
{
sER_Raster[i*ncols+j] = 0;
continue;
}
if (flag[i*ncols+j] == 0)//是否为地面点
{
sER_Raster[i*ncols+j] = 0;
continue;
}
minZ = SegmentationTools::getMinZvalueofRasterCell(pGrideIndex,nrows,ncols,inputCloud,i,j);
for (std::vector<int>::const_iterator it = pGrideIndex[i*ncols+j].ptIndex.begin();
it!=pGrideIndex[i*ncols+j].ptIndex.end();++it)
{
if (minZ+threshold > inputCloud[*it].z)
{
++n3D;
}
}
sER_Raster[i*ncols+j] = n3D/float(n2D);
n3D = 0;
}
}
std::cout<<"have created slope-adaptive Echo Ratio Raster.\nDone."<<std::endl;
return true;
}
bool pointIsBuilding(const pcl::PointXY &searchPoint,//当前非地面点
const std::vector<int> &pointIdxRadiusSearch,//领域内点ID
const std::vector<float> &pointRadiusSquaredDistance,//领域内各点到当前非地面点距离
const std::vector<LiDARBaseTools::LasPoint> &groundPoints//地面点云
)
/*
todo: judge if a point belong to a building
date: 2015.3.13
author: JianPing([email protected])
see: Area-wide roof plane segmentation in airbone LiDAR
point clouds(2010)
*/
{
const LiDARBaseTools::LasPoint* tempPoint;
float tempAng;
int nPoints = pointIdxRadiusSearch.size();
float *angs = new float[nPoints];
int i = 0;
for(std::vector<int>::const_iterator it = pointIdxRadiusSearch.begin();//计算各点夹角(弧度制)
it != pointIdxRadiusSearch.end() ; ++it)
{
tempPoint = &groundPoints[*it];
tempAng = atan2(tempPoint->y-searchPoint.y,tempPoint->x-searchPoint.x);
if(tempAng < 0)
{
tempAng+=2*3.1415926;
}
angs[i] = tempAng;
i++;
}
float angtrans;
for(i = 0 ; i < nPoints-1 ; i++)//将领域内的点按照角度从小到大排序
{
for(int j = 0 ; j < nPoints-i-1 ; j++)
{
if(angs[j]>angs[j+1])
{
angtrans = angs[j];
angs[j] = angs[j+1];
angs[j+1] = angtrans;
}
}
}
const float pi_2 = (3.1415926/2);//依次计算夹角
for(i = 0 ; i < nPoints-1 ; i++)
{
if((angs[i+1]-angs[i])>pi_2)
{
return true;
}
}
if((angs[0]-angs[nPoints-1]+4*pi_2)>pi_2)
{
return true;
}
return false;
}
/************************************************************************/
/* angularClassifier */
/************************************************************************/
bool angularClassifier(const std::vector<LiDARBaseTools::LasPoint> &nongroundPoints,//非地面点云
const std::vector<LiDARBaseTools::LasPoint> &groundPoints,//地面点云
const float radius,//搜索半径
std::vector<int> &buildingIndex)//建筑点ID
/*
todo: use angularClassifier to classify building and vegetation
in: nonground points
ground points
search radius
date: 2015.3.13
author: JianPing([email protected])
see: Classification of lidar bare-earth points,buildings,vegetation,and
small objects based on region growing and angular classifier
*/
{
std::cout<<"begin angular classifier"<<std::endl;
//1.create ground points kd-Tree
pcl::PointCloud<pcl::PointXY>::Ptr groundcloud(new pcl::PointCloud<pcl::PointXY>);
pcl::KdTreeFLANN<pcl::PointXY> kdTree;//不用手动析构
groundcloud->resize(groundPoints.size());
int i = 0;
for (std::vector<LiDARBaseTools::LasPoint>::const_iterator it = groundPoints.begin();
it!=groundPoints.end() ; ++it)
{
groundcloud->points[i].x = it->x;
groundcloud->points[i].y = it->y;
//groundcloud->points[i].z = it->z;
i++;
}
kdTree.setInputCloud(groundcloud);
//2.Neighbors within radius search
pcl::PointXY searchPoint;//nonground Points
std::vector<int> pointIdxRadiusSearch;
std::vector<float> pointRadiusSquaredDistance;
i = 0;
for (std::vector<LiDARBaseTools::LasPoint>::const_iterator it = nongroundPoints.begin();
it!=nongroundPoints.end() ; ++it)
{
searchPoint.x = it->x;
searchPoint.y = it->y;
//searchPoint.z = it->z;
kdTree.radiusSearch(searchPoint,radius,pointIdxRadiusSearch,pointRadiusSquaredDistance);
//std::cout<<pointIdxRadiusSearch.size()<<std::endl;
if (pointIdxRadiusSearch.size() ==0)
{
buildingIndex.push_back(i);
i++;
continue;
}
if(true == pointIsBuilding(searchPoint,pointIdxRadiusSearch,pointRadiusSquaredDistance,groundPoints))
{
buildingIndex.push_back(i);
}
i++;
pointIdxRadiusSearch.clear();
pointRadiusSquaredDistance.clear();
}
std::cout<<"angular classifier done.\n"<<std::endl;
return true;
}
#pragma endregion
#pragma region Estimating the normals
/************************************************************************/
/* Estimating the normals */
/************************************************************************/
struct SurfaceSegment
{
int SegmentID;
std::vector<int> PointID;
float normal_x;
float normal_y;
float normal_z;
unsigned char ClassLabel;//0建筑,1植被,2散乱点云
};
pcl::PointCloud<pcl::PointXYZ>::Ptr m_cloud(new pcl::PointCloud<pcl::PointXYZ>);
pcl::NormalEstimation< pcl::PointXYZ , pcl::Normal> m_ne;
pcl::search::KdTree<pcl::PointXYZ>::Ptr m_tree(new pcl::search::KdTree<pcl::PointXYZ>);
pcl::PointCloud<pcl::Normal>::Ptr m_cloud_normals(new pcl::PointCloud<pcl::Normal>);
const int K = 20;
std::deque<int> m_seedPointdeque;//种子点(按残差排序)
std::vector<SurfaceSegment> m_SurfaceSeg;//划分的面
bool getSeedPointsIndex(const float searchRadius)
/*
todo: 得到建筑物种子点(根据领域内点到拟合面的残差的方差排序),存入seedPointdeque
date: 2015.3.23
author: JianPing([email protected])
see: Area-wide roof plane segmentation in airborne
LiDAR point clouds(2012)
*/
{
std::cout<<"begin to get seed"<<std::endl;
float *roughness = new float[m_cloud->points.size()];//残差
std::vector<int> pointIdxKNNSearch(K);
std::vector<float> pointKNNSquaredDistance(K);
pcl::PointXYZ searchPoint;
pcl::Normal searchPointNormal;
std::vector <double> difDis;//领域内满足searchRadius内的点的残差
/***计算每个点领域残差***/
for (int i = 0 ; i < m_cloud->points.size() ; i++)
{
searchPoint = m_cloud->points[i];
searchPointNormal = m_cloud_normals->points[i];
m_tree->nearestKSearch(searchPoint,K,pointIdxKNNSearch,pointKNNSquaredDistance);
for (int j = 0 ; j < K ; j++)
{
if (pointKNNSquaredDistance[j] <= searchRadius)
{
/*计算邻域点到种子点所在平面的垂直距离*/
float x1,y1,z1;
double dis;
//std::cout<<pointIdxKNNSearch[j]<<"\n";
x1 = m_cloud->points[pointIdxKNNSearch[j]].x;
y1 = m_cloud->points[pointIdxKNNSearch[j]].y;
z1 = m_cloud->points[pointIdxKNNSearch[j]].z;//平面外一点(x1,y1,z1)
//std::cout<<x1<<" "<<y1<<" "<<z1<<"\n";
double g;
g = sqrt(searchPointNormal.normal_x*searchPointNormal.normal_x+
searchPointNormal.normal_y*searchPointNormal.normal_y+
searchPointNormal.normal_z*searchPointNormal.normal_z);//求平面方程系数nx,ny,nz的平方和的开平方
double f1 = searchPointNormal.normal_x*(x1-searchPoint.x);
double f2 = searchPointNormal.normal_x*(y1-searchPoint.y);
double f3 = searchPointNormal.normal_x*(z1-searchPoint.z);
double f = fabs(f1 + f2 + f3);
dis = (f/g);
difDis.push_back(dis);
}
}
/***计算残差的方差***/
double average(0),s(0);
for (std::vector<double>::iterator it = difDis.begin() ; it!=difDis.end() ; it++)
{
average+=*it;
}
average/=difDis.size();
for (std::vector<double>::iterator it = difDis.begin() ; it!=difDis.end() ; it++)
{
s+=(fabs(*it - average)*fabs(*it - average));
}
if (difDis.size() == 0)//单独一点
{
roughness[i] = 99999;
}
else
{
roughness[i] = sqrt(s/difDis.size());
}
/***清理***/
difDis.clear();
pointIdxKNNSearch.clear();
pointKNNSquaredDistance.clear();
}
/***roughness按照从小到大排序***/
int *pointIdx = new int[m_cloud->points.size()];
for (int i = 0 ; i < m_cloud->points.size() ; i++)
{
pointIdx[i] = i;//初始化
}
float ftemp;int ntemp;
for (int i = 0 ; i < m_cloud->points.size()-1 ; i++)
{
for (int j = 0 ; j < m_cloud->points.size()-i-1 ; j++)
{
if (roughness[j]>roughness[j+1])
{
ftemp = roughness[j];
roughness[j] = roughness[j+1];
roughness[j+1] = ftemp;
ntemp = pointIdx[j];
pointIdx[j] = pointIdx[j+1];
pointIdx[j+1] = ntemp;
}
}
}
for (int i = 0 ; i < m_cloud->points.size() ; i++)
{
m_seedPointdeque.push_back(pointIdx[i]);
}
delete [] pointIdx;
delete [] roughness;
return true;
}
bool pointSegmentaionCore(const float alpha , const float dist)
/*
todo: 种子点生长
date: 2015.3.24
author: JianPing([email protected])
see: Area-wide roof plane segmentation in airborne
LiDAR point clouds(2012)
*/
{
float cosT = cos(alpha*3.14159/180.);
std::cout<<"cosT: "<<cosT<<std::endl;
std::cout<<"dist: "<<dist<<std::endl;
/***记录点是否分类***/
int *pointClassLabel = new int[m_cloud->points.size()];
for (int i = 0 ; i < m_cloud->points.size() ; i++)
{
pointClassLabel[i] = -1;//初始化为未标记
}
SurfaceSegment tempSeg;
pcl::PointXYZ searchPoint;
pcl::Normal searchPointNormal;
std::vector<int>pointIdxSearch;
std::vector<float>pointNKNSquaredDistance;
/*建立队列deque,用于存储一个分割区域的种子点*/
std::deque <int> tempseed;
while(!m_seedPointdeque.empty())
{
int tempIdx = m_seedPointdeque.front();
m_seedPointdeque.pop_front();
if ( pointClassLabel[tempIdx]!=-1 )
{
continue;
}
tempseed.push_back(tempIdx);
tempSeg.PointID.push_back(tempIdx);
while(!tempseed.empty())
{
tempIdx = tempseed.front();
tempseed.pop_front();
searchPoint = m_cloud->points[tempIdx];
searchPointNormal = m_cloud_normals->points[tempIdx];
int N = m_tree->nearestKSearch(searchPoint,K,pointIdxSearch,pointNKNSquaredDistance);
for (int i = 1 ; i < N ; i++)
{
if (pointClassLabel[pointIdxSearch[i]] == -1)
{
/*计算邻域点和种子点法向量的夹角*/
float nx1 = m_cloud_normals->points[pointIdxSearch[i]].normal_x;
float ny1 = m_cloud_normals->points[pointIdxSearch[i]].normal_y;
float nz1 = m_cloud_normals->points[pointIdxSearch[i]].normal_z;
float n_n1 = searchPointNormal.normal_x*nx1+
searchPointNormal.normal_y*ny1+
searchPointNormal.normal_z*nz1;
float n_n = sqrt(searchPointNormal.normal_x*searchPointNormal.normal_x+
searchPointNormal.normal_y*searchPointNormal.normal_y+
searchPointNormal.normal_z*searchPointNormal.normal_z);
float n1_n1 = sqrt(nx1*nx1+ny1*ny1+nz1*nz1);
float Cosnormal = abs(n_n1/(n_n*n1_n1));
/*计算邻域点到种子点所在平面的垂直距离*/
float x1,y1,z1;double dis;
x1 = m_cloud->points[pointIdxSearch[i]].x;
y1 = m_cloud->points[pointIdxSearch[i]].y;
z1 = m_cloud->points[pointIdxSearch[i]].z;//平面外一点(x1,y1,z1)
double g;
g = sqrt(searchPointNormal.normal_x*searchPointNormal.normal_x+
searchPointNormal.normal_y*searchPointNormal.normal_y+
searchPointNormal.normal_z*searchPointNormal.normal_z);//求平面方程系数nx,ny,nz的平方和的开平方
double f1 = searchPointNormal.normal_x*(x1-searchPoint.x);
double f2 = searchPointNormal.normal_y*(y1-searchPoint.y);
double f3 = searchPointNormal.normal_z*(z1-searchPoint.z);
double f = fabs(f1 + f2 + f3);
dis = (f/g);
if (Cosnormal > cosT && fabs(dis) < dist)//面状点生长的条件
{
tempSeg.PointID.push_back(pointIdxSearch[i]);
tempseed.push_back(pointIdxSearch[i]);
pointClassLabel[pointIdxSearch[i]] = 0;
}
}
}
pointIdxSearch.clear();
pointNKNSquaredDistance.clear();
}
if (tempSeg.PointID.size() < 5)//点数过少
{
for (int i = 0 ; i < tempSeg.PointID.size() ; i++)
{
pointClassLabel[tempSeg.PointID[i]] = -1;
}
tempSeg.PointID.clear();
tempseed.clear();
continue;
}
m_SurfaceSeg.push_back(tempSeg);
tempSeg.PointID.clear();
tempseed.clear();
}
/***清理***/
delete []pointClassLabel;
return true;
}
//main
bool pointNormalSegmentation(const std::vector<LiDARBaseTools::LasPoint> &nongroundPoints,
const float searchRadius,
const float alpha,
const float dist)
{
std::cout<<"begin pointNormalSegmentation"<<std::endl;
m_cloud->points.resize(nongroundPoints.size());
int i = 0;
for (std::vector<LiDARBaseTools::LasPoint>::const_iterator it = nongroundPoints.begin();
it!= nongroundPoints.end();it++)
{
m_cloud->points[i].x = it->x;
m_cloud->points[i].y = it->y;
m_cloud->points[i].z = it->z;
i++;
}
m_tree->setInputCloud(m_cloud);
m_ne.setInputCloud(m_cloud);
m_ne.setSearchMethod(m_tree);
m_ne.setRadiusSearch(searchRadius);
m_ne.compute(*m_cloud_normals);
/***得到roughness从小到大排序的seedPointdeque***/
getSeedPointsIndex(searchRadius);
/***面生长***/
pointSegmentaionCore(alpha,dist);
std::cout<<"success pointNormalSegmentation"<<std::endl;
return true;
}
#pragma endregion
}
#endif