forked from NNU-GISA/ALS_Refine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloudprocessing.cpp
256 lines (210 loc) · 9.09 KB
/
cloudprocessing.cpp
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
//
// This file is for the general implements of several famous point cloud processing algorithms
// Dependent 3rd Libs: PCL (>1.7)
// Author: Yue Pan et al. @ WHU LIESMARS
//
//PCL
#include <pcl/filters/extract_indices.h>
#include <pcl/segmentation/progressive_morphological_filter.h>
#include <pcl/ModelCoefficients.h>
#include <pcl/sample_consensus/method_types.h>
#include <pcl/sample_consensus/model_types.h>
#include <pcl/segmentation/sac_segmentation.h>
#include <pcl/filters/statistical_outlier_removal.h>
#include <pcl/surface/concave_hull.h>
#include <pcl/kdtree/kdtree_flann.h>
#include <pcl/ModelCoefficients.h>
#include <pcl/filters/project_inliers.h>
#include "cloudprocessing.h"
#include "utility.h"
template<typename PointT>
void CProceesing<PointT>::planesegRansac(const typename pcl::PointCloud<PointT>::Ptr &cloud, float threshold, typename pcl::PointCloud<PointT>::Ptr & planecloud)
{
pcl::ModelCoefficients::Ptr coefficients(new pcl::ModelCoefficients);
pcl::PointIndices::Ptr inliers(new pcl::PointIndices);
// Create the segmentation object
pcl::SACSegmentation<PointT> seg;
// Optional
seg.setOptimizeCoefficients(true);
// Mandatory
seg.setModelType(pcl::SACMODEL_PLANE);
seg.setMethodType(pcl::SAC_RANSAC);
seg.setDistanceThreshold(threshold);
seg.setInputCloud(cloud);
seg.segment(*inliers, *coefficients);
if (inliers->indices.size() == 0)
{
PCL_ERROR("Could not estimate a planar model for the given dataset.");
}
/*cout << "Model coefficients: " << coefficients->values[0] << " "
<< coefficients->values[1] << " "
<< coefficients->values[2] << " "
<< coefficients->values[3] << std::endl;*/
cout << "Model inliers number: " << inliers->indices.size() << std::endl;
for (size_t i = 0; i < inliers->indices.size(); ++i)
{
planecloud->push_back(cloud->points[inliers->indices[i]]);
}
}
template<typename PointT>
void CProceesing<PointT>::groundprojection(const typename pcl::PointCloud<PointT>::Ptr &cloud, typename pcl::PointCloud<PointT>::Ptr & projcloud)
{
// Create a set of planar coefficients with X=Y=Z=0
pcl::ModelCoefficients::Ptr coefficients(new pcl::ModelCoefficients());
coefficients->values.resize(4);
coefficients->values[0] = coefficients->values[1] = coefficients->values[3] = 0;
coefficients->values[2] = cloud->points[0].z;
// Create the filtering object
pcl::ProjectInliers<PointT> proj;
proj.setModelType(pcl::SACMODEL_PLANE);
proj.setInputCloud(cloud);
proj.setModelCoefficients(coefficients);
proj.filter(*projcloud);
//cout << "Cloud projection completed" << endl;
}
template<typename PointT>
void CProceesing<PointT>::alphashape(const typename pcl::PointCloud<PointT>::Ptr &cloud, float alpha_value, typename pcl::PointCloud<PointT>::Ptr & boundary_cloud)
{
pcl::ConcaveHull<PointT> chull; //创建边缘提取对象;
chull.setInputCloud(cloud); //设置输入点云;
chull.setAlpha(alpha_value);
chull.reconstruct(boundary_cloud);
//std::cout<< "Concave hull has: " << boundary_cloud->points.size() << " data points." << endl;
}
template<typename PointT>
void CProceesing<PointT>::CornerpointKNN(const typename pcl::PointCloud<PointT>::Ptr & boundary_cloud, int K, float disthreshold, float maxcos, typename pcl::PointCloud<PointT>::Ptr & corner_cloud)
{
// 先建KDtree
pcl::KdTreeFLANN <PointT> kdtree;
kdtree.setInputCloud(boundary_cloud);
vector<int> pointIdxNKNSearch(K); //距离升序排列
vector<float> pointNKNSquaredDistance(K); //注意是距离平方
for (int i = 0; i < boundary_cloud->size(); i++){
kdtree.nearestKSearch(boundary_cloud->points[i], K, pointIdxNKNSearch, pointNKNSquaredDistance); // K 近邻搜索结果
float max1_max2;
max1_max2 = sqrt(pointNKNSquaredDistance[K - 1]) - sqrt(pointNKNSquaredDistance[K - 2]);
float Xa, Xb, Xo, Ya, Yb, Yo, AOpBO, AO, BO, cosAOB;
Xo = boundary_cloud->points[i].x;
Yo = boundary_cloud->points[i].y;
Xa = boundary_cloud->points[pointIdxNKNSearch[K - 1]].x;
Ya = boundary_cloud->points[pointIdxNKNSearch[K - 1]].y;
if (max1_max2 < disthreshold) //若距离最大与次大点间距小于阈值,认为它们处于同侧,找异侧点
{
float maxdis = 0;
int maxindex = -1;
float Xc, Yc, Xd, Yd;
Xc = boundary_cloud->points[pointIdxNKNSearch[K - 2]].x;
Yc = boundary_cloud->points[pointIdxNKNSearch[K - 2]].y;
//次远点找之前邻域点中的最远点
for (int j = 0; j < K - 2; j++){
Xd = boundary_cloud->points[pointIdxNKNSearch[j]].x;
Yd = boundary_cloud->points[pointIdxNKNSearch[j]].y;
float dis = sqrt((Xd - Xc)*(Xd - Xc) + (Yd - Yc)*(Yd - Yc));
if (dis > maxdis) {
maxdis = dis;
maxindex = j;
}
}
Xb = boundary_cloud->points[pointIdxNKNSearch[maxindex]].x;
Yb = boundary_cloud->points[pointIdxNKNSearch[maxindex]].y;
}
//否则直接接受
else{
Xb = boundary_cloud->points[pointIdxNKNSearch[K - 2]].x;
Yb = boundary_cloud->points[pointIdxNKNSearch[K - 2]].y;
}
//夹角计算
AOpBO = (Xa - Xo)*(Xb - Xo) + (Ya - Yo)*(Yb - Yo);
AO = sqrt((Xa - Xo)*(Xa - Xo) + (Ya - Yo)*(Ya - Yo));
BO = sqrt((Xb - Xo)*(Xb - Xo) + (Yb - Yo)*(Yb - Yo));
cosAOB = abs(AOpBO / AO / BO);
if (cosAOB < maxcos) corner_cloud->push_back(boundary_cloud->points[i]); //夹角满足条件,认为是角点
}
}
template<typename PointT>
void CProceesing<PointT>::CornerpointRadius(const typename pcl::PointCloud<PointT>::Ptr & boundary_cloud, float radius, float disthreshold, float maxcos, typename pcl::PointCloud<PointT>::Ptr & corner_cloud)
{
// 先建KDtree
pcl::KdTreeFLANN <PointT> kdtree;
kdtree.setInputCloud(boundary_cloud);
// Neighbors within radius search
std::vector<int> pointIdxRadiusSearch;
std::vector<float> pointRadiusSquaredDistance;
for (int i = 0; i < boundary_cloud->size(); i++){
if (kdtree.radiusSearch(boundary_cloud->points[i], radius, pointIdxRadiusSearch, pointRadiusSquaredDistance)>2){
int K = pointIdxRadiusSearch.size(); // Radius 内的近邻点数
float max1_max2;
max1_max2 = sqrt(pointRadiusSquaredDistance[K - 1]) - sqrt(pointRadiusSquaredDistance[K - 2]);
float Xa, Xb, Xo, Ya, Yb, Yo, AOpBO, AO, BO, cosAOB;
Xo = boundary_cloud->points[i].x;
Yo = boundary_cloud->points[i].y;
Xa = boundary_cloud->points[pointIdxNKNSearch[K - 1]].x;
Ya = boundary_cloud->points[pointIdxNKNSearch[K - 1]].y;
if (max1_max2 < disthreshold) //若距离最大与次大点间距小于阈值,认为它们处于同侧,找异侧点
{
float maxdis = 0;
int maxindex = -1;
float Xc, Yc, Xd, Yd;
Xc = boundary_cloud->points[pointIdxRadiusSearch[K - 2]].x;
Yc = boundary_cloud->points[pointIdxRadiusSearch[K - 2]].y;
//次远点找之前邻域点中的最远点
for (int j = 0; j < K - 2; j++){
Xd = boundary_cloud->points[pointIdxRadiusSearch[j]].x;
Yd = boundary_cloud->points[pointIdxRadiusSearch[j]].y;
float dis = sqrt((Xd - Xc)*(Xd - Xc) + (Yd - Yc)*(Yd - Yc));
if (dis > maxdis) {
maxdis = dis;
maxindex = j;
}
}
Xb = boundary_cloud->points[pointIdxRadiusSearch[maxindex]].x;
Yb = boundary_cloud->points[pointIdxRadiusSearch[maxindex]].y;
}
//否则直接接受
else{
Xb = boundary_cloud->points[pointIdxRadiusSearch[K - 2]].x;
Yb = boundary_cloud->points[pointIdxRadiusSearch[K - 2]].y;
}
//夹角计算
AOpBO = (Xa - Xo)*(Xb - Xo) + (Ya - Yo)*(Yb - Yo);
AO = sqrt((Xa - Xo)*(Xa - Xo) + (Ya - Yo)*(Ya - Yo));
BO = sqrt((Xb - Xo)*(Xb - Xo) + (Yb - Yo)*(Yb - Yo));
cosAOB = abs(AOpBO / AO / BO);
if (cosAOB < maxcos) corner_cloud->push_back(boundary_cloud->points[i]); //夹角满足条件,认为是角点
}
}
}
template<typename PointT>
void CProceesing<PointT>::GroundFilter_PMF(const typename pcl::PointCloud<PointT>::Ptr &cloud, typename pcl::PointCloud<PointT>::Ptr &gcloud, typename pcl::PointCloud<PointT>::Ptr &ngcloud, int max_window_size, float slope, float initial_distance, float max_distance)
{
pcl::PointIndicesPtr ground_points(new pcl::PointIndices);
pcl::ProgressiveMorphologicalFilter<PointT> pmf;
pmf.setInputCloud(cloud);
pmf.setMaxWindowSize(max_window_size); //20
pmf.setSlope(slope);//1.0f
pmf.setInitialDistance(initial_distance);//0.5f
pmf.setMaxDistance(max_distance);//3.0f
pmf.extract(ground_points->indices);
// Create the filtering object
pcl::ExtractIndices<PointT> extract;
extract.setInputCloud(cloud);
extract.setIndices(ground_points);
extract.filter(*gcloud);
//std::cout << "Ground cloud after filtering (PMF): " << std::endl;
//std::cout << *gcloud << std::endl;
// Extract non-ground returns
extract.setNegative(true);
extract.filter(*ngcloud);
//std::out << "Non-ground cloud after filtering (PMF): " << std::endl;
//std::out << *ngcloud << std::endl;
}
template<typename PointT>
void CProceesing<PointT>::SORFilter(const typename pcl::PointCloud<PointT>::Ptr & incloud, int MeanK, double std, typename pcl::PointCloud<PointT>::Ptr & outcloud)
{
// Create the filtering object
pcl::StatisticalOutlierRemoval<PointT> sor;
sor.setInputCloud(incloud);
sor.setMeanK(MeanK); //50
sor.setStddevMulThresh(std);//1.0
sor.filter(outcloud);
}