赞
踩
ICP(Iterative Closest Point),即最近点迭代算法,是最为经典的点云配准算法。其方法在于,通过求取源点云和目标点云之间的对应点对,基于对应点对构造平移、旋转矩阵,利用所求矩阵,将源点云变换到目标点云位置,计算变换后点云与目标点云误差函数,若误差函数值大于设定阈值且小于设定迭代次数,则迭代进行上述运算直到满足给定误差要求。
ICP 算法采用最小二乘法计算目标函数,原理简单且具有较高精度,但是由于采用迭代计算,导致算法计算速度较慢,同时采用 ICP 进行配准计算时,其对待配准点云初始位置有一定要求,若所选初始位置不合理,会导致算法陷入局部最优。
接下来将从,ICP 使用出发,进一步分析源码及公式推导。
demo 示例,输入点云平移,反求转换矩阵(ICP 使用及迭代跳出规则,见注释)。
/** \brief @b IterativeClosestPoint provides a base implementation of the Iterative Closest Point algorithm. * The transformation is estimated based on Singular Value Decomposition (SVD). * * The algorithm has several termination criteria: * * <ol> * <li>Number of iterations has reached the maximum user imposed number of iterations (via \ref setMaximumIterations)</li> * <li>The epsilon (difference) between the previous transformation and the current estimated transformation is smaller than an user imposed value (via \ref setTransformationEpsilon)</li> * <li>The sum of Euclidean squared errors is smaller than a user defined threshold (via \ref setEuclideanFitnessEpsilon)</li> * </ol> * * * Usage example: * \code * IterativeClosestPoint<PointXYZ, PointXYZ> icp; * // Set the input source and target * icp.setInputCloud (cloud_source); * icp.setInputTarget (cloud_target); * * // Set the max correspondence distance to 5cm (e.g., correspondences with higher distances will be ignored) * icp.setMaxCorrespondenceDistance (0.05); * // Set the maximum number of iterations (criterion 1) * icp.setMaximumIterations (50); * // Set the transformation epsilon (criterion 2) * icp.setTransformationEpsilon (1e-8); * // Set the euclidean distance difference epsilon (criterion 3) * icp.setEuclideanFitnessEpsilon (1); * * // Perform the alignment * icp.align (cloud_source_registered); * * // Obtain the transformation that aligned cloud_source to cloud_source_registered * Eigen::Matrix4f transformation = icp.getFinalTransformation (); * \endcode * * \author Radu B. Rusu, Michael Dixon * \ingroup registration */ #include <iostream> #include <pcl/io/pcd_io.h> #include <pcl/point_types.h> #include <pcl/registration/icp.h> int main() { pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_source(new pcl::PointCloud<pcl::PointXYZ>); pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_target(new pcl::PointCloud<pcl::PointXYZ>); // fill in the cloud_source data cloud_source->width = 5; cloud_source->height = 1; cloud_source->is_dense = false; cloud_source->points.resize(cloud_source->width * cloud_source->height); for (size_t i = 0; i < cloud_source->points.size(); ++i) { cloud_source->points[i].x = 1024 * rand() / (RAND_MAX + 1.0f); cloud_source->points[i].y = 1024 * rand() / (RAND_MAX + 1.0f); cloud_source->points[i].z = 1024 * rand() / (RAND_MAX + 1.0f); } std::cout << "cloud_source has " << cloud_source->points.size() << " data points: " << std::endl; for (size_t i = 0; i < cloud_source->points.size(); ++i) { std::cout << " " << cloud_source->points[i].x << " " << cloud_source->points[i].y << " " << cloud_source->points[i].z << std::endl; } *cloud_target = *cloud_source; for (size_t i = 0; i < cloud_target->points.size(); ++i) { cloud_target->points[i].x = cloud_target->points[i].x + 0.7f; } std::cout << "cloud_target has " << cloud_target->points.size() << " data points: " << std::endl; for (size_t i = 0; i < cloud_target->points.size(); ++i) { std::cout << " " << cloud_target->points[i].x << " " << cloud_target->points[i].y << " " << cloud_target->points[i].z << std::endl; } pcl::IterativeClosestPoint<pcl::PointXYZ, pcl::PointXYZ> icp; icp.setInputSource(cloud_source); icp.setInputTarget(cloud_target); pcl::PointCloud<pcl::PointXYZ> Final; icp.align(Final); std::cout << "converged: " << icp.hasConverged() << "; score: " << icp.getFitnessScore() << std::endl; std::cout << "transformation: " << std::endl << icp.getFinalTransformation() << std::endl; // std::cout << Final << std::endl; std::cout << "transformed cloud_source has " << cloud_source->points.size() << " data points: " << std::endl; for (size_t i = 0; i < Final.points.size(); ++i) { std::cout << " " << Final.points[i].x << " " << Final.points[i].y << " " << Final.points[i].z << std::endl; } return (0); }
上述 demo 示例,点云配准在 align()
这个函数实现。align()
实现代码形参 output 为传入参数 Final,即由 cloud_source 最终转换点云。
// template <typename PointSource, typename PointTarget, typename Scalar> inline void pcl::Registration<PointSource, PointTarget, Scalar>::align (PointCloudSource &output) { align (output, Matrix4::Identity ()); } // template <typename PointSource, typename PointTarget, typename Scalar> inline void pcl::Registration<PointSource, PointTarget, Scalar>::align (PointCloudSource &output, const Matrix4& guess) { if (!initCompute ()) return; // Resize the output dataset if (output.points.size () != indices_->size ()) output.points.resize (indices_->size ()); // Copy the header output.header = input_->header; // Check if the output will be computed for all points or only a subset if (indices_->size () != input_->points.size ()) { output.width = static_cast<uint32_t> (indices_->size ()); output.height = 1; } else { output.width = static_cast<uint32_t> (input_->width); output.height = input_->height; } output.is_dense = input_->is_dense; // Copy the point data to output for (size_t i = 0; i < indices_->size (); ++i) output.points[i] = input_->points[(*indices_)[i]]; // Set the internal point representation of choice unless otherwise noted if (point_representation_ && !force_no_recompute_) tree_->setPointRepresentation (point_representation_); // Perform the actual transformation computation converged_ = false; final_transformation_ = transformation_ = previous_transformation_ = Matrix4::Identity (); // Right before we estimate the transformation, we set all the point.data[3] values to 1 to aid the rigid // transformation for (size_t i = 0; i < indices_->size (); ++i) output.points[i].data[3] = 1.0; computeTransformation (output, guess); deinitCompute (); }
上述 align() 实现,主要是 computeTransformation()
函数,此函数前的预备工作,关系不大。computeTransformation()
函数,其主体是一个 do-while 循环。这里要说三个内容:correspondence_estimation_、correspondence_rejectors_ 和 convergence_criteria_,三个变量的作用分别是查找最近点、剔除错误对应点、收敛准则。因为是 do-while 循环,因此合理推测收敛准则的作用是跳出循环。
/// template <typename PointSource, typename PointTarget, typename Scalar> void pcl::IterativeClosestPoint<PointSource, PointTarget, Scalar>::computeTransformation ( PointCloudSource &output, const Matrix4 &guess) { // Point cloud containing the correspondences of each point in <input, indices> PointCloudSourcePtr input_transformed (new PointCloudSource); nr_iterations_ = 0; converged_ = false; // Initialise final transformation to the guessed one final_transformation_ = guess; // If the guessed transformation is non identity if (guess != Matrix4::Identity ()) { input_transformed->resize (input_->size ()); // Apply guessed transformation prior to search for neighbours transformCloud (*input_, *input_transformed, guess); } else *input_transformed = *input_; transformation_ = Matrix4::Identity (); // Make blobs if necessary determineRequiredBlobData (); PCLPointCloud2::Ptr target_blob (new PCLPointCloud2); if (need_target_blob_) pcl::toPCLPointCloud2 (*target_, *target_blob); // Pass in the default target for the Correspondence Estimation/Rejection code correspondence_estimation_->setInputTarget (target_); if (correspondence_estimation_->requiresTargetNormals ()) correspondence_estimation_->setTargetNormals (target_blob); // Correspondence Rejectors need a binary blob for (size_t i = 0; i < correspondence_rejectors_.size (); ++i) { registration::CorrespondenceRejector::Ptr& rej = correspondence_rejectors_[i]; if (rej->requiresTargetPoints ()) rej->setTargetPoints (target_blob); if (rej->requiresTargetNormals () && target_has_normals_) rej->setTargetNormals (target_blob); } convergence_criteria_->setMaximumIterations (max_iterations_); convergence_criteria_->setRelativeMSE (euclidean_fitness_epsilon_); convergence_criteria_->setTranslationThreshold (transformation_epsilon_); convergence_criteria_->setRotationThreshold (1.0 - transformation_epsilon_); // Repeat until convergence do { // Get blob data if needed PCLPointCloud2::Ptr input_transformed_blob; if (need_source_blob_) { input_transformed_blob.reset (new PCLPointCloud2); toPCLPointCloud2 (*input_transformed, *input_transformed_blob); } // Save the previously estimated transformation previous_transformation_ = transformation_; // Set the source each iteration, to ensure the dirty flag is updated correspondence_estimation_->setInputSource (input_transformed); if (correspondence_estimation_->requiresSourceNormals ()) correspondence_estimation_->setSourceNormals (input_transformed_blob); // Estimate correspondences if (use_reciprocal_correspondence_) correspondence_estimation_->determineReciprocalCorrespondences (*correspondences_, corr_dist_threshold_); else correspondence_estimation_->determineCorrespondences (*correspondences_, corr_dist_threshold_); //if (correspondence_rejectors_.empty ()) CorrespondencesPtr temp_correspondences (new Correspondences (*correspondences_)); for (size_t i = 0; i < correspondence_rejectors_.size (); ++i) { registration::CorrespondenceRejector::Ptr& rej = correspondence_rejectors_[i]; PCL_DEBUG ("Applying a correspondence rejector method: %s.\n", rej->getClassName ().c_str ()); if (rej->requiresSourcePoints ()) rej->setSourcePoints (input_transformed_blob); if (rej->requiresSourceNormals () && source_has_normals_) rej->setSourceNormals (input_transformed_blob); rej->setInputCorrespondences (temp_correspondences); rej->getCorrespondences (*correspondences_); // Modify input for the next iteration if (i < correspondence_rejectors_.size () - 1) *temp_correspondences = *correspondences_; } size_t cnt = correspondences_->size (); // Check whether we have enough correspondences if (static_cast<int> (cnt) < min_number_correspondences_) { PCL_ERROR ("[pcl::%s::computeTransformation] Not enough correspondences found. Relax your threshold parameters.\n", getClassName ().c_str ()); convergence_criteria_->setConvergenceState(pcl::registration::DefaultConvergenceCriteria<Scalar>::CONVERGENCE_CRITERIA_NO_CORRESPONDENCES); converged_ = false; break; } // Estimate the transform transformation_estimation_->estimateRigidTransformation (*input_transformed, *target_, *correspondences_, transformation_); // Tranform the data transformCloud (*input_transformed, *input_transformed, transformation_); // Obtain the final transformation final_transformation_ = transformation_ * final_transformation_; ++nr_iterations_; // Update the vizualization of icp convergence //if (update_visualizer_ != 0) // update_visualizer_(output, source_indices_good, *target_, target_indices_good ); converged_ = static_cast<bool> ((*convergence_criteria_)); } while (!converged_); // Transform the input cloud using the final transformation PCL_DEBUG ("Transformation is:\n\t%5f\t%5f\t%5f\t%5f\n\t%5f\t%5f\t%5f\t%5f\n\t%5f\t%5f\t%5f\t%5f\n\t%5f\t%5f\t%5f\t%5f\n", final_transformation_ (0, 0), final_transformation_ (0, 1), final_transformation_ (0, 2), final_transformation_ (0, 3), final_transformation_ (1, 0), final_transformation_ (1, 1), final_transformation_ (1, 2), final_transformation_ (1, 3), final_transformation_ (2, 0), final_transformation_ (2, 1), final_transformation_ (2, 2), final_transformation_ (2, 3), final_transformation_ (3, 0), final_transformation_ (3, 1), final_transformation_ (3, 2), final_transformation_ (3, 3)); // Copy all the values output = *input_; // Transform the XYZ + normals transformCloud (*input_, output, final_transformation_); }
上述 computeTransformation() 实现,其中关键的第一步 determineCorrespondences()
(或 determineReciprocalCorrespondences(),互为最近邻,更严格)函数,KdTree 查找一一对应点对。
// determineCorrespondences() 函数 // D:\PCL 1.8.1\include\pcl-1.8\pcl\registration\impl\correspondence_estimation.hpp /// template <typename PointSource, typename PointTarget, typename Scalar> void pcl::registration::CorrespondenceEstimation<PointSource, PointTarget, Scalar>::determineCorrespondences ( pcl::Correspondences &correspondences, double max_distance) { if (!initCompute ()) return; double max_dist_sqr = max_distance * max_distance; correspondences.resize (indices_->size ()); std::vector<int> index (1); std::vector<float> distance (1); pcl::Correspondence corr; unsigned int nr_valid_correspondences = 0; // Check if the template types are the same. If true, avoid a copy. // Both point types MUST be registered using the POINT_CLOUD_REGISTER_POINT_STRUCT macro! if (isSamePointType<PointSource, PointTarget> ()) { // Iterate over the input set of source indices for (std::vector<int>::const_iterator idx = indices_->begin (); idx != indices_->end (); ++idx) { tree_->nearestKSearch (input_->points[*idx], 1, index, distance); if (distance[0] > max_dist_sqr) continue; corr.index_query = *idx; corr.index_match = index[0]; corr.distance = distance[0]; correspondences[nr_valid_correspondences++] = corr; } } else { PointTarget pt; // Iterate over the input set of source indices for (std::vector<int>::const_iterator idx = indices_->begin (); idx != indices_->end (); ++idx) { // Copy the source data to a target PointTarget format so we can search in the tree copyPoint (input_->points[*idx], pt); tree_->nearestKSearch (pt, 1, index, distance); if (distance[0] > max_dist_sqr) continue; corr.index_query = *idx; corr.index_match = index[0]; corr.distance = distance[0]; correspondences[nr_valid_correspondences++] = corr; } } correspondences.resize (nr_valid_correspondences); deinitCompute (); }
computeTransformation() 实现,覆盖 ICP 算法流程,主要包括 determineCorrespondences() 函数(见 3.1.1.1 节),estimateRigidTransformation()
函数及 transformCloud() 函数(见 3.1.1.3 节)。
estimateRigidTransformation()
,计算平移、旋转变换矩阵(ICP 算法核心所在,重难点)
;
查看类图可以知道,包括 SVD 奇异值分解算法,查看 transformation_estimation_svd.hpp 中的 TransformationEstimationSVD 类(含链接)的 estimateRigidTransformation 方法(见下述,源码):
// estimateRigidTransformation() 函数 // D:\PCL 1.8.1\include\pcl-1.8\pcl\registration\impl\transformation_estimation_svd.hpp #ifndef PCL_REGISTRATION_TRANSFORMATION_ESTIMATION_SVD_HPP_ #define PCL_REGISTRATION_TRANSFORMATION_ESTIMATION_SVD_HPP_ #include <pcl/common/eigen.h> /// template <typename PointSource, typename PointTarget, typename Scalar> inline void pcl::registration::TransformationEstimationSVD<PointSource, PointTarget, Scalar>::estimateRigidTransformation ( const pcl::PointCloud<PointSource> &cloud_src, const pcl::PointCloud<PointTarget> &cloud_tgt, Matrix4 &transformation_matrix) const { size_t nr_points = cloud_src.points.size (); if (cloud_tgt.points.size () != nr_points) { PCL_ERROR ("[pcl::TransformationEstimationSVD::estimateRigidTransformation] Number or points in source (%lu) differs than target (%lu)!\n", nr_points, cloud_tgt.points.size ()); return; } ConstCloudIterator<PointSource> source_it (cloud_src); ConstCloudIterator<PointTarget> target_it (cloud_tgt); estimateRigidTransformation (source_it, target_it, transformation_matrix); } /// template <typename PointSource, typename PointTarget, typename Scalar> void pcl::registration::TransformationEstimationSVD<PointSource, PointTarget, Scalar>::estimateRigidTransformation ( const pcl::PointCloud<PointSource> &cloud_src, const std::vector<int> &indices_src, const pcl::PointCloud<PointTarget> &cloud_tgt, Matrix4 &transformation_matrix) const { if (indices_src.size () != cloud_tgt.points.size ()) { PCL_ERROR ("[pcl::TransformationSVD::estimateRigidTransformation] Number or points in source (%lu) differs than target (%lu)!\n", indices_src.size (), cloud_tgt.points.size ()); return; } ConstCloudIterator<PointSource> source_it (cloud_src, indices_src); ConstCloudIterator<PointTarget> target_it (cloud_tgt); estimateRigidTransformation (source_it, target_it, transformation_matrix); } /// template <typename PointSource, typename PointTarget, typename Scalar> inline void pcl::registration::TransformationEstimationSVD<PointSource, PointTarget, Scalar>::estimateRigidTransformation ( const pcl::PointCloud<PointSource> &cloud_src, const std::vector<int> &indices_src, const pcl::PointCloud<PointTarget> &cloud_tgt, const std::vector<int> &indices_tgt, Matrix4 &transformation_matrix) const { if (indices_src.size () != indices_tgt.size ()) { PCL_ERROR ("[pcl::TransformationEstimationSVD::estimateRigidTransformation] Number or points in source (%lu) differs than target (%lu)!\n", indices_src.size (), indices_tgt.size ()); return; } ConstCloudIterator<PointSource> source_it (cloud_src, indices_src); ConstCloudIterator<PointTarget> target_it (cloud_tgt, indices_tgt); estimateRigidTransformation (source_it, target_it, transformation_matrix); } /// template <typename PointSource, typename PointTarget, typename Scalar> void pcl::registration::TransformationEstimationSVD<PointSource, PointTarget, Scalar>::estimateRigidTransformation ( const pcl::PointCloud<PointSource> &cloud_src, const pcl::PointCloud<PointTarget> &cloud_tgt, const pcl::Correspondences &correspondences, Matrix4 &transformation_matrix) const { ConstCloudIterator<PointSource> source_it (cloud_src, correspondences, true); ConstCloudIterator<PointTarget> target_it (cloud_tgt, correspondences, false); estimateRigidTransformation (source_it, target_it, transformation_matrix); } /// template <typename PointSource, typename PointTarget, typename Scalar> inline void pcl::registration::TransformationEstimationSVD<PointSource, PointTarget, Scalar>::estimateRigidTransformation ( ConstCloudIterator<PointSource>& source_it, ConstCloudIterator<PointTarget>& target_it, Matrix4 &transformation_matrix) const { // Convert to Eigen format const int npts = static_cast <int> (source_it.size ()); if (use_umeyama_) { Eigen::Matrix<Scalar, 3, Eigen::Dynamic> cloud_src (3, npts); Eigen::Matrix<Scalar, 3, Eigen::Dynamic> cloud_tgt (3, npts); for (int i = 0; i < npts; ++i) { cloud_src (0, i) = source_it->x; cloud_src (1, i) = source_it->y; cloud_src (2, i) = source_it->z; ++source_it; cloud_tgt (0, i) = target_it->x; cloud_tgt (1, i) = target_it->y; cloud_tgt (2, i) = target_it->z; ++target_it; } // Call Umeyama directly from Eigen (PCL patched version until Eigen is released) transformation_matrix = pcl::umeyama (cloud_src, cloud_tgt, false); } else { source_it.reset (); target_it.reset (); // <cloud_src,cloud_src> is the source dataset transformation_matrix.setIdentity (); Eigen::Matrix<Scalar, 4, 1> centroid_src, centroid_tgt; // Estimate the centroids of source, target compute3DCentroid (source_it, centroid_src); compute3DCentroid (target_it, centroid_tgt); source_it.reset (); target_it.reset (); // Subtract the centroids from source, target Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> cloud_src_demean, cloud_tgt_demean; demeanPointCloud (source_it, centroid_src, cloud_src_demean); demeanPointCloud (target_it, centroid_tgt, cloud_tgt_demean); getTransformationFromCorrelation (cloud_src_demean, centroid_src, cloud_tgt_demean, centroid_tgt, transformation_matrix); } } /// template <typename PointSource, typename PointTarget, typename Scalar> void pcl::registration::TransformationEstimationSVD<PointSource, PointTarget, Scalar>::getTransformationFromCorrelation ( const Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> &cloud_src_demean, const Eigen::Matrix<Scalar, 4, 1> ¢roid_src, const Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> &cloud_tgt_demean, const Eigen::Matrix<Scalar, 4, 1> ¢roid_tgt, Matrix4 &transformation_matrix) const { transformation_matrix.setIdentity (); // Assemble the correlation matrix H = source * target' Eigen::Matrix<Scalar, 3, 3> H = (cloud_src_demean * cloud_tgt_demean.transpose ()).topLeftCorner (3, 3); // Compute the Singular Value Decomposition Eigen::JacobiSVD<Eigen::Matrix<Scalar, 3, 3> > svd (H, Eigen::ComputeFullU | Eigen::ComputeFullV); Eigen::Matrix<Scalar, 3, 3> u = svd.matrixU (); Eigen::Matrix<Scalar, 3, 3> v = svd.matrixV (); // Compute R = V * U' if (u.determinant () * v.determinant () < 0) { for (int x = 0; x < 3; ++x) v (x, 2) *= -1; } Eigen::Matrix<Scalar, 3, 3> R = v * u.transpose (); // Return the correct transformation transformation_matrix.topLeftCorner (3, 3) = R; const Eigen::Matrix<Scalar, 3, 1> Rc (R * centroid_src.head (3)); transformation_matrix.block (0, 3, 3, 1) = centroid_tgt.head (3) - Rc; } //#define PCL_INSTANTIATE_TransformationEstimationSVD(T,U) template class PCL_EXPORTS pcl::registration::TransformationEstimationSVD<T,U>; #endif /* PCL_REGISTRATION_TRANSFORMATION_ESTIMATION_SVD_HPP_ */
computeTransformation() 实现,其中关键的第三步 transformCloud()
函数,do-while 中,用当前新求变换矩阵,在每一次迭代中更新不断平移、旋转的 cloud_source 点云副本;do-while 后,用最终变换矩阵,对初始 cloud_source 点云进行变换,并以引用传回变换后点云。transformCloud()
函数对是否考虑点云法向量即是否对法向量进行同步旋转。
/// template <typename PointSource, typename PointTarget, typename Scalar> void pcl::IterativeClosestPoint<PointSource, PointTarget, Scalar>::transformCloud ( const PointCloudSource &input, PointCloudSource &output, const Matrix4 &transform) { Eigen::Vector4f pt (0.0f, 0.0f, 0.0f, 1.0f), pt_t; Eigen::Matrix4f tr = transform.template cast<float> (); // XYZ is ALWAYS present due to the templatization, so we only have to check for normals if (source_has_normals_) { Eigen::Vector3f nt, nt_t; Eigen::Matrix3f rot = tr.block<3, 3> (0, 0); for (size_t i = 0; i < input.size (); ++i) { const uint8_t* data_in = reinterpret_cast<const uint8_t*> (&input[i]); uint8_t* data_out = reinterpret_cast<uint8_t*> (&output[i]); memcpy (&pt[0], data_in + x_idx_offset_, sizeof (float)); memcpy (&pt[1], data_in + y_idx_offset_, sizeof (float)); memcpy (&pt[2], data_in + z_idx_offset_, sizeof (float)); if (!pcl_isfinite (pt[0]) || !pcl_isfinite (pt[1]) || !pcl_isfinite (pt[2])) continue; pt_t = tr * pt; memcpy (data_out + x_idx_offset_, &pt_t[0], sizeof (float)); memcpy (data_out + y_idx_offset_, &pt_t[1], sizeof (float)); memcpy (data_out + z_idx_offset_, &pt_t[2], sizeof (float)); memcpy (&nt[0], data_in + nx_idx_offset_, sizeof (float)); memcpy (&nt[1], data_in + ny_idx_offset_, sizeof (float)); memcpy (&nt[2], data_in + nz_idx_offset_, sizeof (float)); if (!pcl_isfinite (nt[0]) || !pcl_isfinite (nt[1]) || !pcl_isfinite (nt[2])) continue; nt_t = rot * nt; memcpy (data_out + nx_idx_offset_, &nt_t[0], sizeof (float)); memcpy (data_out + ny_idx_offset_, &nt_t[1], sizeof (float)); memcpy (data_out + nz_idx_offset_, &nt_t[2], sizeof (float)); } } else { for (size_t i = 0; i < input.size (); ++i) { const uint8_t* data_in = reinterpret_cast<const uint8_t*> (&input[i]); uint8_t* data_out = reinterpret_cast<uint8_t*> (&output[i]); memcpy (&pt[0], data_in + x_idx_offset_, sizeof (float)); memcpy (&pt[1], data_in + y_idx_offset_, sizeof (float)); memcpy (&pt[2], data_in + z_idx_offset_, sizeof (float)); if (!pcl_isfinite (pt[0]) || !pcl_isfinite (pt[1]) || !pcl_isfinite (pt[2])) continue; pt_t = tr * pt; memcpy (data_out + x_idx_offset_, &pt_t[0], sizeof (float)); memcpy (data_out + y_idx_offset_, &pt_t[1], sizeof (float)); memcpy (data_out + z_idx_offset_, &pt_t[2], sizeof (float)); } } }
TransformationEstimationSVD 类(含链接)有五个 estimateRigidTransformation()
函数,前四个 public 版本 estimateRigidTransformation()
都是第五个 protected 版本 estimateRigidTransformation()
的封装。
它们会将各自的输入都转为 ConstCloudIterator 后,再调用第五个 estimateRigidTransformation()
。
/// template <typename PointSource, typename PointTarget, typename Scalar> inline void pcl::registration::TransformationEstimationSVD<PointSource, PointTarget, Scalar>::estimateRigidTransformation ( const pcl::PointCloud<PointSource> &cloud_src, const pcl::PointCloud<PointTarget> &cloud_tgt, Matrix4 &transformation_matrix) const { size_t nr_points = cloud_src.points.size (); // 两个点云的数量必须一致 if (cloud_tgt.points.size () != nr_points) { PCL_ERROR ("[pcl::TransformationEstimationSVD::estimateRigidTransformation] Number or points in source (%lu) differs than target (%lu)!\n", nr_points, cloud_tgt.points.size ()); return; } ConstCloudIterator<PointSource> source_it (cloud_src); ConstCloudIterator<PointTarget> target_it (cloud_tgt); estimateRigidTransformation (source_it, target_it, transformation_matrix); }
/// template <typename PointSource, typename PointTarget, typename Scalar> void pcl::registration::TransformationEstimationSVD<PointSource, PointTarget, Scalar>::estimateRigidTransformation ( const pcl::PointCloud<PointSource> &cloud_src, const std::vector<int> &indices_src, const pcl::PointCloud<PointTarget> &cloud_tgt, Matrix4 &transformation_matrix) const { // source 点云索引数量必须与 target 点云数量一致 if (indices_src.size () != cloud_tgt.points.size ()) { PCL_ERROR ("[pcl::TransformationSVD::estimateRigidTransformation] Number or points in source (%lu) differs than target (%lu)!\n", indices_src.size (), cloud_tgt.points.size ()); return; } ConstCloudIterator<PointSource> source_it (cloud_src, indices_src); ConstCloudIterator<PointTarget> target_it (cloud_tgt); estimateRigidTransformation (source_it, target_it, transformation_matrix); }
/// template <typename PointSource, typename PointTarget, typename Scalar> inline void pcl::registration::TransformationEstimationSVD<PointSource, PointTarget, Scalar>::estimateRigidTransformation ( const pcl::PointCloud<PointSource> &cloud_src, const std::vector<int> &indices_src, const pcl::PointCloud<PointTarget> &cloud_tgt, const std::vector<int> &indices_tgt, Matrix4 &transformation_matrix) const { // 两个点云的索引数量必须一致 if (indices_src.size () != indices_tgt.size ()) { PCL_ERROR ("[pcl::TransformationEstimationSVD::estimateRigidTransformation] Number or points in source (%lu) differs than target (%lu)!\n", indices_src.size (), indices_tgt.size ()); return; } ConstCloudIterator<PointSource> source_it (cloud_src, indices_src); ConstCloudIterator<PointTarget> target_it (cloud_tgt, indices_tgt); estimateRigidTransformation (source_it, target_it, transformation_matrix); }
///
template <typename PointSource, typename PointTarget, typename Scalar> void
pcl::registration::TransformationEstimationSVD<PointSource, PointTarget, Scalar>::estimateRigidTransformation (
const pcl::PointCloud<PointSource> &cloud_src,
const pcl::PointCloud<PointTarget> &cloud_tgt,
const pcl::Correspondences &correspondences,
Matrix4 &transformation_matrix) const
{
// ConstCloudIterator 构建会依据最后一个参数 (bool) 来决定要使用 Correspondence 的 index_query 或 index_match
ConstCloudIterator<PointSource> source_it (cloud_src, correspondences, true);
ConstCloudIterator<PointTarget> target_it (cloud_tgt, correspondences, false);
estimateRigidTransformation (source_it, target_it, transformation_matrix);
}
进入 estimateRigidTransformation()
,根据 use_umeyama_ 是否为 true,此处会分为两个分支。在 use_umeyama_ 为 true 的情況下,会遍历输入的点云迭代器 source_it 和 target_it 建立 Eigen::Matrix 矩阵,然后调用 pcl::umeyama 计算它们之间的转换矩阵。
/// template <typename PointSource, typename PointTarget, typename Scalar> inline void pcl::registration::TransformationEstimationSVD<PointSource, PointTarget, Scalar>::estimateRigidTransformation ( ConstCloudIterator<PointSource>& source_it, ConstCloudIterator<PointTarget>& target_it, Matrix4 &transformation_matrix) const { // Convert to Eigen format const int npts = static_cast <int> (source_it.size ()); if (use_umeyama_) { Eigen::Matrix<Scalar, 3, Eigen::Dynamic> cloud_src (3, npts); Eigen::Matrix<Scalar, 3, Eigen::Dynamic> cloud_tgt (3, npts); for (int i = 0; i < npts; ++i) { cloud_src (0, i) = source_it->x; cloud_src (1, i) = source_it->y; cloud_src (2, i) = source_it->z; ++source_it; cloud_tgt (0, i) = target_it->x; cloud_tgt (1, i) = target_it->y; cloud_tgt (2, i) = target_it->z; ++target_it; } // 第三个参数是 with_scaling, 表示可以同时估计尺度缩放程度 // Call Umeyama directly from Eigen (PCL patched version until Eigen is released) transformation_matrix = pcl::umeyama (cloud_src, cloud_tgt, false); }
在 use_umeyama_ 为 false 的情況下,首先计算 source 点云和 target 点云的重心,接着对两点云做 demean,然后将 demean 后的点云和它们原来的重心一同传入 getTransformationFromCorrelation(),计算得到 transformation_matrix。
else { source_it.reset (); target_it.reset (); // <cloud_src,cloud_src> is the source dataset transformation_matrix.setIdentity (); Eigen::Matrix<Scalar, 4, 1> centroid_src, centroid_tgt; // Estimate the centroids of source, target compute3DCentroid (source_it, centroid_src); compute3DCentroid (target_it, centroid_tgt); source_it.reset (); target_it.reset (); // Subtract the centroids from source, target Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> cloud_src_demean, cloud_tgt_demean; demeanPointCloud (source_it, centroid_src, cloud_src_demean); demeanPointCloud (target_it, centroid_tgt, cloud_tgt_demean); getTransformationFromCorrelation (cloud_src_demean, centroid_src, cloud_tgt_demean, centroid_tgt, transformation_matrix); } }
getTransformationFromCorrelation()
函数是 ICP 算法的核心所在,这部分的代码与 ICP(Iterative Closest Point)公式推导(含链接)对照参看。这个函数接受 demean(移除平均值)后的点云 cloud_src_demean 及 cloud_tgt_demean 和它们原来的重心作为参数,它会套用公式计算旋转矩阵和平移向量,并填入输出的转换矩阵 transformation_matrix 中。其中
template<typename _MatrixType, int QRPreconditioner>
class Eigen::JacobiSVD< _MatrixType, QRPreconditioner >(含链接),QRPreconditioner 为可选参数。
/// template <typename PointSource, typename PointTarget, typename Scalar> void pcl::registration::TransformationEstimationSVD<PointSource, PointTarget, Scalar>::getTransformationFromCorrelation ( const Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> &cloud_src_demean, const Eigen::Matrix<Scalar, 4, 1> ¢roid_src, const Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> &cloud_tgt_demean, const Eigen::Matrix<Scalar, 4, 1> ¢roid_tgt, Matrix4 &transformation_matrix) const { transformation_matrix.setIdentity (); // Assemble the correlation matrix H = source * target' Eigen::Matrix<Scalar, 3, 3> H = (cloud_src_demean * cloud_tgt_demean.transpose ()).topLeftCorner (3, 3); // Compute the Singular Value Decomposition Eigen::JacobiSVD<Eigen::Matrix<Scalar, 3, 3> > svd (H, Eigen::ComputeFullU | Eigen::ComputeFullV); Eigen::Matrix<Scalar, 3, 3> u = svd.matrixU (); Eigen::Matrix<Scalar, 3, 3> v = svd.matrixV (); // R 最优证明,提取关键点——正交阵可能为旋转阵或者反射阵,区分行列式 1 或 -1,见下述 // R 为正交标准阵,故旋转阵行列式应为 1,三维同适用(正交阵相乘保持正交),故判断 det(R),证明前提可用,以保证计算结果正确 // 理论支撑 1,两个矩阵乘积的行列式与矩阵行列式的乘积相等 // 理论支撑 2,矩阵的行列式矩阵转置的行列式相等 // Compute R = V * U' if (u.determinant () * v.determinant () < 0) { // H = UΣV' 中奇异值最小为 0,即共面,故改变 v 第三维而不改变 H = UΣV' 结果,但由反射转换到 R 旋转的假设 for (int x = 0; x < 3; ++x) v (x, 2) *= -1; // 理论上还有一种情况,无奇异值为 0,即非共面,SVD 方法失效,需要用其他如 RANSAC-like 方法对抗离群数据过多 } // R 最优证明,即多项式(内积非负)负值项最大化。内积转换为迹,用到 Schwarz 不等式,SVD 分解,定义最优 R,得证,无需再旋转 Eigen::Matrix<Scalar, 3, 3> R = v * u.transpose (); // T 计算,旋转后源中心点 - 目标中心点,取其负值,即源点云转换到目标点云 // Return the correct transformation transformation_matrix.topLeftCorner (3, 3) = R; const Eigen::Matrix<Scalar, 3, 1> Rc (R * centroid_src.head (3)); transformation_matrix.block (0, 3, 3, 1) = centroid_tgt.head (3) - Rc; }
对文章内容有不解,请随时留言。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。