-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsegment_cluster.cpp
More file actions
300 lines (268 loc) · 11.4 KB
/
Copy pathsegment_cluster.cpp
File metadata and controls
300 lines (268 loc) · 11.4 KB
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
#include <iostream>
// #include "surface_matching.hpp"
// #include "surface_matching/ppf_helpers.hpp"
#include <pcl/io/pcd_io.h>
#include <pcl/io/ply_io.h>
#include <pcl/point_types.h>
#include <pcl/filters/voxel_grid.h>
#include <pcl/filters/statistical_outlier_removal.h>
#include <pcl/segmentation/extract_clusters.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/segmentation/sac_segmentation.h>
#include <pcl/ModelCoefficients.h>
#include <pcl/filters/extract_indices.h>
#include <pcl/filters/fast_bilateral.h>
#include <pcl/filters/passthrough.h>
#include <vcg/complex/algorithms/pointcloud_normal.h>
#include <vcg/complex/complex.h>
// #include <wrap/io_trimesh/import_off.h>
// #include <wrap/io_trimesh/import.h>
// #include <wrap/io_trimesh/export_ply.h>
// #include <apps/QT/trimesh_QT_shared/mesh.h>
#include <thread>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
using namespace std::chrono_literals;
using namespace std;
using namespace pcl;
using namespace vcg;
class MyEdge;
class MyFace;
class MyVertex;
struct MyUsedTypes : public UsedTypes<Use<MyVertex>::AsVertexType,
Use<MyEdge>::AsEdgeType,
Use<MyFace>::AsFaceType>
{
};
class MyVertex : public Vertex<MyUsedTypes, vertex::Coord3f, vertex::Normal3f, vertex::BitFlags>
{
};
class MyFace : public Face<MyUsedTypes, face::FFAdj, face::VertexRef, face::BitFlags>
{
};
class MyEdge : public Edge<MyUsedTypes>
{
};
class MyMesh : public tri::TriMesh<vector<MyVertex>, vector<MyFace>, vector<MyEdge>>
{
};
static void help(const string &errorMessage)
{
cout << "Program init error : " << errorMessage << endl;
cout << "\nUsage : ppf_normal_computation [input model file] [output model file]" << endl;
cout << "\nPlease start again with new parameters" << endl;
}
// char filename[256][256];
// int len = 0;
int trave_dir(char* path, vector<string> &file_names, int depth)
{
DIR *d; //声明一个句柄
struct dirent *file; //readdir函数的返回值就存放在这个结构体中
struct stat sb;
if(!(d = opendir(path)))
{
printf("error opendir %s!!!\n",path);
return -1;
}
int idx=0;
while((file = readdir(d)) != NULL)
{
//把当前目录.,上一级目录..及隐藏文件都去掉,避免死循环遍历目录
if(strncmp(file->d_name, ".", 1) == 0)
continue;
// strcpy(file_names[idx++], file->d_name); //保存遍历到的文件名
// file_names[idx++] = file->d_name;
std::string str5 = file->d_name;
file_names.push_back(str5);
//判断该文件是否是目录,及是否已搜索了三层,这里我定义只搜索了三层目录,太深就不搜了,省得搜出太多文件
// if(stat(file->d_name, &sb) >= 0 && S_ISDIR(sb.st_mode) && depth <= 1)
// {
// trave_dir(file->d_name, depth + 1);
// }
}
closedir(d);
return 0;
}
int main(int argc, char **argv)
{
if (argc < 3)
{
help("Not enough input arguments");
exit(1);
}
// string modelFileName = (string)argv[1];
// string outputFileName = (string)argv[2];
// cv::Mat points, pointsAndNormals;
vector<string> file_ids;
trave_dir("../samples/data/cloud", file_ids, 1);
boost::shared_ptr<visualization::PCLVisualizer> viewer(new visualization::PCLVisualizer("cloud_scene"));
/*设置窗口viewer的背景颜色*/
viewer->setBackgroundColor(0, 0, 0);
for(int i=0; i<file_ids.size(); i++)
{
stringstream ss_in;
ss_in << "../samples/data/cloud/" << file_ids[i];
cout << "path: "<<ss_in.str()<<endl;
PointCloud<PointXYZRGBA>::Ptr cloud(new PointCloud<PointXYZRGBA>);
// PointCloud<PointXYZRGBA>::Ptr cloud_mm(new PointCloud<PointXYZRGBA>);
PointCloud<PointXYZRGBA>::Ptr cloud_filtered(new PointCloud<PointXYZRGBA>);
PointCloud<PointXYZ>::Ptr cloud_filtered_xyz(new PointCloud<PointXYZ>);
PointCloud<PointXYZ>::Ptr cloud_sampled(new PointCloud<PointXYZ>);
PointCloud<PointXYZ>::Ptr sor_cloud(new PointCloud<PointXYZ>);
PointCloud<PointNormal>::Ptr cloud_cluster(new PointCloud<PointNormal>);
// PointCloud<PointXYZ>::Ptr ext_cloud(new PointCloud<PointXYZ>);
PointCloud<PointNormal>::Ptr cloud_point(new PointCloud<PointNormal>);
PointCloud<PointNormal>::Ptr ext_cloud(new PointCloud<PointNormal>);
PointCloud<PointNormal>::Ptr cloud_normal(new PointCloud<PointNormal>);
if (pcl::io::loadPLYFile(ss_in.str(), *cloud) == -1)
{
PCL_ERROR("read false");
return 0;
}
// Apply the filter
pcl::FastBilateralFilter<pcl::PointXYZRGBA> fbf;
fbf.setInputCloud (cloud);
fbf.setSigmaS (5.0);
fbf.setSigmaR (3.0);
// fbf.setKeepOrganized(true);
fbf.filter (*cloud_filtered);
cout << "after bilateral filter point cloud: " <<cloud_filtered->size()<< endl;
//3.直通滤波
// pcl::PointCloud<pcl::PointXYZRGBA>::Ptr cloud_(new pcl::PointCloud<pcl::PointXYZRGBA>);
// pcl::PassThrough<pcl::PointXYZRGBA> pass; //创建滤波器对象
// pass.setInputCloud(cloud_filtered); //设置待滤波的点云
// pass.setFilterFieldName("z"); //设置在Z轴方向上进行滤波
// pass.setFilterLimits(-1500, 0); //设置滤波范围(从最高点向下12米去除)
// pass.setFilterLimitsNegative(false); //保留
// pass.filter(*cloud_);
copyPointCloud(*cloud_filtered, *cloud_filtered_xyz);
//体素化下采样******************************************************
VoxelGrid<PointXYZ> vox;
vox.setInputCloud(cloud_filtered_xyz);
vox.setLeafSize(2.0, 2.0, 2.0);
// vox.setKeepOrganized(true);
vox.filter(*cloud_sampled);
cout << "down sampling point cloud: " <<cloud_sampled->size()<< endl;
//去除离群点********************************************************
StatisticalOutlierRemoval<PointXYZ> sor;
sor.setMeanK(50);
sor.setInputCloud(cloud_sampled);
sor.setStddevMulThresh(2.0);
// sor.setKeepOrganized(true);
sor.filter(*sor_cloud);
cout << "Statistical Outlier Removal: " << sor_cloud->size()<< endl;
// MyMesh m;
// if (tri::io::ImporterOFF<MyMesh>::Open(m, argv[1]) != 0)
// {
// printf("Error reading file %s\n", argv[1]);
// exit(0);
// }
// vcg::tri::io::ImporterPLY<MyMesh>::Open(m, argv[1]);
MyMesh m;
m.Clear();
int vertCount = sor_cloud->width * sor_cloud->height;
vcg::tri::Allocator<MyMesh>::AddVertices(m, vertCount);
for (int i = 0; i < vertCount; ++i)
m.vert[i].P() = vcg::Point3f(sor_cloud->points[i].x, sor_cloud->points[i].y, sor_cloud->points[i].z);
tri::PointCloudNormal<MyMesh>::Param p;
p.fittingAdjNum = 10;
p.smoothingIterNum = 1;
p.viewPoint = {0.0, 0.0, 0.0};
p.useViewPoint = true;
tri::PointCloudNormal<MyMesh>::Compute(m, p, 0);
for (int i = 0; i < m.vert.size(); ++i)
{
PointNormal np;
vcg::Point3f n = m.vert[i].N();
vcg::Point3f p = m.vert[i].P();
np.x = p[0] / 1000.0;
np.y = p[1] / 1000.0;
np.z = p[2] / 1000.0;
np.normal_x = n[0];
np.normal_y = n[1];
np.normal_z = n[2];
cloud_normal->push_back(np);
}
cout << "normal computation: " << cloud_normal->size() << endl;
//欧式聚类*******************************************************
// vector<PointIndices> ece_inlier;
// search::KdTree<PointNormal>::Ptr tree(new search::KdTree<PointNormal>);
// EuclideanClusterExtraction<PointNormal> ece;
// ece.setInputCloud(cloud_normal);
// ece.setClusterTolerance(80);
// ece.setMinClusterSize(1000);
// ece.setMaxClusterSize(200000);
// ece.setSearchMethod(tree);
// ece.extract(ece_inlier);
// //聚类结果展示***************************************************
ExtractIndices<PointNormal> ext;
// int maxInd = 0, maxNum = 0;
// cout << "number of clusters: " << ece_inlier.size() << endl;
// for (int i = 0; i < ece_inlier.size(); i++)
// {
// if (ece_inlier[i].indices.size() > maxNum)
// {
// maxInd = i;
// maxNum = ece_inlier[i].indices.size();
// }
// }
// vector<int> ece_inlier_ext = ece_inlier[maxInd].indices;
// copyPointCloud(*cloud_normal, ece_inlier_ext, *cloud_cluster); //按照索引提取点云数据
// cout << "Euclidean Cluster Extraction: " << cloud_cluster->size() << endl;
//平面分割(RANSAC)********************************************************
SACSegmentation<PointNormal> sac;
PointIndices::Ptr inliner(new PointIndices);
ModelCoefficients::Ptr coefficients(new ModelCoefficients);
PointCloud<PointNormal>::Ptr sac_cloud(new PointCloud<PointNormal>);
sac.setInputCloud(cloud_normal);
sac.setMethodType(SAC_RANSAC);
sac.setModelType(SACMODEL_PLANE);
sac.setMaxIterations(100);
sac.setDistanceThreshold(0.003);
//提取平面(展示并输出)******************************************************
ext.setInputCloud(cloud_normal);
sac.segment(*inliner, *coefficients);
if (inliner->indices.size() == 0)
{
std::cout << "segmentation failure!!" << endl;
}
//按照索引提取点云*************
ext.setIndices(inliner);
ext.setNegative(true);
ext.filter(*ext_cloud);
cout << "plane segmentation: " << ext_cloud->size() << endl;
// save result model
stringstream ss_out;
ss_out << "../samples/data/cloud/" << file_ids[i] << "_normal.ply";
pcl::io::savePLYFile(ss_out.str(), *ext_cloud);
// viewer->addPointCloud<PointNormal>(ext_cloud, "cloud_scene");
// viewer->setPointCloudRenderingProperties(visualization::PCL_VISUALIZER_POINT_SIZE, 1, "cloud_scene");
// cout << "Loading points\n";
// // cv::ppf_match_3d::loadPLYSimple(modelFileName.c_str(), 1).copyTo(points);
// cv::Mat cloud_in = cv::Mat(sor_cloud->size(), 3, CV_32FC1);
// for (int i = 0; i < sor_cloud->size(); i++)
// {
// float *data = cloud_in.ptr<float>(i);
// data[0] = sor_cloud->at(i).x;
// data[1] = sor_cloud->at(i).y;
// data[2] = sor_cloud->at(i).z;
// }
// cout << "Computing normals\n";
// cv::Vec3d viewpoint(0, 0, 0);
// cv::ppf_match_3d::computeNormalsPC3d(cloud_in, pointsAndNormals, 10, false, viewpoint);
// std::cout << "Writing points\n";
// cv::ppf_match_3d::writePLY(pointsAndNormals, outputFileName.c_str());
//the following function can also be used for debugging purposes
//cv::ppf_match_3d::writePLYVisibleNormals(pointsAndNormals, outputFileName.c_str());
std::cout << "Done\n";
// while (!viewer->wasStopped())
// {
// viewer->spinOnce(100);
// std::this_thread::sleep_for(100ms);
// }
}
return 0;
}