-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFeatureExtract.cpp
More file actions
401 lines (344 loc) · 15 KB
/
Copy pathFeatureExtract.cpp
File metadata and controls
401 lines (344 loc) · 15 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
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
#include "MainFrame.h"
void MainFrame::AllFeatureExtract(const std::string& project_files, const std::vector<std::string>& features_need) {
// Open the CSV file for writing
std::string csv_path = project_files + "/feature.csv";
std::ofstream csv_file(csv_path, std::ios::out | std::ios::trunc);
if (!csv_file.is_open()) {
std::cerr << "Error: Could not open CSV file " << csv_path << std::endl;
return;
}
Log("Created csv: " + project_files + "/feature.csv");
// Write the headers based on the required features
csv_file << "SID";
if (std::find(features_need.begin(), features_need.end(), "Geometrical") != features_need.end()) {
csv_file << ",Noofpoints,Xmax,Xmin,Ymax,Ymin,Zmax,Zmin,Zrange";
}
if (std::find(features_need.begin(), features_need.end(), "Statistical") != features_need.end()) {
csv_file << ",meanabsht";
}
if (std::find(features_need.begin(), features_need.end(), "Shape") != features_need.end()) {
csv_file << ",ptness,curveness,surfaceness,linearity,planarity,volumetric";
}
if (std::find(features_need.begin(), features_need.end(), "Density") != features_need.end()) {
csv_file << ",Density,HeightVariance";
}
if (std::find(features_need.begin(), features_need.end(), "Color") != features_need.end()) {
csv_file << ",Rmax,Rmin,Gmax,Gmin,Bmax,Bmin,Rmean,Gmean,Bmean,NDVI";
}
if (std::find(features_need.begin(), features_need.end(), "Area") != features_need.end()) {
csv_file << ",Aconv,Apoly,Bpoly";
}
csv_file << "\n";
ProgressBar(1);
// Iterate through the segments folder
std::string segments_folder = project_files + "/segments";
for (const auto& entry : std::filesystem::directory_iterator(segments_folder)) {
std::string filepath = entry.path().string();
std::string filename = entry.path().filename().string();
std::regex re("segment_(\\d+)");
std::smatch match;
if (std::regex_search(filename, match, re)) {
int segment_id = std::stoi(match[1].str());
// Read segment file
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZRGB>());
if (pcl::io::loadPCDFile<pcl::PointXYZRGB>(filepath, *cloud) == -1) {
std::cerr << "Couldn't read file " << filepath << std::endl;
continue;
}
if (cloud->empty()) {
std::cerr << "Error: No points found in file " << filepath << std::endl;
continue;
}
// Initialize feature variables
//geometrical
int no_of_points = cloud->points.size();
double xmax = std::numeric_limits<double>::lowest();
double xmin = std::numeric_limits<double>::max();
double ymax = std::numeric_limits<double>::lowest();
double ymin = std::numeric_limits<double>::max();
double zmax = std::numeric_limits<double>::lowest();
double zmin = std::numeric_limits<double>::max();
double zrange = 0;
// stastical
double meanabsht = 0;
//color
double rmax = 0, rmin = 255, gmax = 0, gmin = 255, bmax = 0, bmin = 255;
double rsum = 0, gsum = 0, bsum = 0;
//Density
Density = 0.0;
HeightVariance = 0.0;
//Area
double Aconv = 0.0;
double Apoly = 0.0;
double Bpoly = 0.0;
// Calculate features
for (const auto& point : cloud->points) {
double x = point.x, y = point.y, z = point.z;
xmax = std::max(xmax, x);
xmin = std::min(xmin, x);
ymax = std::max(ymax, y);
ymin = std::min(ymin, y);
zmax = std::max(zmax, z);
zmin = std::min(zmin, z);
meanabsht += std::abs(z);
// Color features
rmax = std::max(rmax, static_cast<double>(point.r));
rmin = std::min(rmin, static_cast<double>(point.r));
gmax = std::max(gmax, static_cast<double>(point.g));
gmin = std::min(gmin, static_cast<double>(point.g));
bmax = std::max(bmax, static_cast<double>(point.b));
bmin = std::min(bmin, static_cast<double>(point.b));
rsum += point.r;
gsum += point.g;
bsum += point.b;
}
zrange = zmax - zmin;
if (no_of_points > 0) meanabsht /= no_of_points;
double rmean = rsum / no_of_points;
double gmean = gsum / no_of_points;
double bmean = bsum / no_of_points;
// Calculate NDVI
double ndvi = 0;
// Write the features to the CSV file
csv_file << segment_id;
if (std::find(features_need.begin(), features_need.end(), "Geometrical") != features_need.end()) {
csv_file << "," << no_of_points << "," << xmax << "," << xmin << "," << ymax << "," << ymin << "," << zmax << "," << zmin << "," << zrange;
}
if (std::find(features_need.begin(), features_need.end(), "Statistical") != features_need.end()) {
csv_file << "," << meanabsht;
}
if (std::find(features_need.begin(), features_need.end(), "Shape") != features_need.end()) {
// Calculate shape features
ptness = 0;
curveness = 0;
surfaceness = 0;
linearity = 0;
planarity = 0;
volumetric = 0;
arma::mat data = PclToArmaMatrix(filepath);
ComputeShapeFeatures(data);
csv_file << "," << ptness << "," << curveness << "," << surfaceness << "," << linearity << "," << planarity << "," << volumetric;
}
if (std::find(features_need.begin(), features_need.end(), "Density") != features_need.end()) {
// Calculate density features
Density = 0.0;
HeightVariance = 0.0;
ComputeDensityFeatures(filepath);
csv_file << "," << Density << "," << HeightVariance;
}
if (std::find(features_need.begin(), features_need.end(), "Color") != features_need.end()) {
csv_file << "," << rmax << "," << rmin << "," << gmax << "," << gmin << "," << bmax << "," << bmin << "," << rmean << "," << gmean << "," << bmean << "," << ndvi;
}
// Other features would be similarly calculated and written here
if (std::find(features_need.begin(), features_need.end(), "Area") != features_need.end()) {
// Calculate shape features
Aconv = 0.0;
Apoly = 0.0;
Bpoly = 0.0;
ComputeAreaFeatures(filepath);
csv_file << "," << Aconv << "," << Apoly << "," << Bpoly;
}
csv_file << "\n";
}
}
Log("Features extracted successfully");
csv_file.close();
}
arma::mat MainFrame::PclToArmaMatrix(const std::string& filePath) {
// Load point cloud data from PCD file
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZRGB>);
if (pcl::io::loadPCDFile<pcl::PointXYZRGB>(filePath, *cloud) == -1) {
PCL_ERROR("Couldn't read file %s\n", filePath.c_str());
throw std::runtime_error("Error loading PCD file");
}
// Convert to Armadillo matrix
arma::mat data(cloud->points.size(), 3);
for (size_t i = 0; i < cloud->points.size(); ++i) {
data(i, 0) = cloud->points[i].x;
data(i, 1) = cloud->points[i].y;
data(i, 2) = cloud->points[i].z;
}
return data;
}
void MainFrame::ComputeShapeFeatures(const arma::mat& data) {
// Center the data
arma::mat centered = data.each_row() - arma::mean(data, 0);
// Compute the covariance matrix
arma::mat cov = arma::cov(centered);
// Perform eigen decomposition
arma::vec eigval;
arma::mat eigvec;
// Check if eigen decomposition fails
if (!arma::eig_sym(eigval, eigvec, cov)) {
return; // Exit the function or handle the error appropriately
}
// Ensure eigenvalues are valid and populated
if (eigval.is_empty() || eigval.size() < 3) {
return; // Exit the function or handle the error appropriately
}
// Sort eigenvalues in ascending order
try {
eigval = arma::sort(eigval);
}
catch (const std::runtime_error& e) {
return; // Exit the function or handle the error appropriately
}
// Extract eigenvalues
double lambda1 = eigval(0);
double lambda2 = eigval(1);
double lambda3 = eigval(2);
// Handle division by zero errors
if (lambda3 != 0.0) {
ptness = lambda1 / lambda3;
surfaceness = (lambda2 - lambda1) / lambda3;
linearity = (lambda3 - lambda2) / lambda3;
planarity = (lambda2 - lambda1) / lambda3;
volumetric = lambda1 * lambda2 * lambda3;
}
else {
// Handle the case where lambda3 is zero
ptness = 0.0;
surfaceness = 0.0;
linearity = 0.0;
planarity = 0.0;
volumetric = 0.0;
}
// Calculate curveness (handling division by zero separately)
double sum_lambda = lambda1 + lambda2 + lambda3;
if (sum_lambda != 0.0) {
curveness = lambda1 / sum_lambda;
}
else {
curveness = 0.0;
}
}
void MainFrame::ComputeDensityFeatures(const std::string& filePath) {
// Load point cloud data from PCD file
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZRGB>);
if (pcl::io::loadPCDFile<pcl::PointXYZRGB>(filePath, *cloud) == -1) {
PCL_ERROR("Couldn't read file %s\n", filePath.c_str());
Log("Error loading PCD file");
return; // Exit if file loading fails
}
// Compute density
Density = ComputeDensity(cloud);
// Compute height variance
HeightVariance = ComputeHeightVariance(cloud);
}
double MainFrame::ComputeDensity(const pcl::PointCloud<pcl::PointXYZRGB>::Ptr& cloud) {
// Calculate the volume of the convex hull
pcl::ConvexHull<pcl::PointXYZRGB> hull;
hull.setInputCloud(cloud);
pcl::PointCloud<pcl::PointXYZRGB>::Ptr hull_cloud(new pcl::PointCloud<pcl::PointXYZRGB>);
std::vector<pcl::Vertices> polygons;
hull.reconstruct(*hull_cloud, polygons);
if (hull_cloud->points.empty() || polygons.empty()) {
return 0.0;
}
// Calculate the volume of the convex hull using the tetrahedron method
double hull_volume = 0.0;
for (const auto& polygon : polygons) {
if (polygon.vertices.size() != 3) continue; // Only process triangles
const pcl::PointXYZRGB& p1 = hull_cloud->points[polygon.vertices[0]];
const pcl::PointXYZRGB& p2 = hull_cloud->points[polygon.vertices[1]];
const pcl::PointXYZRGB& p3 = hull_cloud->points[polygon.vertices[2]];
// Calculate the volume of the tetrahedron formed by the triangle and the origin
double volume = std::abs(p1.x * (p2.y * p3.z - p3.y * p2.z) -
p1.y * (p2.x * p3.z - p3.x * p2.z) +
p1.z * (p2.x * p3.y - p3.x * p2.y)) / 6.0;
hull_volume += volume;
}
if (hull_volume == 0.0) {
return 0.0;
}
// Calculate density
double density = static_cast<double>(cloud->size()) / hull_volume;
return density;
}
double MainFrame::ComputeHeightVariance(const pcl::PointCloud<pcl::PointXYZRGB>::Ptr& cloud) {
// Compute mean Z value
double mean_z = 0.0;
for (size_t i = 0; i < cloud->size(); ++i) {
mean_z += cloud->points[i].z;
}
mean_z /= cloud->size();
// Compute variance
double variance = 0.0;
for (size_t i = 0; i < cloud->size(); ++i) {
double diff = cloud->points[i].z - mean_z;
variance += diff * diff;
}
variance /= cloud->size();
return variance;
}
void MainFrame::ComputeAreaFeatures(const std::string& filePath) {
// Load point cloud data from PCD file
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZRGB>);
if (pcl::io::loadPCDFile<pcl::PointXYZRGB>(filePath, *cloud) == -1) {
PCL_ERROR("Couldn't read file %s\n", filePath.c_str());
wxMessageBox("Error loading PCD file");
Aconv = 0;
Apoly = 0;
Bpoly = 0;
return;
}
// Project point cloud onto XY plane for Aconv and Apoly
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_xy(new pcl::PointCloud<pcl::PointXYZ>);
for (const auto& point : cloud->points) {
cloud_xy->points.emplace_back(point.x, point.y, 0.0);
}
// Calculate Aconv (area of convex hull in XY plane)
try {
pcl::ConvexHull<pcl::PointXYZ> hull;
pcl::PointCloud<pcl::PointXYZ>::Ptr hull_cloud(new pcl::PointCloud<pcl::PointXYZ>);
std::vector<pcl::Vertices> polygons;
hull.setInputCloud(cloud_xy);
hull.reconstruct(*hull_cloud, polygons);
Aconv = hull.getTotalArea();
// Calculate Apoly (area of polygon in XY plane)
Apoly = 0.0;
for (const auto& polygon : polygons) {
if (polygon.vertices.size() < 3) continue; // Skip degenerate polygons
double area = 0.0;
for (size_t i = 1; i < polygon.vertices.size() - 1; ++i) {
const auto& p0 = hull_cloud->points[polygon.vertices[0]];
const auto& p1 = hull_cloud->points[polygon.vertices[i]];
const auto& p2 = hull_cloud->points[polygon.vertices[i + 1]];
area += std::abs(p0.x * (p1.y - p2.y) + p1.x * (p2.y - p0.y) + p2.x * (p0.y - p1.y)) / 2.0;
}
Apoly += area;
}
}
catch (const std::exception& e) {
Aconv = 0;
Apoly = 0;
}
// Project point cloud onto XZ plane for Bpoly
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_xz(new pcl::PointCloud<pcl::PointXYZ>);
for (const auto& point : cloud->points) {
cloud_xz->points.emplace_back(point.x, 0.0, point.z);
}
// Calculate Bpoly (area of polygon in XZ plane)
try {
pcl::ConvexHull<pcl::PointXYZ> hull_xz;
pcl::PointCloud<pcl::PointXYZ>::Ptr hull_cloud_xz(new pcl::PointCloud<pcl::PointXYZ>);
std::vector<pcl::Vertices> polygons;
hull_xz.setInputCloud(cloud_xz);
hull_xz.reconstruct(*hull_cloud_xz, polygons);
Bpoly = 0.0;
for (const auto& polygon : polygons) {
if (polygon.vertices.size() < 3) continue; // Skip degenerate polygons
double area = 0.0;
for (size_t i = 1; i < polygon.vertices.size() - 1; ++i) {
const auto& p0 = hull_cloud_xz->points[polygon.vertices[0]];
const auto& p1 = hull_cloud_xz->points[polygon.vertices[i]];
const auto& p2 = hull_cloud_xz->points[polygon.vertices[i + 1]];
area += std::abs(p0.x * (p1.z - p2.z) + p1.x * (p2.z - p0.z) + p2.x * (p0.z - p1.z)) / 2.0;
}
Bpoly += area;
}
}
catch (const std::exception& e) {
Bpoly = 0;
}
}