Point Cloud Library (PCL)  1.14.0-dev
registration.h
1 #pragma once
2 
3 #include "typedefs.h"
4 
5 #include <pcl/registration/ia_ransac.h>
6 #include <pcl/registration/icp.h>
7 
8 /* Use SampleConsensusInitialAlignment to find a rough alignment from the source cloud to the target cloud by finding
9  * correspondences between two sets of local features
10  * Inputs:
11  * source_points
12  * The "source" points, i.e., the points that must be transformed to align with the target point cloud
13  * source_descriptors
14  * The local descriptors for each source point
15  * target_points
16  * The "target" points, i.e., the points to which the source point cloud will be aligned
17  * target_descriptors
18  * The local descriptors for each target point
19  * min_sample_distance
20  * The minimum distance between any two random samples
21  * max_correspondence_distance
22  * The
23  * nr_iterations
24  * The number of RANSAC iterations to perform
25  * Return: A transformation matrix that will roughly align the points in source to the points in target
26  */
27 Eigen::Matrix4f
28 computeInitialAlignment (const PointCloudPtr & source_points, const LocalDescriptorsPtr & source_descriptors,
29  const PointCloudPtr & target_points, const LocalDescriptorsPtr & target_descriptors,
30  float min_sample_distance, float max_correspondence_distance, int nr_iterations)
31 {
33  sac_ia.setMinSampleDistance (min_sample_distance);
34  sac_ia.setMaxCorrespondenceDistance (max_correspondence_distance);
35  sac_ia.setMaximumIterations (nr_iterations);
36 
37  sac_ia.setInputSource (source_points);
38  sac_ia.setSourceFeatures (source_descriptors);
39 
40  sac_ia.setInputTarget (target_points);
41  sac_ia.setTargetFeatures (target_descriptors);
42 
43  PointCloud registration_output;
44  sac_ia.align (registration_output);
45 
46  return (sac_ia.getFinalTransformation ());
47 }
48 
49 /* Use IterativeClosestPoint to find a precise alignment from the source cloud to the target cloud,
50  * starting with an initial guess
51  * Inputs:
52  * source_points
53  * The "source" points, i.e., the points that must be transformed to align with the target point cloud
54  * target_points
55  * The "target" points, i.e., the points to which the source point cloud will be aligned
56  * initial_alignment
57  * An initial estimate of the transformation matrix that aligns the source points to the target points
58  * max_correspondence_distance
59  * A threshold on the distance between any two corresponding points. Any corresponding points that are further
60  * apart than this threshold will be ignored when computing the source-to-target transformation
61  * outlier_rejection_threshold
62  * A threshold used to define outliers during RANSAC outlier rejection
63  * transformation_epsilon
64  * The smallest iterative transformation allowed before the algorithm is considered to have converged
65  * max_iterations
66  * The maximum number of ICP iterations to perform
67  * Return: A transformation matrix that will precisely align the points in source to the points in target
68  */
69 Eigen::Matrix4f
70 refineAlignment (const PointCloudPtr & source_points, const PointCloudPtr & target_points,
71  const Eigen::Matrix4f & initial_alignment, float max_correspondence_distance,
72  float outlier_rejection_threshold, float transformation_epsilon, float max_iterations)
73 {
74 
76  icp.setMaxCorrespondenceDistance (max_correspondence_distance);
77  icp.setRANSACOutlierRejectionThreshold (outlier_rejection_threshold);
78  icp.setTransformationEpsilon (transformation_epsilon);
79  icp.setMaximumIterations (max_iterations);
80 
81  PointCloudPtr source_points_transformed (new PointCloud);
82  pcl::transformPointCloud (*source_points, *source_points_transformed, initial_alignment);
83 
84  icp.setInputSource (source_points_transformed);
85  icp.setInputTarget (target_points);
86 
87  PointCloud registration_output;
88  icp.align (registration_output);
89 
90  return (icp.getFinalTransformation () * initial_alignment);
91 }
IterativeClosestPoint provides a base implementation of the Iterative Closest Point algorithm.
Definition: icp.h:98
void setInputTarget(const PointCloudTargetConstPtr &cloud) override
Provide a pointer to the input target (e.g., the point cloud that we want to align the input source t...
Definition: icp.h:232
void setInputSource(const PointCloudSourceConstPtr &cloud) override
Provide a pointer to the input source (e.g., the point cloud that we want to align to the target)
Definition: icp.h:199
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: point_cloud.h:173
virtual void setInputSource(const PointCloudSourceConstPtr &cloud)
Provide a pointer to the input source (e.g., the point cloud that we want to align to the target)
Matrix4 getFinalTransformation()
Get the final transformation matrix estimated by the registration method.
Definition: registration.h:259
void setMaximumIterations(int nr_iterations)
Set the maximum number of iterations the internal optimization should run for.
Definition: registration.h:277
virtual void setInputTarget(const PointCloudTargetConstPtr &cloud)
Provide a pointer to the input target (e.g., the point cloud that we want to align the input source t...
void setRANSACOutlierRejectionThreshold(double inlier_threshold)
Set the inlier distance threshold for the internal RANSAC outlier rejection loop.
Definition: registration.h:316
void setMaxCorrespondenceDistance(double distance_threshold)
Set the maximum distance threshold between two correspondent points in source <-> target.
Definition: registration.h:336
void align(PointCloudSource &output)
Call the registration algorithm which estimates the transformation and returns the transformed source...
void setTransformationEpsilon(double epsilon)
Set the transformation epsilon (maximum allowable translation squared difference between two consecut...
Definition: registration.h:358
SampleConsensusInitialAlignment is an implementation of the initial alignment algorithm described in ...
Definition: ia_ransac.h:54
void setMinSampleDistance(float min_sample_distance)
Set the minimum distances between samples.
Definition: ia_ransac.h:188
void setTargetFeatures(const FeatureCloudConstPtr &features)
Provide a shared pointer to the target point cloud's feature descriptors.
Definition: ia_ransac.hpp:64
void setSourceFeatures(const FeatureCloudConstPtr &features)
Provide a shared pointer to the source point cloud's feature descriptors.
Definition: ia_ransac.hpp:50
void transformPointCloud(const pcl::PointCloud< PointT > &cloud_in, pcl::PointCloud< PointT > &cloud_out, const Eigen::Matrix< Scalar, 4, 4 > &transform, bool copy_all_fields)
Apply a rigid transform defined by a 4x4 matrix.
Definition: transforms.hpp:221