forked from denabazazian/Edge_Extraction
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Difference_Eigenvalues.cpp
336 lines (263 loc) · 12 KB
/
Difference_Eigenvalues.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
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
/*
* EigenvalueCovariance.cpp
*
* Created on: May 6, 2015
* Author: dbazazian
*/
#include <iostream>
#include <Eigen/Eigenvalues>
#include <Eigen/Dense>
#include <vector>
#include <math.h>
#include <cmath>
#include <fstream>
#include <string>
#include <vector>
#include <pcl/io/io.h>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/features/integral_image_normal.h>
#include <pcl/features/normal_3d.h>
#include <pcl/common/common_headers.h>
#include <pcl/features/integral_image_normal.h>
#include <pcl/features/normal_3d.h>
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/filters/passthrough.h>
#include <pcl/ModelCoefficients.h>
#include <pcl/filters/project_inliers.h>
#include <pcl/features/shot_omp.h>
#include "pcl/features/fpfh.h"
#include <pcl/io/ply_io.h>
using namespace std;
using namespace Eigen;
int
main (int argc, char*argv[])
{
pcl::PointCloud<pcl::PointXYZRGBA>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZRGBA>);
// pcl::io::loadPCDFile ("/Path/TO/ArtificialPointClouds/Wedge/W0.2S45T45.pcd", *cloud);
// pcl::io::loadPCDFile ("/Path/TO/ArtificialPointClouds/TwoPlane22.pcd", *cloud);
// pcl::io::loadPCDFile ("/Path/TO/ArtificialPointClouds/CubeSharpEdge.pcd", *cloud);
// pcl::io::loadPCDFile ("/Path/TO/ArtificialPointClouds/OnePlane.pcd", *cloud);
// pcl::io::loadPCDFile ("/Path/TO/ArtificialPointClouds/bunny.pcd",*cloud);
// pcl::io::loadPCDFile ("/Path/TO/ArtificialPointClouds/Statue.pcd", *cloud);
// pcl::io::loadPCDFile ("/Path/TO/ArtificialPointClouds/dragon.pcd", *cloud);
// pcl::io::loadPCDFile ("/Path/TO/ArtificialPointClouds/IntersectionThreePlanes.pcd", *cloud);
// pcl::io::loadPCDFile ("/Path/TO/ArtificialPointClouds/AddingNoise/Bunny03Noise50.pcd", *cloud);
pcl::io::loadPCDFile ("/Path/TO/ArtificialPointClouds/TetrahedronMultiple.pcd", *cloud);
// pcl::io::loadPCDFile ("/Path/TO/ArtificialPointClouds/SpherMultiple.pcd", *cloud);
// pcl::io::loadPCDFile ("/Path/TO/ArtificialPointClouds/AimAtShape/trim-starC.pcd", *cloud);
// pcl::io::loadPCDFile ("/Path/TO/ArtificialPointClouds/AimAtShape/VaseC.pcd", *cloud);
// pcl::io::loadPCDFile ("/Path/TO/ArtificialPointClouds/AimAtShape/twirlC.pcd", *cloud);
// pcl::io::loadPCDFile ("/Path/TO/ArtificialPointClouds/AimAtShape/fandiskC.pcd", *cloud);
//pcl::io::loadPCDFile ("/Path/TO/ArtificialPointClouds/AimAtShape/sharp_sphereC.pcd", *cloud);
std::cout << "Number of points in the Cube Input cloud is:"<< cloud->points.size() << std::endl;
pcl::PointCloud<pcl::PointXYZRGBA>::Ptr Normals (new pcl::PointCloud<pcl::PointXYZRGBA>);
Normals->resize(cloud->size());
// K nearest neighbor search
int KNumbersNeighbor = 10; // numbers of neighbors 7 , 120
std::vector<int> NeighborsKNSearch(KNumbersNeighbor);
std::vector<float> NeighborsKNSquaredDistance(KNumbersNeighbor);
int* NumbersNeighbor = new int [cloud ->points.size ()];
pcl::KdTreeFLANN<pcl::PointXYZRGBA> kdtree;
kdtree.setInputCloud (cloud);
pcl::PointXYZRGBA searchPoint;
double* SmallestEigen = new double [cloud->points.size() ];
double* MiddleEigen = new double [cloud->points.size() ];
double* LargestEigen = new double [cloud->points.size() ];
double* DLS = new double [cloud->points.size() ];
double* DLM = new double [cloud->points.size() ];
double* DMS = new double [cloud->points.size() ];
double* Sigma = new double [cloud->points.size() ];
// std::vector<double> SmallestEigen;
// std::vector<double> MiddleEigen;
// std::vector<double> LargestEigen;
//
// std::vector<double> DLS;
// std::vector<double> DML;
// std::vector<double> DMS;
// ************ All the Points of the cloud *******************
for (size_t i = 0; i < cloud ->points.size (); ++i) {
searchPoint.x = cloud->points[i].x;
searchPoint.y = cloud->points[i].y;
searchPoint.z = cloud->points[i].z;
if ( kdtree.nearestKSearch (searchPoint, KNumbersNeighbor, NeighborsKNSearch, NeighborsKNSquaredDistance) > 0 ) {
NumbersNeighbor[i]= NeighborsKNSearch.size (); }
else { NumbersNeighbor[i] = 0; }
float Xmean; float Ymean; float Zmean;
float sum= 0.00;
// Computing Covariance Matrix
for (size_t ii = 0; ii < NeighborsKNSearch.size (); ++ii){
sum += cloud->points[ NeighborsKNSearch[ii] ].x; }
Xmean = sum / NumbersNeighbor[i] ;
sum= 0.00;
for (size_t ii = 0; ii < NeighborsKNSearch.size (); ++ii){
sum += cloud->points[NeighborsKNSearch[ii] ].y;}
Ymean = sum / NumbersNeighbor[i] ;
sum= 0.00;
for (size_t ii = 0; ii < NeighborsKNSearch.size (); ++ii){
sum += cloud->points[NeighborsKNSearch[ii] ].z;}
Zmean = sum / NumbersNeighbor[i] ;
float CovXX; float CovXY; float CovXZ; float CovYX; float CovYY; float CovYZ; float CovZX; float CovZY; float CovZZ;
sum = 0.00 ;
for (size_t ii = 0; ii < NeighborsKNSearch.size (); ++ii){
sum += ( (cloud->points[NeighborsKNSearch[ii] ].x - Xmean ) * ( cloud->points[NeighborsKNSearch[ii] ].x - Xmean ) );}
CovXX = sum / ( NumbersNeighbor[i]-1) ;
sum = 0.00 ;
for (size_t ii = 0; ii < NeighborsKNSearch.size (); ++ii){
sum += ( (cloud->points[NeighborsKNSearch[ii] ].x - Xmean ) * ( cloud->points[NeighborsKNSearch[ii] ].y - Ymean ) );}
CovXY = sum / ( NumbersNeighbor[i]-1) ;
CovYX = CovXY ;
sum = 0.00 ;
for (size_t ii = 0; ii < NeighborsKNSearch.size (); ++ii){
sum += ( (cloud->points[NeighborsKNSearch[ii] ].x - Xmean ) * ( cloud->points[NeighborsKNSearch[ii] ].z - Zmean ) );}
CovXZ= sum / ( NumbersNeighbor[i]-1) ;
CovZX = CovXZ;
sum = 0.00 ;
for (size_t ii = 0; ii < NeighborsKNSearch.size (); ++ii){
sum += ( (cloud->points[NeighborsKNSearch[ii] ].y - Ymean ) * ( cloud->points[NeighborsKNSearch[ii] ].y - Ymean ) );}
CovYY = sum / ( NumbersNeighbor[i]-1) ;
sum = 0.00 ;
for (size_t ii = 0; ii < NeighborsKNSearch.size (); ++ii){
sum += ( (cloud->points[NeighborsKNSearch[ii] ].y - Ymean ) * ( cloud->points[NeighborsKNSearch[ii] ].z - Zmean ) );}
CovYZ = sum / ( NumbersNeighbor[i]-1) ;
CovZY = CovYZ;
sum = 0.00 ;
for (size_t ii = 0; ii < NeighborsKNSearch.size (); ++ii){
sum += ( (cloud->points[NeighborsKNSearch[ii] ].z - Zmean ) * ( cloud->points[NeighborsKNSearch[ii] ].z - Zmean ) );}
CovZZ = sum / ( NumbersNeighbor[i]-1) ;
// Computing Eigenvalue and EigenVector
Matrix3f Cov;
Cov << CovXX, CovXY, CovXZ, CovYX, CovYY, CovYZ, CovZX, CovZY, CovZZ;
SelfAdjointEigenSolver<Matrix3f> eigensolver(Cov);
if (eigensolver.info() != Success) abort();
double EigenValue1 = eigensolver.eigenvalues()[0];
double EigenValue2 = eigensolver.eigenvalues()[1];
double EigenValue3 = eigensolver.eigenvalues()[2];
double Smallest = 0.00; double Middle = 0.00; double Largest= 0.00;
if (EigenValue1< EigenValue2 ) { Smallest = EigenValue1 ; } else { Smallest = EigenValue2 ; }
if (EigenValue3< Smallest ) { Smallest = EigenValue3 ; }
if(EigenValue1 <= EigenValue2 && EigenValue1 <= EigenValue3) {
Smallest = EigenValue1;
if(EigenValue2 <= EigenValue3) {Middle = EigenValue2; Largest = EigenValue3;}
else {Middle = EigenValue3; Largest = EigenValue2;}
}
if(EigenValue1 >= EigenValue2 && EigenValue1 >= EigenValue3)
{
Largest = EigenValue1;
if(EigenValue2 <= EigenValue3) { Smallest = EigenValue2; Middle = EigenValue3; }
else {Smallest = EigenValue3; Middle = EigenValue2;}
}
if ((EigenValue1 >= EigenValue2 && EigenValue1 <= EigenValue3) || (EigenValue1 <= EigenValue2 && EigenValue1 >= EigenValue3))
{
Middle = EigenValue1;
if(EigenValue2 >= EigenValue3){Largest = EigenValue2; Smallest = EigenValue3;}
else{Largest = EigenValue3; Smallest = EigenValue2;}
}
SmallestEigen[i]= Smallest ;
MiddleEigen[i]= Middle;
LargestEigen[i]= Largest;
DLS[i] = std::abs ( SmallestEigen[i] / LargestEigen[i]) ; // std::abs ( LargestEigen[i] - SmallestEigen[i] ) ;
DLM[i] = std::abs ( MiddleEigen[i] / LargestEigen[i]) ; // std::abs ( LargestEigen[i] - MiddleEigen[i] ) ;
DMS[i] = std::abs ( SmallestEigen[i] / MiddleEigen[i]) ; // std::abs ( MiddleEigen[i] - SmallestEigen[i] ) ;
Sigma[i] = (SmallestEigen[i] ) / ( SmallestEigen[i] + MiddleEigen[i] + LargestEigen[i] ) ;
} // For each point of the cloud
std::cout<< " Computing Sigma is Done! " << std::endl;
// Color Map For the difference of the eigen values
double MaxD=0.00 ;
double MinD= cloud ->points.size ();
int Ncolors=256;
for (size_t i = 0; i < cloud ->points.size (); ++i) {
if ( Sigma [i] < MinD) MinD= Sigma [i];
if ( Sigma[i] > MaxD) MaxD = Sigma [i];
}
std::cout<< " Minimum is :" << MinD<< std::endl;
std::cout<< " Maximum is :" << MaxD << std::endl;
// *****************************************
/*
// computing the standard deviation
double ss = 0.00 ;
for (size_t i = 0; i < cloud ->points.size (); ++i) {
ss += Sigma [i] ;}
double avg = ss / cloud ->points.size () ;
ss = 0.00 ;
for (size_t i = 0; i < cloud ->points.size (); ++i) {
ss += (Sigma [i] - avg ) * ( Sigma [i] - avg ) ;}
double stddvtion = sqrt ( ss / ( cloud ->points.size () - 1 ) ) ;
std::cout<< " Standard Deviation is :" << stddvtion << std::endl;
MaxD = ( 2 )* stddvtion;
//MaxD = 10* stddvtion;
// Color table
double line;
double code[Ncolors][3];
ifstream colorcode ( "/Path/TO/ArtificialPointClouds/JetColorDensity/ColorCodes256.txt" );
//store color codes in array
int i=0,j=0;
while( colorcode>> line ) {
code[i][j]=line;
j++;
if (j == 3)
i++;
}
code[1][0] = 0;
code[1][1] = 0;
code[1][2] = 135.468;
// jet color map
int level = 0;
float step = ( ( MaxD - MinD) / Ncolors ) ;
for (size_t i = 0; i < cloud ->points.size (); ++i) {
if ( SmallestEigen [i] <= MaxD ) {
level = floor( (SmallestEigen [i] - MinD ) / step ) ;
cloud->points[i].r = code[ level ][0];
cloud->points[i].g = code[ level ][1];
cloud->points[i].b = code[ level ][2];
} // if sigma less than Max
}
*/
// *****************************************
// *****************************************
int Edgepoints = 0;
// Red and white (khaki)
for (size_t i = 0; i < cloud ->points.size (); ++i) {
cloud->points[i].r = 240;
cloud->points[i].g = 230 ;
cloud->points[i].b = 140;
}
int level = 0;
float step = ( ( MaxD - MinD) / Ncolors ) ;
// level = floor( (Sigma [i] - MinD ) / step ) ;
for (size_t i = 0; i < cloud ->points.size (); ++i) {
if ( Sigma [i] > ( MinD + ( 6* step) ) ) { //6*step
cloud->points[i].r = 255;
cloud->points[i].g = 0 ;
cloud->points[i].b = 0;
// Dim gray....
// cloud->points[i].r = 105;
// cloud->points[i].g = 105 ;
// cloud->points[i].b = 105;
Edgepoints ++;
}
}
// *****************************************
std::cout<< " Number of Edge points is :" << Edgepoints << std::endl;
// writing the Sigma on the disk
// std::ofstream ofsSigma;
// ofsSigma.open("/Path/TO/SigmaDragon.txt");
// for (size_t i = 0; i < cloud ->points.size (); ++i) {
// ofsSigma << Sigma [i]<< ","<< std::endl ;
// }
pcl::PLYWriter writePLY;
// writePLY.write ("/Path/TO/RatioSmallestEigen22.ply", *cloud, false);
// writePLY.write ("/Path/TO/CloudEigeJnetTwirl.ply", *cloud, false);
// writePLY.write ("/Path/TO/CloudEigeJnetDragon.ply", *cloud, false);
// writePLY.write ("/Path/TO/EigenTwoPlane90N10.ply", *cloud, false);
// writePLY.write ("/Path/TO/DragonRedWhite.ply", *cloud, false);
// writePLY.write ("/Path/TO/IntersectionThreePlanes.ply", *cloud, false);
// writePLY.write ("/Path/TO/BunnyNoise50.ply", *cloud, false);
//writePLY.write ("/Path/TO/BunnyEdges.ply", *cloud, false);
writePLY.write ("/Path/TO/TetahedronMultiple.ply", *cloud, false);
pcl::visualization::CloudViewer viewer("Cloud Viewer");
viewer.showCloud(cloud);
while (!viewer.wasStopped ())
{}
return 0;
}