-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPEBL.cpp
1220 lines (1041 loc) · 47.9 KB
/
PEBL.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
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Plane Extraction and Object Clustering for Camera Localization
*
* PEBCL.cpp
* Written by Kevin Zhang
*/
#include <iostream>
#include <math.h>
#include <string>
#include <vector>
#include <Eigen/Core>
#include <pcl/common/distances.h>
#include <pcl/conversions.h>
#include <pcl/console/parse.h>
#include <pcl/exceptions.h>
#include <pcl/features/normal_3d.h>
#include <pcl/features/pfh.h>
#include <pcl/features/shot_omp.h>
#include <pcl/filters/filter.h>
#include <pcl/filters/extract_indices.h>
#include <pcl/filters/statistical_outlier_removal.h>
#include <pcl/filters/voxel_grid.h>
#include <pcl/filters/voxel_grid_occlusion_estimation.h>
#include <pcl/io/pcd_io.h>
#include <pcl/kdtree/kdtree.h>
#include <pcl/kdtree/kdtree_flann.h>
#include <pcl/keypoints/iss_3d.h>
#include <pcl/keypoints/sift_keypoint.h>
#include <pcl/keypoints/susan.h>
#include <pcl/ModelCoefficients.h>
#include <pcl/PCLPointCloud2.h>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <pcl/point_types_conversion.h>
#include <pcl/registration/transforms.h>
#include <pcl/registration/correspondence_estimation.h>
#include <pcl/registration/correspondence_rejection_features.h>
#include <pcl/sample_consensus/method_types.h>
#include <pcl/sample_consensus/model_types.h>
#include <pcl/segmentation/sac_segmentation.h>
#include <pcl/segmentation/extract_clusters.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl_conversions/pcl_conversions.h>
#include <pcl_ros/transforms.h>
#include "plane_segmentation.h"
#include "plane_segmentation.cpp"
#include "moment_of_inertia.h"
#include "moment_of_inertia.cpp"
std::string filename;
std::string lib_filename;
void
downsample (pcl::PointCloud<pcl::PointXYZRGB>::Ptr &points, float leaf_size,
pcl::PointCloud<pcl::PointXYZRGB>::Ptr &downsampled_out)
{
pcl::VoxelGrid<pcl::PointXYZRGB> vox_grid;
vox_grid.setLeafSize (leaf_size, leaf_size, leaf_size);
vox_grid.setInputCloud (points);
vox_grid.filter (*downsampled_out);
}
void
compute_surface_normals (pcl::PointCloud<pcl::PointXYZRGB>::Ptr &points, float normal_radius,
pcl::PointCloud<pcl::Normal>::Ptr &normals_out)
{
pcl::NormalEstimation<pcl::PointXYZRGB, pcl::Normal> norm_est;
pcl::search::KdTree<pcl::PointXYZRGB>::Ptr tree (new pcl::search::KdTree<pcl::PointXYZRGB> ());
// Use a FLANN-based KdTree to perform neighborhood searches
norm_est.setSearchMethod (tree);
// Specify the size of the local neighborhood to use when computing the surface normals
norm_est.setRadiusSearch (normal_radius);
// Set the input points
norm_est.setInputCloud (points);
// Estimate the surface normals and store the result in "normals_out"
norm_est.compute (*normals_out);
}
void
detect_keypoints (pcl::PointCloud<pcl::PointXYZRGB>::Ptr &points,
float min_scale, int nr_octaves, int nr_scales_per_octave, float min_contrast,
pcl::PointCloud<pcl::PointWithScale>::Ptr &keypoints_out)
{
pcl::SIFTKeypoint<pcl::PointXYZRGB, pcl::PointWithScale> sift_detect;
pcl::search::KdTree<pcl::PointXYZRGB>::Ptr tree (new pcl::search::KdTree<pcl::PointXYZRGB> ());
// Use a FLANN-based KdTree to perform neighborhood searches
sift_detect.setSearchMethod (tree);
// Set the detection parameters
sift_detect.setScales (min_scale, nr_octaves, nr_scales_per_octave);
sift_detect.setMinimumContrast (min_contrast);
// Set the input
sift_detect.setInputCloud (points);
// Detect the keypoints and store them in "keypoints_out"
sift_detect.compute (*keypoints_out);
}
double
computeCloudResolution (const pcl::PointCloud<pcl::PointXYZRGB>::ConstPtr &cloud)
{
double res = 0.0;
int n_points = 0;
int nres;
std::vector<int> indices (2);
std::vector<float> sqr_distances (2);
pcl::search::KdTree<pcl::PointXYZRGB> tree;
tree.setInputCloud (cloud);
for (size_t i = 0; i < cloud->size (); ++i)
{
if (! pcl_isfinite ((*cloud)[i].x))
{
continue;
}
//Considering the second neighbor since the first is the point itself.
nres = tree.nearestKSearch (i, 2, indices, sqr_distances);
if (nres == 2)
{
res += sqrt (sqr_distances[1]);
++n_points;
}
}
if (n_points != 0)
{
res /= n_points;
}
return res;
}
void
detect_ISSkeypoints (pcl::PointCloud<pcl::PointXYZRGB>::Ptr &points,
pcl::PointCloud<pcl::Normal>::Ptr &normals,
pcl::PointCloud<pcl::PointXYZRGB>::Ptr &keypoints_out)
{
double model_resolution;
model_resolution = computeCloudResolution(points);
pcl::ISSKeypoint3D<pcl::PointXYZRGB, pcl::PointXYZRGB> iss_detector;
pcl::search::KdTree<pcl::PointXYZRGB>::Ptr tree (new pcl::search::KdTree<pcl::PointXYZRGB> ());
// Use a FLANN-based KdTree to perform neighborhood searches
iss_detector.setSearchMethod (tree);
// Set the detection parameters
iss_detector.setSalientRadius (6 * model_resolution);
iss_detector.setNonMaxRadius (4 * model_resolution);
iss_detector.setThreshold21 (0.975);
iss_detector.setThreshold32 (0.975);
iss_detector.setNormals (normals);
iss_detector.setMinNeighbors (5);
// Set the input
iss_detector.setInputCloud (points);
// Detect the keypoints and store them in "keypoints_out"
iss_detector.compute (*keypoints_out);
}
void
detect_SUSANkeypoints (pcl::PointCloud<pcl::PointXYZRGB>::Ptr &points,
pcl::PointCloud<pcl::Normal>::Ptr &normals,
pcl::PointCloud<pcl::PointXYZRGB>::Ptr &keypoints_out)
{
/*pcl::PointCloud<pcl::PointXYZI>::Ptr grayPoints(new pcl::PointCloud<pcl::PointXYZI>());
pcl::PointCloudXYZRGBtoXYZI(*points, *grayPoints);*/
pcl::SUSANKeypoint<pcl::PointXYZRGB, pcl::PointXYZRGB> susan_detector;
pcl::search::KdTree<pcl::PointXYZRGB>::Ptr tree (new pcl::search::KdTree<pcl::PointXYZRGB> ());
// Use a FLANN-based KdTree to perform neighborhood searches
susan_detector.setSearchMethod (tree);
// Set the detection parameters
if (normals->points.size() == points->points.size())
{
susan_detector.setNormals (normals);
}
// Set the input
susan_detector.setInputCloud (points);
// Detect the keypoints and store them in "keypoints_out"
susan_detector.compute (*keypoints_out);
}
void visualize_keypoints (const pcl::PointCloud<pcl::PointXYZRGB>::Ptr points,
const pcl::PointCloud<pcl::PointXYZRGB>::Ptr keypoints)
{
// Add the points to the vizualizer
std::cout << "Num Keypoints = " << keypoints->size () << std::endl;
std::cout << "Num Total Points = " << points->size () << std::endl;
pcl::visualization::PCLVisualizer viz;
viz.addPointCloud (points, "points");
// Draw each keypoint as a sphere
for (size_t i = 0; i < keypoints->size (); ++i)
{
// Get the point data
pcl::PointXYZRGB & p = keypoints->points[i];
// Pick the radius of the sphere
pcl::PointXYZI k;
pcl::PointXYZRGBtoXYZI(p,k);
float r = k.intensity/100;
// * Note: the scale is given as the standard deviation of a Gaussian blur, so a
// radius of 2*p.scale is a good illustration of the extent of the keypoint
// Generate a unique string for each sphere
std::stringstream ss ("keypoint");
ss << i;
// Add a sphere at the keypoint
viz.addSphere (p, r, 1.0, 0.0, 0.0, ss.str ());
}
// Give control over to the visualizer
viz.spin ();
}
void visualize_keypointsScaled (const pcl::PointCloud<pcl::PointXYZRGB>::Ptr points,
const pcl::PointCloud<pcl::PointWithScale>::Ptr keypoints)
{
// Add the points to the vizualizer
pcl::visualization::PCLVisualizer viz;
viz.addPointCloud (points, "points");
// Draw each keypoint as a sphere
for (size_t i = 0; i < keypoints->size (); ++i)
{
// Get the point data
const pcl::PointWithScale & p = keypoints->points[i];
// Pick the radius of the sphere *
float r = 2*p.scale;
std::cout << "Scale:" << r << std::endl;
// * Note: the scale is given as the standard deviation of a Gaussian blur, so a
// radius of 2*p.scale is a good illustration of the extent of the keypoint
// Generate a unique string for each sphere
std::stringstream ss ("keypoint");
ss << i;
// Add a sphere at the keypoint
viz.addSphere (p, r, 1.0, 0.0, 0.0, ss.str ());
}
// Give control over to the visualizer
viz.spin ();
}
void
compute_PFH_features_at_keypoints (pcl::PointCloud<pcl::PointXYZRGB>::Ptr &points,
pcl::PointCloud<pcl::Normal>::Ptr &normals,
pcl::PointCloud<pcl::PointWithScale>::Ptr &keypoints, float feature_radius,
pcl::PointCloud<pcl::PFHSignature125>::Ptr &descriptors_out)
{
// Create a PFHEstimation object
pcl::PFHEstimation<pcl::PointXYZRGB, pcl::Normal, pcl::PFHSignature125> pfh_est;
pcl::search::KdTree<pcl::PointXYZRGB>::Ptr tree (new pcl::search::KdTree<pcl::PointXYZRGB> ());
// Set it to use a FLANN-based KdTree to perform its neighborhood searches
pfh_est.setSearchMethod (tree);
// Specify the radius of the PFH feature
pfh_est.setRadiusSearch (feature_radius);
/* This is a little bit messy: since our keypoint detection returns PointWithScale points, but we want to
* use them as an input to our PFH estimation, which expects clouds of PointXYZRGB points. To get around this,
* we'll use copyPointCloud to convert "keypoints" (a cloud of type PointCloud<PointWithScale>) to
* "keypoints_xyzrgb" (a cloud of type PointCloud<PointXYZRGB>). Note that the original cloud doesn't have any RGB
* values, so when we copy from PointWithScale to PointXYZRGB, the new r,g,b fields will all be zero.
*/
pcl::PointCloud<pcl::PointXYZRGB>::Ptr keypoints_xyzrgb (new pcl::PointCloud<pcl::PointXYZRGB>);
pcl::copyPointCloud (*keypoints, *keypoints_xyzrgb);
// Use all of the points for analyzing the local structure of the cloud
pfh_est.setSearchSurface (points);
pfh_est.setInputNormals (normals);
// But only compute features at the keypoints
pfh_est.setInputCloud (keypoints_xyzrgb);
// Compute the features
pfh_est.compute (*descriptors_out);
}
void
compute_PFH_features_at_ISSkeypoints (pcl::PointCloud<pcl::PointXYZRGB>::Ptr &points,
pcl::PointCloud<pcl::Normal>::Ptr &normals,
pcl::PointCloud<pcl::PointXYZRGB>::Ptr &keypoints, float feature_radius,
pcl::PointCloud<pcl::PFHSignature125>::Ptr &descriptors_out)
{
// Create a PFHEstimation object
pcl::PFHEstimation<pcl::PointXYZRGB, pcl::Normal, pcl::PFHSignature125> pfh_est;
pcl::search::KdTree<pcl::PointXYZRGB>::Ptr tree (new pcl::search::KdTree<pcl::PointXYZRGB> ());
// Set it to use a FLANN-based KdTree to perform its neighborhood searches
pfh_est.setSearchMethod (tree);
// Specify the radius of the PFH feature
pfh_est.setRadiusSearch (feature_radius);
/* This is a little bit messy: since our keypoint detection returns PointWithScale points, but we want to
* use them as an input to our PFH estimation, which expects clouds of PointXYZRGB points. To get around this,
* we'll use copyPointCloud to convert "keypoints" (a cloud of type PointCloud<PointWithScale>) to
* "keypoints_xyzrgb" (a cloud of type PointCloud<PointXYZRGB>). Note that the original cloud doesn't have any RGB
* values, so when we copy from PointWithScale to PointXYZRGB, the new r,g,b fields will all be zero.
*/
pcl::PointCloud<pcl::PointXYZRGB>::Ptr keypoints_xyzrgb (new pcl::PointCloud<pcl::PointXYZRGB>);
pcl::copyPointCloud (*keypoints, *keypoints_xyzrgb);
// Use all of the points for analyzing the local structure of the cloud
pfh_est.setSearchSurface (points);
pfh_est.setInputNormals (normals);
// But only compute features at the keypoints
pfh_est.setInputCloud (keypoints_xyzrgb);
// Compute the features
pfh_est.compute (*descriptors_out);
}
void
compute_SHOTColorFeatures_at_keypoints (pcl::PointCloud<pcl::PointXYZRGB>::Ptr &points,
pcl::PointCloud<pcl::Normal>::Ptr &normals,
pcl::PointCloud<pcl::PointXYZRGB>::Ptr &keypoints, float feature_radius,
pcl::PointCloud<pcl::SHOT1344>::Ptr &descriptors_out)
{
pcl::SHOTColorEstimationOMP<pcl::PointXYZRGB, pcl::Normal, pcl::SHOT1344> descr_est;
descr_est.setRadiusSearch (feature_radius);
descr_est.setInputCloud (keypoints);
descr_est.setInputNormals (normals);
descr_est.setSearchSurface (points);
descr_est.compute (*descriptors_out);
}
void
find_feature_correspondences (pcl::PointCloud<pcl::SHOT1344>::Ptr &source_descriptors,
pcl::PointCloud<pcl::SHOT1344>::Ptr &target_descriptors,
std::vector<int> &correspondences_out, std::vector<float> &correspondence_scores_out)
{
// Resize the output vector
correspondences_out.resize (source_descriptors->size ());
correspondence_scores_out.resize (source_descriptors->size ());
// Use a KdTree to search for the nearest matches in feature space
pcl::KdTreeFLANN<pcl::SHOT1344> descriptor_kdtree;
descriptor_kdtree.setInputCloud (target_descriptors);
// Find the index of the best match for each keypoint, and store it in "correspondences_out"
const int k = 1;
std::vector<int> k_indices (k);
std::vector<float> k_squared_distances (k);
for (size_t i = 0; i < source_descriptors->size (); ++i)
{
descriptor_kdtree.nearestKSearch (*source_descriptors, i, k, k_indices, k_squared_distances);
correspondences_out[i] = k_indices[0];
correspondence_scores_out[i] = k_squared_distances[0];
}
}
void filter_correspondences(const pcl::PointCloud<pcl::PointWithScale>::Ptr keypoints1,
const pcl::PointCloud<pcl::PointWithScale>::Ptr keypoints2,
const std::vector<int> &correspondences,
const std::vector<float> &correspondence_scores,
pcl::PointCloud<pcl::PointWithScale>::Ptr &out_keypoints1,
std::vector<int> &remaining_correspondences, std::vector<float> &remaining_correspondence_scores)
{
int numSearch = keypoints1->points.size();
float largestDistance = 0.0;
int firstPointIndex;
int secondPointIndex;
std::vector<float> temp;
for(int i = 0; i < numSearch; i++)
{
float distanceTotal = 0.0;
for(int k = 0; k < numSearch; k++)
{
distanceTotal += pcl::euclideanDistance(keypoints2->points[correspondences[i]],keypoints2->points[correspondences[k]]);
}
float avgDistance = distanceTotal/numSearch;
temp.push_back(avgDistance);
}
std::sort (temp.begin (), temp.end ());
float median_score = temp[temp.size ()/4];
/* for(int i = 0; i < numSearch; i++)
{
for(int k = i+1; k < numSearch; k++)
{
float distanceBetweenKeyPoints = pcl::euclideanDistance(keypoints1->points[i],keypoints1->points[k]);
if(distanceBetweenKeyPoints > largestDistance)
{
firstPointIndex = i;
secondPointIndex = k;
largestDistance = distanceBetweenKeyPoints;
}
}
}*/
//int j = 0;
//int leastDistance = largestDistance;
for(int i = 0; i < numSearch; i++)
{
/*float distanceTotal = 0.0;
for(int k = 0; k < numSearch; k++)
{
distanceTotal += pcl::euclideanDistance(keypoints2->points[correspondences[i]],keypoints2->points[correspondences[k]]);
}
float avgDistance = distanceTotal/numSearch;*/
if(temp[i] < median_score)
{
//leastDistance = avgDistance;
out_keypoints1->points.push_back(keypoints1->points[i]);
remaining_correspondences.push_back(correspondences[i]);
remaining_correspondence_scores.push_back(correspondence_scores[i]);
//j++;
}
}
}
void filter_ISScorrespondences(const pcl::PointCloud<pcl::PointXYZRGB>::Ptr keypoints1,
const pcl::PointCloud<pcl::PointXYZRGB>::Ptr keypoints2,
const std::vector<int> &correspondences,
const std::vector<float> &correspondence_scores,
pcl::PointCloud<pcl::PointXYZRGB>::Ptr &out_keypoints1,
std::vector<int> &remaining_correspondences, std::vector<float> &remaining_correspondence_scores)
{
int numSearch = keypoints1->points.size();
float largestDistance = 0.0;
int firstPointIndex;
int secondPointIndex;
std::vector<float> scores (correspondence_scores);
std::sort (scores.begin (), scores.end ());
float median_score = scores[scores.size ()/2];
std::vector<float> temp;
for(int i = 0; i < numSearch; i++)
{
float distanceTotal = 0.0;
for(int k = 0; k < numSearch; k++)
{
distanceTotal += pcl::euclideanDistance(keypoints2->points[correspondences[i]],keypoints2->points[correspondences[k]]);
}
float avgDistance = distanceTotal/numSearch;
temp.push_back(avgDistance);
}
std::sort (temp.begin (), temp.end ());
float median_distance = temp[temp.size ()/4];
/* for(int i = 0; i < numSearch; i++)
{
for(int k = i+1; k < numSearch; k++)
{
float distanceBetweenKeyPoints = pcl::euclideanDistance(keypoints1->points[i],keypoints1->points[k]);
if(distanceBetweenKeyPoints > largestDistance)
{
firstPointIndex = i;
secondPointIndex = k;
largestDistance = distanceBetweenKeyPoints;
}
}
}*/
//int j = 0;
//int leastDistance = largestDistance;
for(int i = 0; i < numSearch; i++)
{
/*float distanceTotal = 0.0;
for(int k = 0; k < numSearch; k++)
{
distanceTotal += pcl::euclideanDistance(keypoints2->points[correspondences[i]],keypoints2->points[correspondences[k]]);
}
float avgDistance = distanceTotal/numSearch;*/
if(temp[i] < median_distance && correspondence_scores[i] < median_score)
{
//leastDistance = avgDistance;
out_keypoints1->points.push_back(keypoints1->points[i]);
remaining_correspondences.push_back(correspondences[i]);
remaining_correspondence_scores.push_back(correspondence_scores[i]);
//j++;
}
}
}
void visualize_correspondences (const pcl::PointCloud<pcl::PointXYZRGB>::Ptr points1,
const pcl::PointCloud<pcl::PointWithScale>::Ptr keypoints1,
const pcl::PointCloud<pcl::PointXYZRGB>::Ptr points2,
const pcl::PointCloud<pcl::PointWithScale>::Ptr keypoints2,
const std::vector<int> &correspondences,
const std::vector<float> &correspondence_scores)
{
// We want to visualize two clouds side-by-side, so do to this, we'll make copies of the clouds and transform them
// by shifting one to the left and the other to the right. Then we'll draw lines between the corresponding points
// Create some new point clouds to hold our transformed data
pcl::PointCloud<pcl::PointXYZRGB>::Ptr points_left (new pcl::PointCloud<pcl::PointXYZRGB>);
pcl::PointCloud<pcl::PointWithScale>::Ptr keypoints_left (new pcl::PointCloud<pcl::PointWithScale>);
pcl::PointCloud<pcl::PointXYZRGB>::Ptr points_right (new pcl::PointCloud<pcl::PointXYZRGB>);
pcl::PointCloud<pcl::PointWithScale>::Ptr keypoints_right (new pcl::PointCloud<pcl::PointWithScale>);
// Shift the first clouds' points to the left
//const Eigen::Vector3f translate (0.0, 0.0, 0.3);
//const Eigen::Vector3f translate (0.4, 0.0, 0.0);
//const Eigen::Quaternionf no_rotation (0, 0, 0, 0);
//pcl::transformPointCloud (*points1, *points_left, -translate, no_rotation);
//pcl::transformPointCloud (*keypoints1, *keypoints_left, -translate, no_rotation);
// Shift the second clouds' points to the right
//pcl::transformPointCloud (*points2, *points_right, translate, no_rotation);
//pcl::transformPointCloud (*keypoints2, *keypoints_right, translate, no_rotation);
// Add the clouds to the vizualizer
pcl::visualization::PCLVisualizer viz;
viz.addPointCloud (points1, "points_left");
viz.addPointCloud (points2, "points_right");
std::cout << "No Segfault yet." << std::endl;
// Compute the median correspondence score
/*std::vector<float> temp (correspondence_scores);
std::sort (temp.begin (), temp.end ());
float median_score = temp[temp.size ()/2];*/
// Draw lines between the best corresponding points
for (size_t i = 0; i < keypoints1->size (); ++i)
{
/*if (correspondence_scores[i] > median_score)
{
continue; // Don't draw weak correspondences
}*/
// Get the pair of points
const pcl::PointWithScale & p_left = keypoints1->points[i];
const pcl::PointWithScale & p_right = keypoints2->points[correspondences[i]];
// Generate a random (bright) color
double r = (rand() % 100);
double g = (rand() % 100);
double b = (rand() % 100);
double max_channel = std::max (r, std::max (g, b));
r /= max_channel;
g /= max_channel;
b /= max_channel;
// Generate a unique string for each line
std::stringstream ss ("line");
ss << i;
// Draw the line
viz.addLine (p_left, p_right, r, g, b, ss.str ());
std::cout << "Line " << i << std::endl;
}
// Give control over to the visualizer
viz.spin ();
}
void visualize_ISScorrespondences (const pcl::PointCloud<pcl::PointXYZRGB>::Ptr points1,
const pcl::PointCloud<pcl::PointXYZRGB>::Ptr keypoints1,
const pcl::PointCloud<pcl::PointXYZRGB>::Ptr points2,
const pcl::PointCloud<pcl::PointXYZRGB>::Ptr keypoints2,
const std::vector<int> &correspondences,
const std::vector<float> &correspondence_scores)
{
// We want to visualize two clouds side-by-side, so do to this, we'll make copies of the clouds and transform them
// by shifting one to the left and the other to the right. Then we'll draw lines between the corresponding points
// Create some new point clouds to hold our transformed data
/*pcl::PointCloud<pcl::PointXYZRGB>::Ptr points_left (new pcl::PointCloud<pcl::PointXYZRGB>);
pcl::PointCloud<pcl::PointXYZRGB>::Ptr keypoints_left (new pcl::PointCloud<pcl::PointXYZRGB>);
pcl::PointCloud<pcl::PointXYZRGB>::Ptr points_right (new pcl::PointCloud<pcl::PointXYZRGB>);
pcl::PointCloud<pcl::PointXYZRGB>::Ptr keypoints_right (new pcl::PointCloud<pcl::PointXYZRGB>);*/
// Shift the first clouds' points to the left
//const Eigen::Vector3f translate (0.0, 0.0, 0.3);
//const Eigen::Vector3f translate (0.4, 0.0, 0.0);
//const Eigen::Quaternionf no_rotation (0, 0, 0, 0);
//pcl::transformPointCloud (*points1, *points_left, -translate, no_rotation);
//pcl::transformPointCloud (*keypoints1, *keypoints_left, -translate, no_rotation);
// Shift the second clouds' points to the right
//pcl::transformPointCloud (*points2, *points_right, translate, no_rotation);
//pcl::transformPointCloud (*keypoints2, *keypoints_right, translate, no_rotation);
// Add the clouds to the vizualizer
pcl::visualization::PCLVisualizer viz;
viz.addPointCloud (points1, "points_left");
viz.addPointCloud (points2, "points_right");
std::cout << "No Segfault yet." << std::endl;
// Compute the median correspondence score
std::vector<float> temp (correspondence_scores);
std::sort (temp.begin (), temp.end ());
float median_score = temp[temp.size ()/2];
// Draw lines between the best corresponding points
for (size_t i = 0; i < keypoints1->size (); ++i)
{
/*if (correspondence_scores[i] > median_score)
{
continue; // Don't draw weak correspondences
}*/
// Get the pair of points
const pcl::PointXYZRGB & p_left = keypoints1->points[i];
const pcl::PointXYZRGB & p_right = keypoints2->points[correspondences[i]];
// Generate a random (bright) color
double r = (rand() % 100);
double g = (rand() % 100);
double b = (rand() % 100);
double max_channel = std::max (r, std::max (g, b));
r /= max_channel;
g /= max_channel;
b /= max_channel;
// Generate a unique string for each line
std::stringstream ss ("line");
ss << i;
// Draw the line
viz.addLine (p_left, p_right, r, g, b, ss.str ());
}
// Give control over to the visualizer
viz.spin ();
}
int main (int argc, char** argv)
{
//Model & scene filenames
std::vector<int> filenames;
filenames = pcl::console::parse_file_extension_argument (argc, argv, ".pcd");
if (filenames.size () != 2)
{
std::cout << "Filenames missing.\n";
exit (-1);
}
filename = argv[filenames[0]];
lib_filename = argv[filenames[1]];
// Read in the cloud data
pcl::PCDReader reader;
pcl::PCDReader libreader;
pcl::PCLPointCloud2 input, libInput;
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZRGB>), model (new pcl::PointCloud<pcl::PointXYZRGB>);
reader.read (filename, input);
libreader.read (lib_filename, libInput);
pcl::fromPCLPointCloud2(input,*cloud);
pcl::fromPCLPointCloud2(libInput,*model);
std::cout << "PointCloud before filtering has: " << cloud->points.size () << " data points." << std::endl; //*
/* Filters */
// Statistical Outlier Removal
pcl::PointCloud<pcl::PointXYZRGB>::Ptr sorOutput(new pcl::PointCloud<pcl::PointXYZRGB>);
pcl::StatisticalOutlierRemoval<pcl::PointXYZRGB> sor;
sor.setInputCloud(cloud);
sor.setMeanK(10);
sor.setStddevMulThresh(0.5);
sor.filter(*sorOutput);
// Voxel Grid
// Create the filtering object: downsample the dataset using a leaf size of 1cm
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_f(new pcl::PointCloud<pcl::PointXYZRGB>);
pcl::VoxelGrid<pcl::PointXYZRGB> vg;
vg.setInputCloud (sorOutput);
vg.setLeafSize (0.01f, 0.01f, 0.01f);
vg.filter (*cloud_f);
std::cout << "PointCloud after filtering has: " << cloud_f->points.size () << " data points." << std::endl;
// Storage of outputted and filtered clouds
pcl::ModelCoefficients::Ptr modelC[100];
pcl::PointCloud<pcl::PointXYZRGB>::Ptr planeClouds[100];
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_filter (new pcl::PointCloud<pcl::PointXYZRGB>), outputCloud (new pcl::PointCloud<pcl::PointXYZRGB>), planeCloud (new pcl::PointCloud<pcl::PointXYZRGB>);
pcl::PCDWriter writer;
// Uncomment to save the filtered pointcloud
/*std::stringstream ss2;
ss2 << "filtered_cloud_cluster.pcd";
writer.write<pcl::PointXYZRGB> (ss2.str (), *cloud_f, false); */
int num_filtered_points = (int) cloud_f->points.size();
int i = 0;
//std::vector<blur> bls;
pcl::PointCloud<pcl::PointWithScale>::Ptr keypoints (new pcl::PointCloud<pcl::PointWithScale>);
/*// Compute keypoints
const float min_scale = 0.01;
const int nr_octaves = 3;
const int nr_octaves_per_scale = 3;
const float min_contrast = 10.0;
detect_keypoints (cloud_f, min_scale, nr_octaves, nr_octaves_per_scale, min_contrast, keypoints);
visualize_keypoints (cloud_f, keypoints);*/
while(cloud_f->points.size() > 0.33 * num_filtered_points)
{
try
{
planeSegmentor pS(cloud_f);
modelC[i] = pS.MC;
// Extract the planar inliers from the input cloud
pcl::ExtractIndices<pcl::PointXYZRGB> extract;
extract.setInputCloud(cloud_f);
extract.setIndices(pS.PI);
extract.setNegative (false);
//Get the points associated with the planar surface
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_plane (new pcl::PointCloud<pcl::PointXYZRGB> ());
extract.filter (*cloud_plane);
int numPointsOnPlane = cloud_plane->points.size();
for (int k = 0; k < numPointsOnPlane; ++k)
{
pcl::PointXYZRGB tempPoint(cloud_plane->points[k].r, cloud_plane->points[k].g, cloud_plane->points[k].b);
tempPoint.x = cloud_plane->points[k].x;
tempPoint.y = (cloud_plane->points[k].y)*(-1.0);
tempPoint.z = (cloud_plane->points[k].z)*(-1.0);
planeCloud->points.push_back(tempPoint);
}
// Perform Euclidean Cluster Extraction on the planar surface
pcl::search::KdTree<pcl::PointXYZRGB>::Ptr plane_t (new pcl::search::KdTree<pcl::PointXYZRGB>);
plane_t->setInputCloud (cloud_plane);
std::vector<pcl::PointIndices> cluster_indices;
pcl::EuclideanClusterExtraction<pcl::PointXYZRGB> ec;
ec.setClusterTolerance (0.02); // 2cm
ec.setMinClusterSize (100);
ec.setMaxClusterSize (cloud_plane->points.size());
ec.setSearchMethod (plane_t);
ec.setInputCloud (cloud_plane);
ec.extract (cluster_indices);
// Find the largest cluster of points in the plane and extract the remaining points.
int largestCluster = 0;
pcl::PointIndices::Ptr inliers (new pcl::PointIndices);
pcl::PointCloud<pcl::PointXYZRGB>::Ptr largestPlane(new pcl::PointCloud<pcl::PointXYZRGB>());
int numClusterIndices = cluster_indices.size();
for (int a = 0; a < numClusterIndices; ++a)
{
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_cluster (new pcl::PointCloud<pcl::PointXYZRGB>);
int numIndices = cluster_indices[a].indices.size();
for (int f = 0; f < numIndices; ++f)
{
cloud_cluster->points.push_back (cloud_plane->points[cluster_indices[a].indices[f]]); //*
}
cloud_cluster->width = cloud_cluster->points.size ();
cloud_cluster->height = 1;
cloud_cluster->is_dense = true;
if(cloud_cluster->points.size() > largestCluster)
{
*largestPlane = *cloud_cluster;
largestCluster = cloud_cluster->points.size();
*inliers = cluster_indices[a];
}
}
// Uncomment to save the largest cluster in the plane to the planeClouds array and also to a point cloud file
std::cout << "PointCloud representing the plane: " << largestPlane->points.size () << " data points." << std::endl;
planeClouds[i] = largestPlane;
std::stringstream ss;
ss << "cloud_plane_" << i << ".pcd";
writer.write<pcl::PointXYZRGB> (ss.str (), *largestPlane, false);
// Find a moment of inertia bounding box of the plane.
MOIBoundingBox MOIBB(planeClouds[i]);
pcl::PointXYZRGB center = MOIBB.position_OBB;
Eigen::VectorXf sidePlane1 = MOIBB.sidePlane1;
Eigen::VectorXf sidePlane2 = MOIBB.sidePlane2;
Eigen::VectorXf sidePlane3 = MOIBB.sidePlane3;
Eigen::VectorXf sidePlane4 = MOIBB.sidePlane4;
// Determine which side is considered the inliers of the plane by flipping each sidePlane's sign
int sidePlane1Sign, sidePlane2Sign, sidePlane3Sign, sidePlane4Sign;
if(center.x * sidePlane1(0) + center.y * sidePlane1(1) + center.z * sidePlane1(2) + sidePlane1(3) > 0)
{
sidePlane1Sign = 1;
}
else
{
sidePlane1Sign = -1;
}
if(center.x * sidePlane2(0) + center.y * sidePlane2(1) + center.z * sidePlane2(2) + sidePlane2(3) > 0)
{
sidePlane2Sign = 1;
}
else
{
sidePlane2Sign = -1;
}
if(center.x * sidePlane3(0) + center.y * sidePlane3(1) + center.z * sidePlane3(2) + sidePlane3(3) > 0)
{
sidePlane3Sign = 1;
}
else
{
sidePlane3Sign = -1;
}
if(center.x * sidePlane4(0) + center.y * sidePlane4(1) + center.z * sidePlane4(2) + sidePlane4(3) > 0)
{
sidePlane4Sign = 1;
}
else
{
sidePlane4Sign = -1;
}
float averageRed = 0;
float averageGreen = 0;
float averageBlue = 0;
int numPlanePoints = planeClouds[i]->points.size();
for(int h = 0; h < numPlanePoints; h++)
{
averageRed += planeClouds[i]->points[h].r;
averageGreen += planeClouds[i]->points[h].g;
averageBlue += planeClouds[i]->points[h].b;
}
std::cout << "avgRed =" << rint(averageRed/numPlanePoints) << " avgGreen =" << rint(averageGreen/numPlanePoints) << " avgBlue =" << rint(averageBlue/numPlanePoints) << std::endl;
/*// Flann KdTree creation
pcl::PointCloud<pcl::PointXYZRGB>::Ptr flannPC (new pcl::PointCloud<pcl::PointXYZRGB>);
*flannPC = *largestPlane;
pcl::KdTreeFLANN<pcl::PointXYZRGB> flannTree;
flannTree.setInputCloud(planeClouds[i]);
// Neighbors within radius search
std::vector<int> pointIdxRadiusSearch;
std::vector<float> pointRadiusSquaredDistance;
float largerRadius = 0.075; //Find neighbors around the center within a 7.5cm radius
float radius = 0.05; //Find neighbors around the randomly selected points within a 5cm radius
blur bl;
bl.setCenter(center);
bl.setRadius(radius);
bl.setLargeRadius(largerRadius);
if ( flannTree.radiusSearch (center, largerRadius, pointIdxRadiusSearch, pointRadiusSquaredDistance) > 0 )
{
std::vector<pcl::PointXYZRGB> blurPoints;
std::vector<float> pointDistancesFromCenter;
for (int z = 0; z < 9; z++)
{
int r = rand() % pointIdxRadiusSearch.size ();
std::vector<int> currentPointRadiusSearch;
std::vector<float> currentPointRadiusSquaredDistance;
float averageRed = 0;
float averageGreen = 0;
float averageBlue = 0;
if ( flannTree.radiusSearch (flannPC->points[ pointIdxRadiusSearch[r] ], radius, currentPointRadiusSearch, currentPointRadiusSquaredDistance) > 0 )
{
for (size_t i = 0; i < currentPointRadiusSearch.size (); ++i)
{
averageRed = (averageRed*i + flannPC->points[ currentPointRadiusSearch[i] ].r)/(i+1);
averageGreen = (averageGreen*i + flannPC->points[ currentPointRadiusSearch[i] ].g)/(i+1);
averageBlue = (averageBlue*i + flannPC->points[ currentPointRadiusSearch[i] ].b)/(i+1);
}
}
pcl::PointXYZRGB blob;
blob.x = flannPC->points[ pointIdxRadiusSearch[r] ].x;
blob.y = flannPC->points[ pointIdxRadiusSearch[r] ].y;
blob.z = flannPC->points[ pointIdxRadiusSearch[r] ].z;
blob.r = rint(averageRed);
blob.b = rint(averageBlue);
blob.g = rint(averageGreen);
std::cout << currentPointRadiusSearch.size ()
<< " neighbors within radius search at (" << blob.x
<< " " << blob.y
<< " " << blob.z
<< ") with radius=" << radius << std::endl;
blurPoints.push_back(blob);
pointDistancesFromCenter.push_back(sqrt(pointRadiusSquaredDistance[r]));
}
bl.setBlurredPoints(blurPoints);
bl.setDistancesFromCenter(pointDistancesFromCenter);
bls.push_back(bl);
}*/
/*pcl::PointCloud<pcl::PointXYZRGB>::Ptr model (new pcl::PointCloud<pcl::PointXYZRGB> ());
pcl::PointCloud<pcl::PointXYZRGB>::Ptr model_keypoints (new pcl::PointCloud<pcl::PointXYZRGB> ());
pcl::search::KdTree<pcl::PointXYZRGB>::Ptr tree (new pcl::search::KdTree<pcl::PointXYZRGB> ());
*model = *planeClouds[i];
pcl::PointCloud<pcl::PointXYZRGB>::Ptr planeCopy (new pcl::PointCloud<pcl::PointXYZRGB> ());
*planeCopy = *planeClouds[i];
double model_resolution;
int n_points = 0;
int nres;
std::vector<int> indices (2);
std::vector<float> sqr_distances (2);
pcl::search::KdTree<pcl::PointXYZRGB> kdtrees;
kdtrees.setInputCloud(planeCopy);
for(size_t i = 0; i < planeCopy->size (); ++i)
{
if (! pcl_isfinite ((*planeCopy)[i].x))
{
continue;
}
//Considering the second neighbor since the first is the point itself.
nres = kdtrees.nearestKSearch (i, 2, indices, sqr_distances);
if (nres == 2)
{
model_resolution += sqrt(sqr_distances[1]);
++n_points;
}
}
if (n_points != 0)
{
model_resolution /= n_points;
}
pcl::ISSKeyPoint3D<pcl::PointXYZRGB, pcl::PointXYZRGB> iss_detector;
iss_detector.setSearchMethod(tree);
iss_detector.setSalientRadius(6*model_resolution);
iss_detector.setNonMaxRadius(4*model_resolution);
iss_detector.setThreshold21(0.975);
iss_detector.setThreshold32(0.975);
iss_detector.setMitNeighbors (5);
iss_detector.setNumberOfThreads (4);
iss_detector.setInputCloud (model);
iss_detector.compute (*model_keypoints);*/
//Extracting the current plane from the overall pointcloud
extract.setNegative (true);
extract.filter(*cloud_filter);
*cloud_f = *cloud_filter;
int numpoints = cloud_f->points.size();
pcl::PointCloud<pcl::PointXYZRGB>::Ptr planeObjects (new pcl::PointCloud<pcl::PointXYZRGB>);
*planeObjects = *planeClouds[i];
for (int n = 0; n < numpoints; n++)
{
float x = cloud_f->points[n].x;
float y = cloud_f->points[n].y;
float z = cloud_f->points[n].z;
if((x * sidePlane1(0) + y * sidePlane1(1) +
z * sidePlane1(2) + sidePlane1(3)) * sidePlane1Sign > 0 &&
(x * sidePlane2(0) + y * sidePlane2(1) +
z * sidePlane2(2) + sidePlane2(3)) * sidePlane2Sign > 0 &&
(x * sidePlane3(0) + y * sidePlane3(1) +
z * sidePlane3(2) + sidePlane3(3)) * sidePlane3Sign > 0 &&
(x * sidePlane4(0) + y * sidePlane4(1) +
z * sidePlane4(2) + sidePlane4(3)) * sidePlane4Sign > 0 &&
modelC[i]->values[0]*x + modelC[i]->values[1]*y + modelC[i]->values[2]*z + modelC[i]->values[3] < 1)
{
outputCloud->points.push_back(cloud_f->points[n]);
planeObjects->points.push_back(cloud_f->points[n]);
}
}
// Creating the KdTree object for the search method of the extraction
pcl::search::KdTree<pcl::PointXYZRGB>::Ptr ktree (new pcl::search::KdTree<pcl::PointXYZRGB>);
ktree->setInputCloud (planeObjects);
std::vector<pcl::PointIndices> clusterIndices;
pcl::EuclideanClusterExtraction<pcl::PointXYZRGB> ece;
ece.setClusterTolerance (0.02); // 2cm
ece.setMinClusterSize (100);
ece.setMaxClusterSize (planeObjects->points.size());
ece.setSearchMethod (ktree);
ece.setInputCloud (planeObjects);
ece.extract (clusterIndices);
int j = 0;