-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsamplePCL.cpp
More file actions
714 lines (613 loc) · 25.6 KB
/
samplePCL.cpp
File metadata and controls
714 lines (613 loc) · 25.6 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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
/****************************************************************************\
* Copyright (C) 2022 Infineon Technologies & pmdtechnologies ag
*
* THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
* KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
* PARTICULAR PURPOSE.
*
\****************************************************************************/
#include <royale.hpp>
#include <condition_variable>
#include <algorithm>
#include <mutex>
#include <thread>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/sample_consensus/method_types.h>
#include <pcl/sample_consensus/model_types.h>
#include <pcl/filters/statistical_outlier_removal.h>
#include <pcl/filters/bilateral.h>
#include <pcl/filters/median_filter.h>
#include <pcl/filters/shadowpoints.h>
#include <pcl/filters/approximate_voxel_grid.h>
#include <pcl/filters/conditional_removal.h>
#include <pcl/filters/extract_indices.h>
#include <pcl/filters/filter_indices.h>
#include <pcl/filters/radius_outlier_removal.h>
#include <pcl/filters/conditional_removal.h>
#include <pcl/features/integral_image_normal.h>
#include <pcl/features/normal_3d.h>
#include <pcl/features/organized_edge_detection.h>
#include <pcl/search/search.h>
#include <pcl/search/kdtree.h>
#include <pcl/segmentation/sac_segmentation.h>
#include <pcl/segmentation/region_growing.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <sample_utils/PlatformResources.hpp>
using namespace royale;
using namespace sample_utils;
using namespace std;
using namespace pcl;
using namespace pcl::visualization;
#define M_COLOR_LOOKUP_SIZE 180
namespace
{
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud;
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloudFiltered;
pcl::PointCloud<pcl::PointXYZI>::Ptr cloudIntensity;
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloudPlanes;
pcl::PointCloud<pcl::PointXYZRGB> ::Ptr cloudDuplicate;
mutex cloudMutex;
std::condition_variable cloudCV;
bool newDataAvailable;
bool showNormals;
bool removeOutliers;
bool bilateral;
bool median;
bool shadowpoints;
bool voxelgrid;
bool condition;
bool growRegion;
bool detectPlanes;
typedef struct RgbColor
{
uint8_t r;
uint8_t g;
uint8_t b;
} RgbColor;
typedef struct HsvColor
{
uint8_t h;
uint8_t s;
uint8_t v;
} HsvColor;
RgbColor m_colorLookup[M_COLOR_LOOKUP_SIZE];
RgbColor HsvToRgb(const HsvColor &hsv)
{
RgbColor rgb;
uint16_t region, remainder;
uint8_t p, q, t;
if (hsv.s == 0)
{
rgb.r = hsv.v;
rgb.g = hsv.v;
rgb.b = hsv.v;
return rgb;
}
region = hsv.h / 43;
remainder = static_cast<uint16_t> ((hsv.h % 43) * 6);
p = static_cast<uint8_t> ((hsv.v * (255 - hsv.s)) >> 8);
q = static_cast<uint8_t> ((hsv.v * (255 - ((hsv.s * remainder) >> 8))) >> 8);
t = static_cast<uint8_t> ((hsv.v * (255 - ((hsv.s * (255 - remainder)) >> 8))) >> 8);
switch (region)
{
case 0:
rgb.r = hsv.v;
rgb.g = t;
rgb.b = p;
break;
case 1:
rgb.r = q;
rgb.g = hsv.v;
rgb.b = p;
break;
case 2:
rgb.r = p;
rgb.g = hsv.v;
rgb.b = t;
break;
case 3:
rgb.r = p;
rgb.g = q;
rgb.b = hsv.v;
break;
case 4:
rgb.r = t;
rgb.g = p;
rgb.b = hsv.v;
break;
default:
rgb.r = hsv.v;
rgb.g = p;
rgb.b = q;
break;
}
return rgb;
}
const RgbColor &getColor(const float dist)
{
const float minDist = 1.3f;
const float maxDist = 2.0f;
const float m_spanDist = 1.0f / (maxDist - minDist);
float clampedDist = std::min(maxDist, dist);
clampedDist = std::max(minDist, clampedDist);
int index = std::min<uint16_t>(M_COLOR_LOOKUP_SIZE - 1,
static_cast<uint16_t> (static_cast<float> (M_COLOR_LOOKUP_SIZE - 1) *
(clampedDist - minDist) * m_spanDist));
if (index < 0)
{
index = 0;
}
return m_colorLookup[index];
}
} // namespace
class RoyaleListener : public IDepthDataListener
{
public:
RoyaleListener()
{
}
void onNewData(const DepthData *data)
{
{
std::unique_lock<std::mutex> lock(cloudMutex);
// Fill in the cloud data
cloud->width = data->width;
cloud->height = data->height;
cloud->is_dense = false;
cloud->points.resize(cloud->width * cloud->height);
//duplicate the cloud
cloudDuplicate->width = data->width;
cloudDuplicate->height = data->height;
cloudDuplicate->is_dense = false;
cloudDuplicate->points.resize(cloud->width * cloud->height);
// do the same for cloudIntensity
cloudIntensity->width = data->width;
cloudIntensity->height = data->height;
cloudIntensity->is_dense = false;
cloudIntensity->points.resize(cloudIntensity->width * cloudIntensity->height);
for (size_t i = 0u; i < cloud->points.size(); ++i)
{
cloud->points[i].x = data->points[i].x;
cloud->points[i].y = data->points[i].y;
cloud->points[i].z = data->points[i].z;
cloudDuplicate->points[i].x = data->points[i].x;
cloudDuplicate->points[i].y = data->points[i].y;
cloudDuplicate->points[i].z = data->points[i].z;
cloudIntensity->points[i].x = data->points[i].x;
cloudIntensity->points[i].y = data->points[i].y;
cloudIntensity->points[i].z = data->points[i].z;
const RgbColor col = getColor(data->points[i].z);
cloud->points[i].r = col.r;
cloud->points[i].g = col.g;
cloud->points[i].b = col.b;
cloudDuplicate->points[i].r = col.r;
cloudDuplicate->points[i].g = col.g;
cloudDuplicate->points[i].b = col.b;
// calculate intensity from color
cloudIntensity->points[i].intensity = data->points[i].grayValue / 2000.0f;
if (data->points[i].depthConfidence > 0)
{
cloud->points[i].a = 255;
cloudDuplicate->points[i].a = 255;
}
else
{
cloud->points[i].a = 0;
// if the point is invalid, mark it with a special value
cloud->points[i].x = cloud->points[i].y = cloud->points[i].z =
std::numeric_limits<float>::quiet_NaN();
cloudDuplicate->points[i].a = 0;
}
}
// notify the waiting loop in the main thread
newDataAvailable = true;
}
cloudCV.notify_all();
}
};
void keyboardEvent(const pcl::visualization::KeyboardEvent &event, void *viewer_void)
{
if (event.getKeySym() == "n" && event.keyDown())
{
// toggle computation of normals
showNormals = !showNormals;
}
if (event.getKeySym() == "m" && event.keyDown())
{
// toggle statistical outlier removal
removeOutliers = !removeOutliers;
}
else if (event.getKeySym() == "b" && event.keyDown())
{
// toggle bilateral filtering
bilateral = !bilateral;
}
else if (event.getKeySym() == "v" && event.keyDown())
{
// toggle median filtering
median = !median;
}
else if (event.getKeySym() == "a" && event.keyDown())
{
// toggle shadowpoint removal
shadowpoints = !shadowpoints;
}
else if (event.getKeySym() == "d" && event.keyDown())
{
// toggle downsampling
voxelgrid = !voxelgrid;
}
else if (event.getKeySym() == "y" && event.keyDown())
{
// toggle conditional removal
condition = !condition;
}
else if (event.getKeySym() == "k" && event.keyDown())
{
// toggle region growing
growRegion = !growRegion;
}
else if (event.getKeySym() == "p" && event.keyDown())
{
// toggle plane detection
detectPlanes = !detectPlanes;
}
}
int main(int argc, char *argv[])
{
// Windows requires that the application allocate these, not the DLL.
PlatformResources resources;
// This is the data listener which will receive callbacks. It's declared
// before the cameraDevice so that, if this function exits with a 'return'
// statement while the camera is still capturing, it will still be in scope
// until the cameraDevice's destructor implicitly de-registers the listener.
RoyaleListener listener;
for (auto i = 0u; i < M_COLOR_LOOKUP_SIZE; ++i)
{
auto h = static_cast<uint8_t> (i);
HsvColor tempHsv;
tempHsv.h = h;
tempHsv.s = 255;
tempHsv.v = 255;
m_colorLookup[i] = HsvToRgb(tempHsv);
}
newDataAvailable = false;
showNormals = false;
removeOutliers = false;
bilateral = false;
median = false;
shadowpoints = false;
voxelgrid = false;
condition = false;
growRegion = false;
detectPlanes = false;
// this represents the main camera device object
std::unique_ptr<ICameraDevice> cameraDevice;
// the camera manager will query for a connected camera
{
CameraManager manager;
// check the number of arguments
if (argc > 1)
{
// if the program was called with an argument try to open this as a file
cout << "Trying to open : " << argv[1] << endl;
cameraDevice = manager.createCamera(argv[1]);
}
else
{
// if no argument was given try to open the first connected camera
royale::Vector<royale::String> camlist(manager.getConnectedCameraList());
cout << "Detected " << camlist.size() << " camera(s)." << endl;
if (!camlist.empty())
{
cameraDevice = manager.createCamera(camlist[0]);
}
else
{
cerr << "No suitable camera device detected." << endl
<< "Please make sure that a supported camera is plugged in, all drivers are "
<< "installed, and you have proper USB permission" << endl;
return 1;
}
camlist.clear();
}
}
// the camera device is now available and CameraManager can be deallocated here
if (cameraDevice == nullptr)
{
// no cameraDevice available
if (argc > 1)
{
// there was a problem opening the file
cerr << "Could not open " << argv[1] << endl;
return 1;
}
else
{
// we couldn't open any camera
cerr << "Cannot create the camera device" << endl;
return 1;
}
}
// IMPORTANT: call the initialize method before working with the camera device
auto status = cameraDevice->initialize();
if (status != CameraStatus::SUCCESS)
{
cerr << "Cannot initialize the camera device, error string : " << getErrorString(status) << endl;
return 1;
}
// create PointCloud objects that will hold the original data and the filtered one
cloud.reset(new pcl::PointCloud<pcl::PointXYZRGB>());
cloudFiltered.reset(new pcl::PointCloud<pcl::PointXYZRGB>());
cloudIntensity.reset(new pcl::PointCloud<pcl::PointXYZI>());
cloudPlanes.reset(new pcl::PointCloud<pcl::PointXYZRGB>());
cloudDuplicate.reset(new pcl::PointCloud<pcl::PointXYZRGB>());
pcl::visualization::PCLVisualizer viewer("PCL Viewer");
viewer.setBackgroundColor(0.0, 0.0, 0.0);
viewer.setCameraPosition(0.0f, 0.0f, -2.0f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f);
viewer.setShowFPS(false);
// we want to be able to switch between filtered and unfiltered
// point clouds, that's why we need to register a callback for keyboard events
viewer.registerKeyboardCallback(keyboardEvent, (void *)&viewer);
pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB> rgb(cloud);
pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB> rgb_planes(cloudPlanes);
// register a data listener
if (cameraDevice->registerDataListener(&listener) != CameraStatus::SUCCESS)
{
cerr << "Error registering data listener" << endl;
return 1;
}
// start capturing from the device/file
if (cameraDevice->startCapture() != CameraStatus::SUCCESS)
{
cerr << "Error starting the capturing" << endl;
return 1;
}
pcl::PointCloud<pcl::Normal>::Ptr normals(new pcl::PointCloud<pcl::Normal>);
pcl::IntegralImageNormalEstimation<pcl::PointXYZRGB, pcl::Normal> ne;
// first plane red, second plane blue, third plane green, fourth plane magenta
const std::uint8_t R[20] = { 255, 0, 0, 255, 127, 200, 255, 0, 0, 0, 255, 0, 0, 255, 127, 200, 255, 0, 0, 0 };
const std::uint8_t G[20] = { 0, 0, 255, 0, 0, 255, 255, 0, 0, 0, 0, 0, 255, 0, 0, 255, 255, 0, 0, 0 };
const std::uint8_t B[20] = { 0, 255, 0, 255, 255, 255, 255, 0, 0, 0, 0, 255, 0, 255, 255, 255, 255, 0, 0, 0 };
pcl::RegionGrowing<pcl::PointXYZRGB, pcl::Normal> reg;
pcl::search::Search<pcl::PointXYZRGB>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZRGB>);
std::cout << "Below you see which key toggles which filter : \n - n : show normals\n - m : remove outliers\n - b : bilateral filter\n - v : median filter\n - a : remove shadow points\n - d : voxelgrid\n - y : conditional removal\n - j : take screenshot (implemented by pcl)\n - k : region growing\n - p : plane detection" << std::endl;
while (!viewer.wasStopped())
{
// while the viewer window is not closed, wait for new data to arrive
std::unique_lock<std::mutex> lock(cloudMutex);
auto timeOut = (std::chrono::system_clock::now() + std::chrono::milliseconds(100));
if (cloudCV.wait_until(lock, timeOut, [&] { return newDataAvailable; }))
{
if (removeOutliers)
{
// set up a point cloud outlier removal and show the result
StatisticalOutlierRemoval<pcl::PointXYZRGB> outlierRemoval;
outlierRemoval.setMeanK(50);
outlierRemoval.setStddevMulThresh(1.0);
outlierRemoval.setInputCloud(cloud);
outlierRemoval.filter(*cloud);
}
if (bilateral)
{
// attention: this filter requires intensity values, so use a cloud based on PointXYZI instead of PointXYZRGB
BilateralFilter<pcl::PointXYZI> bilateralFilter;
bilateralFilter.setHalfSize(0.2);
bilateralFilter.setStdDev(0.2);
bilateralFilter.filter(*cloudIntensity);
// display cloudIntensity
viewer.removeAllPointClouds();
if (!viewer.updatePointCloud(cloud, "Bilateral Filtered"))
{
viewer.addPointCloud<pcl::PointXYZI>(cloudIntensity, "Bilateral Filtered", 0);
}
}
if (median)
{
// filter the data with a median filter with window size 11
MedianFilter<pcl::PointXYZRGB> medianFilter;
medianFilter.setInputCloud(cloud);
medianFilter.setWindowSize(11);
medianFilter.filter(*cloud);
}
if (shadowpoints)
{
// compute normals first
ne.setNormalEstimationMethod(ne.AVERAGE_3D_GRADIENT);
ne.setMaxDepthChangeFactor(2.2f);
ne.setNormalSmoothingSize(5.0f);
ne.setInputCloud(cloud);
ne.compute(*normals);
double shadowThreshold = 0.9;
ShadowPoints<pcl::PointXYZRGB, pcl::Normal> shadowPoints;
shadowPoints.setNormals(normals);
shadowPoints.setThreshold(shadowThreshold);
shadowPoints.setInputCloud(cloud);
shadowPoints.filter(*cloud);
}
if (voxelgrid)
{
ApproximateVoxelGrid<pcl::PointXYZRGB> appVoxelgrid;
appVoxelgrid.setInputCloud(cloud);
appVoxelgrid.setLeafSize(0.05, 0.05, 0.05);
appVoxelgrid.setDownsampleAllData(true);
appVoxelgrid.filter(*cloud);
}
if (condition)
{
// set condition: z > 0.0 and z < 1.5
ConditionAnd<pcl::PointXYZRGB>::Ptr c(new pcl::ConditionAnd<pcl::PointXYZRGB>);
c->addComparison(pcl::FieldComparison<pcl::PointXYZRGB>::ConstPtr(new pcl::FieldComparison<pcl::PointXYZRGB>("z", pcl::ComparisonOps::GT, 0.0)));
c->addComparison(pcl::FieldComparison<pcl::PointXYZRGB>::ConstPtr(new pcl::FieldComparison<pcl::PointXYZRGB>("z", pcl::ComparisonOps::LT, 1.5)));
// remove points that don't meet the condition
ConditionalRemoval<pcl::PointXYZRGB> conditionalRemoval;
conditionalRemoval.setInputCloud(cloud);
conditionalRemoval.setCondition(c);
conditionalRemoval.filter(*cloud);
}
if (growRegion)
{
pcl::PointCloud<pcl::Normal>::Ptr normals(new pcl::PointCloud<pcl::Normal>);
pcl::NormalEstimation<pcl::PointXYZRGB, pcl::Normal> normal_estimator;
normal_estimator.setSearchMethod(tree);
normal_estimator.setInputCloud(cloud);
normal_estimator.setKSearch(50);
normal_estimator.compute(*normals);
cloudFiltered->points.resize(cloud->size());
for (size_t i = 0; i < cloud->size(); i++)
{
cloudFiltered->points[i].x = cloud->points[i].x;
cloudFiltered->points[i].y = cloud->points[i].y;
cloudFiltered->points[i].z = cloud->points[i].z;
cloudFiltered->points[i].r = 150;
cloudFiltered->points[i].g = 150;
cloudFiltered->points[i].b = 150;
}
viewer.removeAllPointClouds();
viewer.addPointCloud<pcl::PointXYZRGB>(cloudFiltered, rgb, "Triangulated points");
viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2,
"Triangulated points");
reg.setMinClusterSize(500);
reg.setMaxClusterSize(5000);
reg.setSearchMethod(tree);
reg.setNumberOfNeighbours(10);
reg.setInputCloud(cloud);
reg.setInputNormals(normals);
reg.setSmoothnessThreshold(3.5 / 180.0 * M_PI);
reg.setCurvatureThreshold(90.0);
std::vector<pcl::PointIndices> clusters;
reg.extract(clusters);
if (!clusters.empty ())
{
int colIdx = 0;
for (auto i = 0; i < clusters.size() ; ++i) // iterate over the found clusters
{
pcl::PointIndices::Ptr indicesPtr(new pcl::PointIndices(clusters[i]));
pcl::ExtractIndices<pcl::PointXYZRGB> extract_indices;
extract_indices.setIndices(indicesPtr);
extract_indices.setInputCloud(cloud);
extract_indices.filter(*cloudFiltered);
std::stringstream ss;
ss << "Cluster " << colIdx;
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZRGB> customHandler(
cloudFiltered, R[colIdx % 10], G[colIdx % 10], B[colIdx % 10]);
viewer.addPointCloud<pcl::PointXYZRGB>(cloudFiltered, customHandler, ss.str());
viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 4,
ss.str());
colIdx++;
}
}
}
if (detectPlanes)
{
size_t s = 0u;
pcl::ModelCoefficients::Ptr coefficients(new pcl::ModelCoefficients);
pcl::PointIndices::Ptr inliers(new pcl::PointIndices);
pcl::SACSegmentation<pcl::PointXYZRGB> seg;
seg.setOptimizeCoefficients(true);
seg.setModelType(pcl::SACMODEL_PLANE);
seg.setMethodType(pcl::SAC_RANSAC);
seg.setDistanceThreshold(0.01);
seg.setMaxIterations(1000);
cloudPlanes->points.resize(cloudDuplicate->size());
for (size_t i = 0; i < cloudDuplicate->size(); i++)
{
cloudPlanes->points[i].x = 0.0f;
cloudPlanes->points[i].y = 0.0f;
cloudPlanes->points[i].z = 0.0f;
}
pcl::ExtractIndices<pcl::PointXYZRGB> extract;
auto i = 0;
const int nr_points = (int)cloudDuplicate->size();
while (cloudDuplicate->size() > 0.1 * nr_points) // While x% of the original cloud is still there
{
// Segment the largest planar component from the remaining cloud
seg.setInputCloud(cloudDuplicate);
seg.segment(*inliers, *coefficients);
if (inliers->indices.size() == 0)
{
break;
}
for (auto x : inliers->indices)
{
cloudPlanes->points[s].x = cloudDuplicate->points[x].x;
cloudPlanes->points[s].y = cloudDuplicate->points[x].y;
cloudPlanes->points[s].z = cloudDuplicate->points[x].z;
cloudPlanes->points[s].r = R[i];
cloudPlanes->points[s].g = G[i];
cloudPlanes->points[s].b = B[i];
++s;
}
// Extract the outliers (outliers because we throw all inliers away now, and the remaining
// points get the new point cloud in which we search planes)
extract.setInputCloud(cloudDuplicate);
extract.setIndices(inliers);
extract.setNegative(true);
{
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_f(new pcl::PointCloud<pcl::PointXYZRGB>());
extract.filter(*cloud_f);
cloudDuplicate.swap(cloud_f);
}
++i;
if (i > 3)
{
break;
}
}
std::cout << "s : " << s << " cloudPlanes->size " << cloudPlanes->size() << std::endl;
// points not belonging to a planar surface get grey-ish
for (auto i = 0u; s < cloudDuplicate->size(); ++s, ++i)
{
cloudPlanes->points[s].x = cloudDuplicate->points[i].x;
cloudPlanes->points[s].y = cloudDuplicate->points[i].y;
cloudPlanes->points[s].z = cloudDuplicate->points[i].z;
cloudPlanes->points[s].r = 100;
cloudPlanes->points[s].g = 100;
cloudPlanes->points[s].b = 100;
}
// display cloudPlanes
viewer.removeAllPointClouds();
if (!viewer.updatePointCloud(cloudPlanes, "Planes"))
{
viewer.addPointCloud<pcl::PointXYZRGB>(cloudPlanes, rgb_planes, "Planes");
viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2, "Planes");
}
}
if (!growRegion & !bilateral & !detectPlanes)
{
viewer.removeAllPointClouds();
if (!viewer.updatePointCloud(cloud, "Triangulated points"))
{
viewer.addPointCloud<pcl::PointXYZRGB>(cloud, rgb, "Triangulated points");
viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2, "Triangulated points");
}
}
viewer.removePointCloud("normals", 0);
if (showNormals)
{
ne.setNormalEstimationMethod(ne.AVERAGE_3D_GRADIENT);
ne.setMaxDepthChangeFactor(0.02f);
ne.setNormalSmoothingSize(10.0f);
ne.setInputCloud(cloud);
ne.compute(*normals);
viewer.addPointCloudNormals<pcl::PointXYZRGB, pcl::Normal>(cloud, normals, 50, 0.05, "normals");
}
newDataAvailable = false;
viewer.spinOnce();
}
else
{
// if we ran into a timeout, just sleep for some time
std::this_thread::sleep_for(std::chrono::microseconds(10));
}
}
// stop capturing
if (cameraDevice->stopCapture() != CameraStatus::SUCCESS)
{
cerr << "Error stopping the capturing" << endl;
return 1;
}
return 0;
}