-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
Copy pathpcd_select_object_plane.cpp
706 lines (632 loc) · 25.4 KB
/
pcd_select_object_plane.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
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
/*
* Software License Agreement (BSD License)
*
* Point Cloud Library (PCL) - www.pointclouds.org
* Copyright (c) 2012-, Open Perception, Inc.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of the copyright holder(s) nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* $Id: openni_viewer.cpp 5059 2012-03-14 02:12:17Z gedikli $
*
*/
#include <pcl/common/angles.h>
#include <pcl/console/parse.h>
#include <pcl/console/print.h>
#include <pcl/console/time.h>
#include <pcl/features/integral_image_normal.h>
#include <pcl/features/normal_3d.h>
#include <pcl/filters/extract_indices.h>
#include <pcl/filters/project_inliers.h>
#include <pcl/geometry/polygon_operations.h>
#include <pcl/io/pcd_io.h>
#include <pcl/sample_consensus/sac_model_plane.h> // for pointToPlaneDistance
#include <pcl/segmentation/edge_aware_plane_comparator.h>
#include <pcl/segmentation/euclidean_cluster_comparator.h>
#include <pcl/segmentation/extract_clusters.h>
#include <pcl/segmentation/extract_polygonal_prism_data.h>
#include <pcl/segmentation/organized_connected_component_segmentation.h>
#include <pcl/segmentation/organized_multi_plane_segmentation.h>
#include <pcl/segmentation/sac_segmentation.h>
#include <pcl/surface/convex_hull.h>
#include <pcl/visualization/image_viewer.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/visualization/point_cloud_handlers.h>
#include <thread>
using namespace pcl;
using namespace pcl::console;
using namespace std::chrono_literals;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
template <typename PointT>
class ObjectSelection {
public:
ObjectSelection()
: plane_comparator_(new EdgeAwarePlaneComparator<PointT, Normal>), rgb_data_()
{
// Set the parameters for planar segmentation
plane_comparator_->setDistanceThreshold(0.01f, false);
}
/////////////////////////////////////////////////////////////////////////
virtual ~ObjectSelection() { delete[] rgb_data_; }
/////////////////////////////////////////////////////////////////////////
void
estimateNormals(const typename PointCloud<PointT>::ConstPtr& input,
PointCloud<Normal>& normals)
{
if (input->isOrganized()) {
IntegralImageNormalEstimation<PointT, Normal> ne;
// Set the parameters for normal estimation
ne.setNormalEstimationMethod(ne.COVARIANCE_MATRIX);
ne.setMaxDepthChangeFactor(0.02f);
ne.setNormalSmoothingSize(20.0f);
// Estimate normals in the cloud
ne.setInputCloud(input);
ne.compute(normals);
// Save the distance map for the plane comparator
float* map = ne.getDistanceMap(); // This will be deallocated with the
// IntegralImageNormalEstimation object...
distance_map_.assign(map, map + input->size()); //...so we must copy the data out
plane_comparator_->setDistanceMap(distance_map_.data());
}
else {
NormalEstimation<PointT, Normal> ne;
ne.setInputCloud(input);
ne.setRadiusSearch(0.02f);
ne.setSearchMethod(search_);
ne.compute(normals);
}
}
/////////////////////////////////////////////////////////////////////////
void
keyboard_callback(const visualization::KeyboardEvent&, void*)
{
// if (event.getKeyCode())
// std::cout << "the key \'" << event.getKeyCode() << "\' (" << event.getKeyCode()
// << ") was";
// else
// std::cout << "the special key \'" << event.getKeySym() << "\' was";
// if (event.keyDown())
// std::cout << " pressed" << std::endl;
// else
// std::cout << " released" << std::endl;
}
/////////////////////////////////////////////////////////////////////////
void
mouse_callback(const visualization::MouseEvent& mouse_event, void*)
{
if (mouse_event.getType() == visualization::MouseEvent::MouseButtonPress &&
mouse_event.getButton() == visualization::MouseEvent::LeftButton) {
std::cout << "left button pressed @ " << mouse_event.getX() << " , "
<< mouse_event.getY() << std::endl;
}
}
/////////////////////////////////////////////////////////////////////////
/** \brief
* Given a plane, and the set of inlier indices representing it,
* segment out the object of intererest supported by it.
*
* \param[in] picked_idx the index of a point on the object
* \param[in] cloud the full point cloud dataset
* \param[in] plane_indices a set of indices representing the plane supporting the
* object of interest
* \param[out] object the segmented resultant object
* \param[out] object the segmented resultant object
*/
void
segmentObject(pcl::index_t picked_idx,
const typename PointCloud<PointT>::ConstPtr& cloud,
const PointIndices::Ptr& plane_indices,
PointCloud<PointT>& object)
{
typename PointCloud<PointT>::Ptr plane_hull(new PointCloud<PointT>);
// Compute the convex hull of the plane
ConvexHull<PointT> chull;
chull.setDimension(2);
chull.setInputCloud(cloud);
chull.setIndices(plane_indices);
chull.reconstruct(*plane_hull);
// Remove the plane indices from the data
typename PointCloud<PointT>::Ptr plane(new PointCloud<PointT>);
ExtractIndices<PointT> extract(true);
extract.setInputCloud(cloud);
extract.setIndices(plane_indices);
extract.setNegative(false);
extract.filter(*plane);
PointIndices::Ptr indices_but_the_plane(new PointIndices);
extract.getRemovedIndices(*indices_but_the_plane);
// Extract all clusters above the hull
PointIndices::Ptr points_above_plane(new PointIndices);
ExtractPolygonalPrismData<PointT> exppd;
exppd.setInputCloud(cloud);
exppd.setIndices(indices_but_the_plane);
exppd.setInputPlanarHull(plane_hull);
exppd.setViewPoint(
(*cloud)[picked_idx].x, (*cloud)[picked_idx].y, (*cloud)[picked_idx].z);
exppd.setHeightLimits(0.001, 0.5); // up to half a meter
exppd.segment(*points_above_plane);
std::vector<PointIndices> euclidean_label_indices;
// Prefer a faster method if the cloud is organized, over EuclidanClusterExtraction
if (cloud_->isOrganized()) {
// Use an organized clustering segmentation to extract the individual clusters
typename EuclideanClusterComparator<PointT, Label>::Ptr
euclidean_cluster_comparator(new EuclideanClusterComparator<PointT, Label>);
euclidean_cluster_comparator->setInputCloud(cloud);
euclidean_cluster_comparator->setDistanceThreshold(0.03f, false);
// Set the entire scene to false, and the inliers of the objects located on top of
// the plane to true
Label l;
l.label = 0;
PointCloud<Label>::Ptr scene(
new PointCloud<Label>(cloud->width, cloud->height, l));
// Mask the objects that we want to split into clusters
for (const auto& index : points_above_plane->indices)
(*scene)[index].label = 1;
euclidean_cluster_comparator->setLabels(scene);
typename EuclideanClusterComparator<PointT, Label>::ExcludeLabelSetPtr
exclude_labels(
new typename EuclideanClusterComparator<PointT, Label>::ExcludeLabelSet);
exclude_labels->insert(0);
euclidean_cluster_comparator->setExcludeLabels(exclude_labels);
OrganizedConnectedComponentSegmentation<PointT, Label> euclidean_segmentation(
euclidean_cluster_comparator);
euclidean_segmentation.setInputCloud(cloud);
PointCloud<Label> euclidean_labels;
euclidean_segmentation.segment(euclidean_labels, euclidean_label_indices);
}
else {
print_highlight(
stderr,
"Extracting individual clusters from the points above the reference plane ");
TicToc tt;
tt.tic();
EuclideanClusterExtraction<PointT> ec;
ec.setClusterTolerance(0.02); // 2cm
ec.setMinClusterSize(100);
ec.setSearchMethod(search_);
ec.setInputCloud(cloud);
ec.setIndices(points_above_plane);
ec.extract(euclidean_label_indices);
print_info("[done, ");
print_value("%g", tt.toc());
print_info(" ms : ");
print_value("%lu", euclidean_label_indices.size());
print_info(" clusters]\n");
}
// For each cluster found
bool cluster_found = false;
for (const auto& euclidean_label_index : euclidean_label_indices) {
if (cluster_found)
break;
// Check if the point that we picked belongs to it
for (std::size_t j = 0; j < euclidean_label_index.indices.size(); ++j) {
if (picked_idx != euclidean_label_index.indices[j])
continue;
copyPointCloud(*cloud, euclidean_label_index.indices, object);
cluster_found = true;
break;
}
}
}
/////////////////////////////////////////////////////////////////////////
void
segment(const PointT& picked_point,
pcl::index_t picked_idx,
PlanarRegion<PointT>& region,
typename PointCloud<PointT>::Ptr& object)
{
object.reset();
// Segment out all planes
std::vector<ModelCoefficients> model_coefficients;
std::vector<PointIndices> inlier_indices, boundary_indices;
std::vector<PlanarRegion<PointT>, Eigen::aligned_allocator<PlanarRegion<PointT>>>
regions;
// Prefer a faster method if the cloud is organized, over RANSAC
if (cloud_->isOrganized()) {
print_highlight(stderr, "Estimating normals ");
TicToc tt;
tt.tic();
// Estimate normals
PointCloud<Normal>::Ptr normal_cloud(new PointCloud<Normal>);
estimateNormals(cloud_, *normal_cloud);
print_info("[done, ");
print_value("%g", tt.toc());
print_info(" ms : ");
print_value("%lu", normal_cloud->size());
print_info(" points]\n");
OrganizedMultiPlaneSegmentation<PointT, Normal, Label> mps;
mps.setMinInliers(1000);
mps.setAngularThreshold(deg2rad(3.0)); // 3 degrees
mps.setDistanceThreshold(0.03); // 2 cm
mps.setMaximumCurvature(0.001); // a small curvature
mps.setProjectPoints(true);
mps.setComparator(plane_comparator_);
mps.setInputNormals(normal_cloud);
mps.setInputCloud(cloud_);
// Use one of the overloaded segmentAndRefine calls to get all the information
// that we want out
PointCloud<Label>::Ptr labels(new PointCloud<Label>);
std::vector<PointIndices> label_indices;
mps.segmentAndRefine(regions,
model_coefficients,
inlier_indices,
labels,
label_indices,
boundary_indices);
}
else {
SACSegmentation<PointT> seg;
seg.setOptimizeCoefficients(true);
seg.setModelType(SACMODEL_PLANE);
seg.setMethodType(SAC_RANSAC);
seg.setMaxIterations(10000);
seg.setDistanceThreshold(0.005);
// Copy XYZ and Normals to a new cloud
typename PointCloud<PointT>::Ptr cloud_segmented(new PointCloud<PointT>(*cloud_));
typename PointCloud<PointT>::Ptr cloud_remaining(new PointCloud<PointT>);
ModelCoefficients coefficients;
ExtractIndices<PointT> extract;
PointIndices::Ptr inliers(new PointIndices());
// Up until 30% of the original cloud is left
int i = 1;
while (double(cloud_segmented->size()) > 0.3 * double(cloud_->size())) {
seg.setInputCloud(cloud_segmented);
print_highlight(stderr, "Searching for the largest plane (%2.0d) ", i++);
TicToc tt;
tt.tic();
seg.segment(*inliers, coefficients);
print_info("[done, ");
print_value("%g", tt.toc());
print_info(" ms : ");
print_value("%lu", inliers->indices.size());
print_info(" points]\n");
// No datasets could be found anymore
if (inliers->indices.empty())
break;
// Save this plane
PlanarRegion<PointT> region;
region.setCoefficients(coefficients);
regions.push_back(region);
inlier_indices.push_back(*inliers);
model_coefficients.push_back(coefficients);
// Extract the outliers
extract.setInputCloud(cloud_segmented);
extract.setIndices(inliers);
extract.setNegative(true);
extract.filter(*cloud_remaining);
cloud_segmented.swap(cloud_remaining);
}
}
print_highlight(
"Number of planar regions detected: %lu for a cloud of %lu points\n",
regions.size(),
cloud_->size());
double max_dist = std::numeric_limits<double>::max();
// Compute the distances from all the planar regions to the picked point, and select
// the closest region
int idx = -1;
for (std::size_t i = 0; i < regions.size(); ++i) {
double dist = pointToPlaneDistance(picked_point, regions[i].getCoefficients());
if (dist < max_dist) {
max_dist = dist;
idx = static_cast<int>(i);
}
}
// Get the plane that holds the object of interest
if (idx != -1) {
plane_indices_.reset(new PointIndices(inlier_indices[idx]));
if (cloud_->isOrganized()) {
approximatePolygon(regions[idx], region, 0.01f, false, true);
print_highlight(
"Planar region: %lu points initial, %lu points after refinement.\n",
regions[idx].getContour().size(),
region.getContour().size());
}
else {
// Save the current region
region = regions[idx];
print_highlight(stderr, "Obtaining the boundary points for the region ");
TicToc tt;
tt.tic();
// Project the inliers to obtain a better hull
typename PointCloud<PointT>::Ptr cloud_projected(new PointCloud<PointT>);
ModelCoefficients::Ptr coefficients(
new ModelCoefficients(model_coefficients[idx]));
ProjectInliers<PointT> proj;
proj.setModelType(SACMODEL_PLANE);
proj.setInputCloud(cloud_);
proj.setIndices(plane_indices_);
proj.setModelCoefficients(coefficients);
proj.filter(*cloud_projected);
// Compute the boundary points as a ConvexHull
ConvexHull<PointT> chull;
chull.setDimension(2);
chull.setInputCloud(cloud_projected);
PointCloud<PointT> plane_hull;
chull.reconstruct(plane_hull);
region.setContour(plane_hull);
print_info("[done, ");
print_value("%g", tt.toc());
print_info(" ms : ");
print_value("%lu", plane_hull.size());
print_info(" points]\n");
}
}
// Segment the object of interest
if (plane_indices_ && !plane_indices_->indices.empty()) {
plane_.reset(new PointCloud<PointT>);
copyPointCloud(*cloud_, plane_indices_->indices, *plane_);
object.reset(new PointCloud<PointT>);
segmentObject(picked_idx, cloud_, plane_indices_, *object);
}
}
/////////////////////////////////////////////////////////////////////////
/**
* \brief Point picking callback. This gets called when the user selects
* a 3D point on screen (in the PCLVisualizer window) using Shift+click.
*
* \param[in] event the event that triggered the call
*/
void
pp_callback(const visualization::PointPickingEvent& event, void*)
{
// Check to see if we got a valid point. Early exit.
int idx = event.getPointIndex();
if (idx == -1)
return;
pcl::Indices indices(1);
std::vector<float> distances(1);
// Get the point that was picked
PointT picked_pt;
event.getPoint(picked_pt.x, picked_pt.y, picked_pt.z);
print_info(stderr,
"Picked point with index %d, and coordinates %f, %f, %f.\n",
idx,
picked_pt.x,
picked_pt.y,
picked_pt.z);
// Add a sphere to it in the PCLVisualizer window
const std::string sphere_name = "sphere_" + std::to_string(idx);
cloud_viewer_->addSphere(picked_pt, 0.01, 1.0, 0.0, 0.0, sphere_name);
// Because VTK/OpenGL stores data without NaN, we lose the 1-1 correspondence, so we
// must search for the real point
search_->nearestKSearch(picked_pt, 1, indices, distances);
// Add some marker to the image
if (image_viewer_) {
// Get the [u, v] in pixel coordinates for the ImageViewer. Remember that 0,0 is
// bottom left.
std::uint32_t width = search_->getInputCloud()->width,
height = search_->getInputCloud()->height;
int v = height - indices[0] / width, u = indices[0] % width;
image_viewer_->addCircle(u, v, 5, 1.0, 0.0, 0.0, "circles", 1.0);
image_viewer_->addFilledRectangle(
u - 5, u + 5, v - 5, v + 5, 0.0, 1.0, 0.0, "boxes", 0.5);
image_viewer_->markPoint(
u, v, visualization::red_color, visualization::blue_color, 10);
}
// Segment the region that we're interested in, by employing a two step process:
// * first, segment all the planes in the scene, and find the one closest to our
// picked point
// * then, use euclidean clustering to find the object that we clicked on and
// return it
PlanarRegion<PointT> region;
segment(picked_pt, indices[0], region, object_);
// If no region could be determined, exit
if (region.getContour().empty()) {
PCL_ERROR("No planar region detected. Please select another point or relax the "
"thresholds and continue.\n");
return;
}
// Else, draw it on screen
cloud_viewer_->addPolygon(region, 0.0, 0.0, 1.0, "region");
cloud_viewer_->setShapeRenderingProperties(
visualization::PCL_VISUALIZER_LINE_WIDTH, 10, "region");
// Draw in image space
if (image_viewer_) {
image_viewer_->addPlanarPolygon(
search_->getInputCloud(), region, 0.0, 0.0, 1.0, "refined_region", 1.0);
}
// If no object could be determined, exit
if (!object_) {
PCL_ERROR("No object detected. Please select another point or relax the "
"thresholds and continue.\n");
return;
}
// Visualize the object in 3D...
visualization::PointCloudColorHandlerCustom<PointT> red(object_, 255, 0, 0);
if (!cloud_viewer_->updatePointCloud(object_, red, "object"))
cloud_viewer_->addPointCloud(object_, red, "object");
// ...and 2D
if (image_viewer_) {
image_viewer_->removeLayer("object");
image_viewer_->addMask(search_->getInputCloud(), *object_, "object");
}
// ...and 2D
if (image_viewer_)
image_viewer_->addRectangle(search_->getInputCloud(), *object_);
}
/////////////////////////////////////////////////////////////////////////
void
compute()
{
// Visualize the data
while (!cloud_viewer_->wasStopped()) {
/*// Add the plane that we're tracking to the cloud visualizer
PointCloud<PointT>::Ptr plane (new Cloud);
if (plane_)
*plane = *plane_;
visualization::PointCloudColorHandlerCustom<PointT> blue (plane, 0, 255, 0);
if (!cloud_viewer_->updatePointCloud (plane, blue, "plane"))
cloud_viewer_->addPointCloud (plane, "plane");
*/
cloud_viewer_->spinOnce();
if (image_viewer_) {
image_viewer_->spinOnce();
if (image_viewer_->wasStopped())
break;
}
std::this_thread::sleep_for(100us);
}
}
/////////////////////////////////////////////////////////////////////////
void
initGUI()
{
cloud_viewer_.reset(new visualization::PCLVisualizer("PointCloud"));
if (cloud_->isOrganized()) {
// If the dataset is organized, and has RGB data, create an image viewer
std::vector<pcl::PCLPointField> fields;
int rgba_index = -1;
rgba_index = getFieldIndex<PointT>("rgba", fields);
if (rgba_index >= 0) {
image_viewer_.reset(new visualization::ImageViewer("RGB PCLImage"));
image_viewer_->registerMouseCallback(&ObjectSelection::mouse_callback, *this);
image_viewer_->registerKeyboardCallback(&ObjectSelection::keyboard_callback,
*this);
image_viewer_->setPosition(cloud_->width, 0);
image_viewer_->setSize(cloud_->width, cloud_->height);
int poff = fields[rgba_index].offset;
// BGR to RGB
rgb_data_ = new unsigned char[cloud_->width * cloud_->height * 3];
for (std::uint32_t i = 0; i < cloud_->width * cloud_->height; ++i) {
RGB rgb;
memcpy(&rgb,
reinterpret_cast<unsigned char*>(&(*cloud_)[i]) + poff,
sizeof(rgb));
rgb_data_[i * 3 + 0] = rgb.r;
rgb_data_[i * 3 + 1] = rgb.g;
rgb_data_[i * 3 + 2] = rgb.b;
}
image_viewer_->showRGBImage(rgb_data_, cloud_->width, cloud_->height);
}
cloud_viewer_->setSize(cloud_->width, cloud_->height);
}
cloud_viewer_->registerMouseCallback(&ObjectSelection::mouse_callback, *this);
cloud_viewer_->registerKeyboardCallback(&ObjectSelection::keyboard_callback, *this);
cloud_viewer_->registerPointPickingCallback(&ObjectSelection::pp_callback, *this);
cloud_viewer_->setPosition(0, 0);
cloud_viewer_->addPointCloud(cloud_, "scene");
cloud_viewer_->resetCameraViewpoint("scene");
cloud_viewer_->addCoordinateSystem(0.1, 0, 0, 0, "global");
}
/////////////////////////////////////////////////////////////////////////
bool
load(const std::string& file)
{
// Load the input file
TicToc tt;
tt.tic();
print_highlight(stderr, "Loading ");
print_value(stderr, "%s ", file.c_str());
cloud_.reset(new PointCloud<PointT>);
if (io::loadPCDFile(file, *cloud_) < 0) {
print_error(stderr, "[error]\n");
return false;
}
print_info("[done, ");
print_value("%g", tt.toc());
print_info(" ms : ");
print_value("%lu", cloud_->size());
print_info(" points]\n");
if (cloud_->isOrganized())
search_.reset(new search::OrganizedNeighbor<PointT>);
else
search_.reset(new search::KdTree<PointT>);
search_->setInputCloud(cloud_);
return true;
}
/////////////////////////////////////////////////////////////////////////
void
save(const std::string& object_file, const std::string& plane_file)
{
PCDWriter w;
if (object_ && !object_->empty()) {
w.writeBinaryCompressed(object_file, *object_);
w.writeBinaryCompressed(plane_file, *plane_);
print_highlight("Object successfully segmented. Saving results in: %s, and %s.\n",
object_file.c_str(),
plane_file.c_str());
}
}
visualization::PCLVisualizer::Ptr cloud_viewer_;
visualization::ImageViewer::Ptr image_viewer_;
typename PointCloud<PointT>::Ptr cloud_;
typename search::Search<PointT>::Ptr search_;
private:
// Segmentation
typename EdgeAwarePlaneComparator<PointT, Normal>::Ptr plane_comparator_;
PointIndices::Ptr plane_indices_;
unsigned char* rgb_data_;
std::vector<float> distance_map_;
// Results
typename PointCloud<PointT>::Ptr plane_;
typename PointCloud<PointT>::Ptr object_;
};
/* ---[ */
int
main(int argc, char** argv)
{
// Parse the command line arguments for .pcd files
std::vector<int> p_file_indices;
p_file_indices = parse_file_extension_argument(argc, argv, ".pcd");
if (p_file_indices.empty()) {
print_error(" Need at least an input PCD file (e.g. scene.pcd) to continue!\n\n");
print_info("Ideally, need an input file, and three output PCD files, e.g., "
"object.pcd, plane.pcd, rest.pcd\n");
return -1;
}
std::string object_file = "object.pcd";
std::string plane_file = "plane.pcd";
if (p_file_indices.size() >= 3)
plane_file = argv[p_file_indices[2]];
if (p_file_indices.size() >= 2)
object_file = argv[p_file_indices[1]];
PCDReader reader;
// Test the header
pcl::PCLPointCloud2 dummy;
reader.readHeader(argv[p_file_indices[0]], dummy);
if (dummy.height != 1 && getFieldIndex(dummy, "rgba") != -1) {
print_highlight("Enabling 2D image viewer mode.\n");
ObjectSelection<PointXYZRGBA> s;
if (!s.load(argv[p_file_indices[0]]))
return -1;
s.initGUI();
s.compute();
s.save(object_file, plane_file);
}
else {
ObjectSelection<PointXYZ> s;
if (!s.load(argv[p_file_indices[0]]))
return -1;
s.initGUI();
s.compute();
s.save(object_file, plane_file);
}
return 0;
}
/* ]--- */