目录
点云姿态估计,我觉得也可以叫一定条件下的刚性匹配,rigid objects匹配,假设模板是A点云,要在B点云中找到与A点云形状、大小都一致的点云集合;
代码实现
代码流程:
1、读入点云数据
2、对点云数据进行下采样
3、求点云数据的法向量
4、求点云数据的FPFH特征,其它特征也行
6、进行匹配
#include <Eigen/Core>
#include <pcl/point_types.h>
#include <pcl/point_cloud.h>
#include <pcl/common/time.h>
#include <pcl/console/print.h>
#include <pcl/features/normal_3d_omp.h>
#include <pcl/features/fpfh_omp.h>
#include <pcl/filters/filter.h>
#include <pcl/filters/voxel_grid.h>
#include <pcl/io/pcd_io.h>
#include <pcl/registration/icp.h>
#include <pcl/registration/sample_consensus_prerejective.h>
#include <pcl/segmentation/sac_segmentation.h>
#include <pcl/visualization/pcl_visualizer.h>
// Align a rigid object to a scene with clutter and occlusions
int
main(int argc, char** argv)
{
// Types
typedef pcl::PointNormal PointNT;
//typedef pcl::PointXYZ PointNT;
typedef pcl::PointCloud<PointNT> PointCloudT;
typedef pcl::FPFHSignature33 FeatureT;
typedef pcl::FPFHEstimationOMP<PointNT, PointNT, FeatureT> FeatureEstimationT;
typedef pcl::PointCloud<FeatureT> FeatureCloudT;
typedef pcl::visualization::PointCloudColorHandlerCustom<PointNT> ColorHandlerT;
// Point clouds
PointCloudT::Ptr object(new PointCloudT);
PointCloudT::Ptr object_aligned(new PointCloudT);
PointCloudT::Ptr scene(new PointCloudT);
FeatureCloudT::Ptr object_features(new FeatureCloudT);
FeatureCloudT::Ptr scene_features(new FeatureCloudT);
// Load object and scene
pcl::console::print_highlight("Loading point clouds...\n");
if (pcl::io::loadPCDFile<PointNT>("ism_train_wolf.pcd", *object) < 0 ||
pcl::io::loadPCDFile<PointNT>("ism_test_wolf.pcd", *scene) < 0)
{
pcl::console::print_error("Error loading object/scene file!\n");
return (1);
}
// Downsample
pcl::console::print_highlight("Downsampling...\n");
pcl::VoxelGrid<PointNT> grid;
const float leaf = 3.0f;//0.005f;
grid.setLeafSize(leaf, leaf, leaf);
grid.setInputCloud(object);
grid.filter(*object);
grid.setInputCloud(scene);
grid.filter(*scene);
// Estimate normals for scene
pcl::console::print_highlight("Estimating scene normals...\n");
pcl::NormalEstimationOMP<PointNT, PointNT> nest;
nest.setRadiusSearch(6.0);
nest.setInputCloud(scene);
nest.compute(*scene);
nest.setInputCloud(object);
nest.compute(*object);
// Estimate features
pcl::console::print_highlight("Estimating features...\n");
FeatureEstimationT fest;
fest.setRadiusSearch(6.0);
fest.setInputCloud(object);
fest.setInputNormals(object);
fest.compute(*object_features);
fest.setInputCloud(scene);
fest.setInputNormals(scene);
fest.compute(*scene_features);
// Perform alignment
pcl::console::print_highlight("Starting alignment...\n");
pcl::SampleConsensusPrerejective<PointNT, PointNT, FeatureT> align;
align.setInputSource(object);
align.setSourceFeatures(object_features);
align.setInputTarget(scene);
align.setTargetFeatures(scene_features);
align.setMaximumIterations(50000); // Number of RANSAC iterations
align.setNumberOfSamples(10);// Number of points to sample for generating/prerejecting a pose
align.setCorrespondenceRandomness(5); // Number of nearest features to use
align.setSimilarityThreshold(0.1f); // Polygonal edge length similarity threshold
align.setMaxCorrespondenceDistance(2.5f * leaf); // Inlier threshold
align.setInlierFraction(0.25f); // Required inlier fraction for accepting a pose hypothesis
{
pcl::ScopeTime t("Alignment");
align.align(*object_aligned);
}
if (align.hasConverged())
{
// Print results
printf("\n");
Eigen::Matrix4f transformation = align.getFinalTransformation();
pcl::console::print_info(" | %6.3f %6.3f %6.3f | \n", transformation(0, 0), transformation(0, 1), transformation(0, 2));
pcl::console::print_info("R = | %6.3f %6.3f %6.3f | \n", transformation(1, 0), transformation(1, 1), transformation(1, 2));
pcl::console::print_info(" | %6.3f %6.3f %6.3f | \n", transformation(2, 0), transformation(2, 1), transformation(2, 2));
pcl::console::print_info("\n");
pcl::console::print_info("t = < %0.3f, %0.3f, %0.3f >\n", transformation(0, 3), transformation(1, 3), transformation(2, 3));
pcl::console::print_info("\n");
pcl::console::print_info("Inliers: %i/%i\n", align.getInliers().size(), object->size());
// Show alignment
pcl::visualization::PCLVisualizer visu("Alignment");
visu.addPointCloud(scene, ColorHandlerT(scene, 0.0, 255.0, 0.0), "scene");
visu.addPointCloud(object_aligned, ColorHandlerT(object_aligned, 0.0, 0.0, 255.0), "object_aligned");
visu.spin();
}
else
{
pcl::console::print_error("Alignment failed!\n");
return (1);
}
return (0);
}
结果分析和使用感受
1、当B点云中,除了目标还含有其它很多点时,效果不好;
2、根据不同的点云,调整参数,主要是与点云密度相关的参数;
3、下采样不是必要的,可以根据选择来;
4、迭代次数多也不一定效果就好;
5、如果点云匹配效果很差,通过调整参数获得比较好的效果恐怕也很难;
6、可能更适合预处理之后的冗余不多的点云,例如先筛选出一个点云区域,或者经过点云矫正的区域,在这么一个区域内进行匹配,到底还是更适合姿态匹配;
7、如果B点云中的物体举例A点云中稍微有变化,不太影响效果,还是可以匹配到一部分;