-
Notifications
You must be signed in to change notification settings - Fork 50
/
AddingNoise.cpp
246 lines (190 loc) · 9.36 KB
/
AddingNoise.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
/*
* AddingNoise.cpp
*
* Created on: May 22, 2015
* Author: dbazazian
*/
// #define STANDARD_DEVIATION_NEIGHBORS
#define GAUSSIAN_NOISE
#ifdef STANDARD_DEVIATION_NEIGHBORS
#include <iostream>
#include <stdio.h> /* printf, NULL */
#include <stdlib.h> /* srand, rand */
#include "time.h"
#include <boost/thread/thread.hpp>
#include <pcl/common/common_headers.h>
#include <pcl/features/normal_3d.h>
#include <pcl/io/io.h>
#include <pcl/io/pcd_io.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/console/parse.h>
#include <pcl/io/ply_io.h>
int
main (int argc, char*argv[])
{
pcl::PointCloud<pcl::PointXYZRGBA>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZRGBA>);
pcl::PointCloud<pcl::PointXYZRGBA>::Ptr Noisycloud (new pcl::PointCloud<pcl::PointXYZRGBA>);
// pcl::io::loadPCDFile ("path/to/OnePlane.pcd", *cloud);
// pcl::io::loadPCDFile ("path/to/CubeSharpEdge.pcd", *cloud);
pcl::io::loadPCDFile ("path/to/CubeFractal2.pcd", *cloud);
std::cout << "Number of points in the input cloud is:"<< cloud->points.size() << std::endl;
Noisycloud->resize(cloud ->points.size () );
// creat kdtree
pcl::KdTreeFLANN<pcl::PointXYZRGBA> kdtree;
kdtree.setInputCloud (cloud);
pcl::PointXYZRGBA searchPoint;
// K nearest neighbor search
int NumbersNeighbor = 12; // numbers of neighbors 7
std::vector<int> NeighborsKNSearch(NumbersNeighbor);
std::vector<float> NeighborsKNSquaredDistance(NumbersNeighbor);
double* StndDevX = new double [cloud->points.size() ];
double* StndDevY = new double [cloud->points.size() ];
double* StndDevZ = new double [cloud->points.size() ];
//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, NumbersNeighbor, NeighborsKNSearch, NeighborsKNSquaredDistance) > 0 ) {
NumbersNeighbor = NeighborsKNSearch.size (); }
else { NumbersNeighbor = 0; }
// computing VAriance
double sumX = 0.00 ; double sumY = 0.00 ; double sumZ = 0.00 ;
for (size_t ii = 0; ii < NeighborsKNSearch.size (); ++ii) {
sumX += (cloud->points[ NeighborsKNSearch[ii] ].x);
sumY += (cloud->points[ NeighborsKNSearch[ii] ].y);
sumZ += (cloud->points[ NeighborsKNSearch[ii] ].z);
} // For each neighbor of the query point
double AvgX = sumX / NeighborsKNSearch.size () ; double AvgY = sumY / NeighborsKNSearch.size () ; double AvgZ= sumZ / NeighborsKNSearch.size () ;
sumX = 0.00 ; sumY = 0.00 ; sumZ = 0.00 ;
for (size_t ii = 0; ii < NeighborsKNSearch.size (); ++ii) {
sumX += ( ((cloud->points[ NeighborsKNSearch[ii] ].x) - AvgX) * ((cloud->points[ NeighborsKNSearch[ii] ].x) - AvgX) ) ;
sumY += ( ((cloud->points[ NeighborsKNSearch[ii] ].y) - AvgY) * ((cloud->points[ NeighborsKNSearch[ii] ].y) - AvgY) ) ;
sumZ += ( ((cloud->points[ NeighborsKNSearch[ii] ].z) - AvgZ) * ((cloud->points[ NeighborsKNSearch[ii] ].z) - AvgZ) ) ;
} // For each neighbor of the query point
StndDevX [i] = sqrt(sumX / ( NeighborsKNSearch.size () - 1)) ;
StndDevY [i] = sqrt(sumY / ( NeighborsKNSearch.size () - 1)) ;
StndDevZ [i] = sqrt(sumZ / ( NeighborsKNSearch.size () - 1)) ;
} // For each Point of the Cloud
// First copy all the points of main cloud to the noisy cloud
for (size_t i = 0; i < Noisycloud ->points.size (); ++i ) {
// Noisy point cloud
Noisycloud->points[i].x = cloud->points[i].x;
Noisycloud->points[i].y = cloud->points[i].y;
Noisycloud->points[i].z = cloud->points[i].z;
Noisycloud->points[i].r = 255;
Noisycloud->points[i].g = 255;
Noisycloud->points[i].b = 255;
}
// Then add noise to each 10 point of the cloud
for (size_t i = 0; i < Noisycloud ->points.size (); i+=10 ) {
// Noisy point cloud
Noisycloud->points[i].x = cloud->points[i].x + (7* StndDevX[i]);
Noisycloud->points[i].y = cloud->points[i].y + (7* StndDevY[i]);
Noisycloud->points[i].z = cloud->points[i].z + (7* StndDevZ[i]);
Noisycloud->points[i].r = 255;
Noisycloud->points[i].g = 255;
Noisycloud->points[i].b = 255;
}
std::cout << "Number of points in the Noisy cloud is:"<< Noisycloud->points.size() << std::endl;
// write red point cloud to disk
pcl::io::savePCDFile ("path/to//AddingNoise/Frac2withNoise710.pcd", *Noisycloud);
pcl::PLYWriter writePLY;
writePLY.write ("path/to//AddingNoise/Frac2withNoise710.ply", *Noisycloud, false);
// Show the cloud
//pcl::visualization::CloudViewer viewer(" ONePlaneWithNoise ");
//viewer.showCloud(Noisycloud);
//while (!viewer.wasStopped ())
//{}
return (0);
}
#endif
#ifdef GAUSSIAN_NOISE
#include <iostream>
#include <stdio.h> /* printf, NULL */
#include <stdlib.h> /* srand, rand */
#include <math.h>
#include "time.h"
#include <boost/thread/thread.hpp>
#include <pcl/common/common_headers.h>
#include <pcl/features/normal_3d.h>
#include <pcl/io/io.h>
#include <pcl/io/pcd_io.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/console/parse.h>
#include <pcl/io/ply_io.h>
#define PI 3.14159265
int
main (int argc, char*argv[])
{
pcl::PointCloud<pcl::PointXYZRGBA>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZRGBA>);
pcl::PointCloud<pcl::PointXYZRGBA>::Ptr Noisycloud (new pcl::PointCloud<pcl::PointXYZRGBA>);
// pcl::io::loadPCDFile ("path/to/OnePlane.pcd", *cloud);
// pcl::io::loadPCDFile ("path/to/CubeSharpEdge.pcd", *cloud);
// pcl::io::loadPCDFile ("path/to/CubeFractal2.pcd", *cloud);
// pcl::io::loadPCDFile ("path/to/TwoPlane22.pcd", *cloud);
// pcl::io::loadPCDFile ("path/to/bunny.pcd",*cloud);
pcl::io::loadPCDFile ("path/to/Tetrahedron.pcd", *cloud);
std::cout << "Number of points in the input cloud is:"<< cloud->points.size() << std::endl;
Noisycloud->resize(cloud ->points.size () );
// First copy all the points of main cloud to the noisy cloud
for (size_t i = 0; i < Noisycloud ->points.size (); ++i ) {
// Noisy point cloud
Noisycloud->points[i].x = cloud->points[i].x;
Noisycloud->points[i].y = cloud->points[i].y;
Noisycloud->points[i].z = cloud->points[i].z;
Noisycloud->points[i].r = 255;
Noisycloud->points[i].g = 255;
Noisycloud->points[i].b = 255;
}
double* GussNosX = new double [cloud->points.size() ];
double* GussNosY = new double [cloud->points.size() ];
double* GussNosZ = new double [cloud->points.size() ];
double variance = 0.3 ;
double mean = 0.00;
// http://forums.codeguru.com/showthread.php?459963-Adding-noise-to-image
//All the Points of the cloud
for (size_t i = 0; i < cloud ->points.size (); i+=35) { // 10 , 50
double u1 = (((((float) rand()) / (float) RAND_MAX) * (0.003 - 0.00)) + 0.00) ;
double u2 = (((((float) rand()) / (float) RAND_MAX) * (0.003- 0.00)) + 0.00) ;
// if (u1 > 0.005 ) u1 = 0.005;
// temp = sqrt(-2.0*variance*log(u1));
// tempint = p[ix][iy] + (int) (temp * sin(TWO_PI*u2) + mean);
double temp = sqrt(-2.0*variance*log(u1));
double tempin = (temp * sin(2* PI *u2) + mean);
Noisycloud->points[i].x = cloud->points[i].x + tempin;
Noisycloud->points[i].y = cloud->points[i].y + tempin;
Noisycloud->points[i].z = cloud->points[i].z + tempin;
}// For each point
// Then add noise to each 10 point of the cloud
// for (size_t i = 0; i < Noisycloud ->points.size (); i+=10 ) {
// // Noisy point cloud
// Noisycloud->points[i].x = cloud->points[i].x + (7* StndDevX[i]);
// Noisycloud->points[i].y = cloud->points[i].y + (7* StndDevY[i]);
// Noisycloud->points[i].z = cloud->points[i].z + (7* StndDevZ[i]);
// Noisycloud->points[i].r = 255;
// Noisycloud->points[i].g = 255;
// Noisycloud->points[i].b = 255;
// }
std::cout << "Number of points in the Noisy cloud is:"<< Noisycloud->points.size() << std::endl;
// write red point cloud to disk
// pcl::io::savePCDFile ("path/to/AddingNoise/Two22plane14Noise.pcd", *Noisycloud);
// pcl::io::savePCDFile ("path/to/AddingNoise/Bunny03Noise50.pcd", *Noisycloud);
pcl::io::savePCDFile ("path/to/AddingNoise/TetrahedronNoise35.pcd", *Noisycloud);
// pcl::io::savePCDFile ("path/to/AddingNoise/Frac2Guass14Noise.pcd", *Noisycloud);
// pcl::io::savePCDFile ("path/to/AddingNoise/Frac2withNoise710.pcd", *Noisycloud);
pcl::PLYWriter writePLY;
writePLY.write ("path/to//AddingNoise/TetrahedronNoise35.ply", *Noisycloud, false);
// writePLY.write ("path/to//AddingNoise/Bunny03Noise50.ply", *Noisycloud, false);
// writePLY.write ("path/to/AddingNoise/Frac2Guass14Noise.ply", *Noisycloud, false);
// writePLY.write ("path/to/AddingNoise/Frac2withNoise710.ply", *Noisycloud, false);
// Show the cloud
pcl::visualization::CloudViewer viewer(" ONePlaneWithNoise ");
viewer.showCloud(Noisycloud);
while (!viewer.wasStopped ())
{}
return (0);
}
#endif