1.背景
学习PCL的过程中,书中的源码,加注了详细的注释,在笔记本上跑出了结果,但是有些问题,知识盲区还是太多,只知其一不知其二。先贴在这里,后续需要慢慢多次消化咀嚼。
2.代码
#include <pcl/io/pcd_io.h>
#include <pcl/point_cloud.h> //点云类型头文件
#include <pcl/correspondence.h> //对应两个实体之间的匹配(例如点,描述符等)
#include <pcl/features/normal_3d_omp.h> //法线投建
#include <pcl/features/shot_omp.h> //描述子
#include <pcl/features/board.h>
#include <pcl/keypoints/uniform_sampling.h>//均匀采样
#include <pcl/recognition/cg/hough_3d.h>//霍夫算子
#include <pcl/recognition/cg/geometric_consistency.h>//几何一致性
#include <pcl/visualization/pcl_visualizer.h>//可视化
#include <pcl/kdtree/kdtree_flann.h>//配准方法
#include <pcl/kdtree/impl/kdtree_flann.hpp>
#include <pcl/common/transforms.h>//转换矩阵
#include <pcl/console/parse.h>
//包含头文件
typedef pcl::PointXYZRGBA PointType;//PointRGBA的数据结构
typedef pcl::Normal NormalType;//法线结构
typedef pcl::ReferenceFrame RFType;//参考帧
typedef pcl::SHOT352 DescriptorType;//SHOT特征的数据结构
std::string model_filename_;//此处定义全局变量模型名称与场景名称
std::string scene_filename_;
//Algorithm params
//算法参数,目前并不清楚有何作用
bool show_keypoints_ (false);
bool show_correspondences_ (false);
bool use_cloud_resolution_ (false);
bool use_hough_ (true);
float model_ss_ (0.01f);
float scene_ss_ (0.03f);
float rf_rad_ (0.015f);
float descr_rad_ (0.02f);
float cg_size_ (0.01f);
float cg_thresh_ (5.0f);
void
showHelp (char *filename)
{
std::cout << std::endl;
std::cout << "***************************************************************************" << std::endl;
std::cout << "* *" << std::endl;
std::cout << "* Correspondence Grouping Tutorial - Usage Guide *" << std::endl;
std::cout << "* *" << std::endl;
std::cout << "***************************************************************************" << std::endl << std::endl;
std::cout << "Usage: " << filename << " model_filename.pcd scene_filename.pcd [Options]" << std::endl << std::endl;
std::cout << "Options:" << std::endl;
std::cout << " -h: Show this help." << std::endl;
std::cout << " -k: Show used keypoints." << std::endl;
std::cout << " -c: Show used correspondences." << std::endl;
std::cout << " -r: Compute the model cloud resolution and multiply" << std::endl;
std::cout << " each radius given by that value." << std::endl;
std::cout << " --algorithm (Hough|GC): Clustering algorithm used (default Hough)." << std::endl;
std::cout << " --model_ss val: Model uniform sampling radius (default 0.01)" << std::endl;
std::cout << " --scene_ss val: Scene uniform sampling radius (default 0.03)" << std::endl;
std::cout << " --rf_rad val: Reference frame radius (default 0.015)" << std::endl;
std::cout << " --descr_rad val: Descriptor radius (default 0.02)" << std::endl;
std::cout << " --cg_size val: Cluster size (default 0.01)" << std::endl;
std::cout << " --cg_thresh val: Clustering threshold (default 5)" << std::endl << std::endl;
}
//定义命令行解析函数,对键盘接受的参数进行解析,首先判断是否为正确参数,其次对参数命令进行解析、
//执行相应的操作
void
parseCommandLine (int argc, char *argv[])
{
//Show help
//首先解析命令行参数,如果命令行接收到-h字符,显示提示信息,并返回,否则继续
if (pcl::console::find_switch (argc, argv, "-h"))
{
showHelp (argv[0]);
exit (0);
}
//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";//如果参数错误,重新显示提示信息并返回
showHelp (argv[0]);
exit (-1);
}
//参数设置正确,存储参数
//第一个接受的参数为模型名称
//第二个接受的参数为场景名称
model_filename_ = argv[filenames[0]];
scene_filename_ = argv[filenames[1]];
//对参数进行匹配
//Program behavior
if (pcl::console::find_switch (argc, argv, "-k"))
{
show_keypoints_ = true;
}
if (pcl::console::find_switch (argc, argv, "-c"))
{
show_correspondences_ = true;
}
if (pcl::console::find_switch (argc, argv, "-r"))
{
use_cloud_resolution_ = true;
}
//定义字符串类型变量used_algorithm,用来保存命令解析结果
std::string used_algorithm;
//pcl::console::parse_argument (argc, argv, "--algorithm", used_algorithm)
//函数含义为在有argc个指令的参数序列argv[]中匹配--algotithm,将匹配结果保存到used_glgorithm
//如果未匹配到返回结果为-1
if (pcl::console::parse_argument (argc, argv, "--algorithm", used_algorithm) != -1)
{
if (used_algorithm.compare ("Hough") == 0)
{
use_hough_ = true;
}else if (used_algorithm.compare ("GC") == 0)
{
use_hough_ = false;
}
else
{
std::cout << "Wrong algorithm name.\n";
showHelp (argv[0]);
exit (-1);
}
}
//General parameters
//如果命令行解析到以下命令以及设置的值
//将用新设置的值对原变量进行修改
pcl::console::parse_argument (argc, argv, "--model_ss", model_ss_);
pcl::console::parse_argument (argc, argv, "--scene_ss", scene_ss_);
pcl::console::parse_argument (argc, argv, "--rf_rad", rf_rad_);
pcl::console::parse_argument (argc, argv, "--descr_rad", descr_rad_);
pcl::console::parse_argument (argc, argv, "--cg_size", cg_size_);
pcl::console::parse_argument (argc, argv, "--cg_thresh", cg_thresh_);
}
double
computeCloudResolution (const pcl::PointCloud<PointType>::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<PointType> 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;
}
int
main (int argc, char *argv[])
{
parseCommandLine (argc, argv);
pcl::PointCloud<PointType>::Ptr model (new pcl::PointCloud<PointType> ());//模型点云
pcl::PointCloud<PointType>::Ptr model_keypoints (new pcl::PointCloud<PointType> ());//模型角点
pcl::PointCloud<PointType>::Ptr scene (new pcl::PointCloud<PointType> ());//目标点云
pcl::PointCloud<PointType>::Ptr scene_keypoints (new pcl::PointCloud<PointType> ());//目标角点
pcl::PointCloud<NormalType>::Ptr model_normals(new pcl::PointCloud<NormalType>());//模型法线
pcl::PointCloud<NormalType>::Ptr scene_normals (new pcl::PointCloud<NormalType> ());//目标法线
pcl::PointCloud<DescriptorType>::Ptr model_descriptors (new pcl::PointCloud<DescriptorType> ());//模型描述子
pcl::PointCloud<DescriptorType>::Ptr scene_descriptors (new pcl::PointCloud<DescriptorType> ());//目标描述子
//
// Load clouds载入文件载入模型
//
if (pcl::io::loadPCDFile (model_filename_, *model) < 0)
{
std::cout << "Error loading model cloud." << std::endl;
showHelp (argv[0]);
return (-1);
}
if (pcl::io::loadPCDFile (scene_filename_, *scene) < 0)
{
std::cout << "Error loading scene cloud." << std::endl;
showHelp (argv[0]);
return (-1);
}
//
// Set up resolution invariance设置分辨率
//
if (use_cloud_resolution_)
{
//static_cast<float>强制类型转换,告诉编译器已知类型转换所造成的精度损失
float resolution = static_cast<float> (computeCloudResolution (model));
if (resolution != 0.0f)
{
model_ss_ *= resolution;
scene_ss_ *= resolution;
rf_rad_ *= resolution;
descr_rad_ *= resolution;
cg_size_ *= resolution;
}
std::cout << "Model resolution: " << resolution << std::endl;
std::cout << "Model sampling size: " << model_ss_ << std::endl;
std::cout << "Scene sampling size: " << scene_ss_ << std::endl;
std::cout << "LRF support radius: " << rf_rad_ << std::endl;
std::cout << "SHOT descriptor radius: " << descr_rad_ << std::endl;
std::cout << "Clustering bin size: " << cg_size_ << std::endl << std::endl;
}
//
// Compute Normals计算法线
// normalestimationomp估计局部表面特性 分别计算三个特性:法向量、曲率、平行,使用openmp标准
// 初始化调度程序并设置要使用的线程数(默认为0)
//
pcl::NormalEstimationOMP<PointType, NormalType> norm_est;
//设置K近邻搜索的阈值为10个点
norm_est.setKSearch (10);//十个近邻点比较适合kinect相机获取的点云数据,如果是其他类型的点云数据,需要读者进行测试
//计算模型点云的法线
norm_est.setInputCloud (model);//设置输入点云
norm_est.compute (*model_normals);//计算点云法线
//计算场景点云的法线
norm_est.setInputCloud (scene);
norm_est.compute (*scene_normals);
//
// Downsample Clouds to Extract keypoints,对点云进行下采样
// 均匀采样点云并提取关键点
//
pcl::PointCloud<int> sampled_indices;
pcl::UniformSampling<PointType> uniform_sampling;
//对模型点云进行下采样
uniform_sampling.setInputCloud (model);//设置输入点云
uniform_sampling.setRadiusSearch (model_ss_);//设置半径
uniform_sampling.filter(*model_keypoints);//滤波
std::cout << "Model total points: " << model->size () << "; Selected Keypoints: " << model_keypoints->size () << std::endl;
//对目标点云进行下采样
uniform_sampling.setInputCloud (scene);//设置输入点云为目标点云
uniform_sampling.setRadiusSearch (scene_ss_);//设置半径
uniform_sampling.filter (*model_keypoints);//对目标点云进行下采样
pcl::copyPointCloud (*scene, sampled_indices.points, *scene_keypoints);
std::cout << "Scene total points: " << scene->size () << "; Selected Keypoints: " << scene_keypoints->size () << std::endl;
//
// Compute Descriptor for keypoints计算关键点的特征描述子
//
pcl::SHOTEstimationOMP<PointType, NormalType, DescriptorType> descr_est;
descr_est.setRadiusSearch (descr_rad_);//设置搜索半径
//计算模型点云的描述子
descr_est.setInputCloud (model_keypoints);//输入模型的关键点
descr_est.setInputNormals (model_normals);//输入模型的法线
descr_est.setSearchSurface (model);//输入模型的点云
descr_est.compute (*model_descriptors);//计算模型的描述子
//计算目标点云的描述子
descr_est.setInputCloud(scene_keypoints);//输入目标点云的关键点
descr_est.setInputNormals (scene_normals);//计算目标点云的法线
descr_est.setSearchSurface (scene);//输入目标点云
descr_est.compute (*scene_descriptors);
//
// Find Model-Scene Correspondences with KdTree
// 使用KD-Tree寻找到model-scene的匹配点
//
pcl::CorrespondencesPtr model_scene_corrs (new pcl::Correspondences ());
pcl::KdTreeFLANN<DescriptorType> match_search;//设置配准的方法
match_search.setInputCloud (model_descriptors);//输入模板点云的描述子
// For each scene keypoint descriptor, find nearest neighbor into the model keypoints descriptor cloud and add it to the correspondences vector.
//对于每一个目标点云的关键点描述子都要找到模板中匹配的关键点描述子并将其添加到相应的匹配向量之中
for (size_t i = 0; i < scene_descriptors->size (); ++i)
{
std::vector<int> neigh_indices (1);//设置最近邻点的索引
std::vector<float> neigh_sqr_dists (1);//输入模板点云的描述子
//忽略NAN点
if (!pcl_isfinite (scene_descriptors->at (i).descriptor[0])) //skipping NaNs
{
continue;
}
//scene_descriptor->at(i)是给定点云 1是临近点个数 neigh_indices是描述子与临近点的平方距离
int found_neighs = match_search.nearestKSearch (scene_descriptors->at (i), 1, neigh_indices, neigh_sqr_dists);
//只有当描述子与临近点的平方距离小于0.25时(描述子与临近点的距离一般在0-1之间,才添加匹配)
if(found_neighs == 1 && neigh_sqr_dists[0] < 0.25f) // add match only if the squared descriptor distance is less than 0.25 (SHOT descriptor distances are between 0 and 1 by design)
{
//neigh_indices[0]是给定点,i是配准数 neigh_sqr_dists是与给定点之间的平方距离
pcl::Correspondence corr (neigh_indices[0], static_cast<int> (i), neigh_sqr_dists[0]);
model_scene_corrs->push_back (corr);//将配准的点储存在容器之中
}
}
std::cout << "Correspondences found: " << model_scene_corrs->size () << std::endl;
//
// Actual Clustering实际配准方法的实现
//
std::vector<Eigen::Matrix4f, Eigen::aligned_allocator<Eigen::Matrix4f> > rototranslations;
std::vector<pcl::Correspondences> clustered_corrs;
// Using Hough3D使用Hough3D算法寻找匹配点
if (use_hough_)
{
//
// Compute (Keypoints) Reference Frames only for Hough
//计算参考帧的Hough,也就是关键点
//
std::cout << "使用3D 霍夫变换方法" << endl;
pcl::PointCloud<RFType>::Ptr model_rf (new pcl::PointCloud<RFType> ());
pcl::PointCloud<RFType>::Ptr scene_rf (new pcl::PointCloud<RFType> ());
//特征估计的方法,点云,法线,参考帧
pcl::BOARDLocalReferenceFrameEstimation<PointType, NormalType, RFType> rf_est;
rf_est.setFindHoles (true);
rf_est.setRadiusSearch (rf_rad_);//设置搜索半径
rf_est.setInputCloud (model_keypoints);//模型关键点
rf_est.setInputNormals (model_normals);//模型法线
rf_est.setSearchSurface (model);//模型的参考帧
rf_est.compute (*model_rf);
rf_est.setInputCloud (scene_keypoints);//目标点云的关键点
rf_est.setInputNormals (scene_normals);//目标点云的法线
rf_est.setSearchSurface (scene);//目标点云的参考帧
rf_est.compute (*scene_rf);
// Clustering聚类的方法
//对于输入点的聚类,以为区分不同的实例的场景中的模型
pcl::Hough3DGrouping<PointType, PointType, RFType, RFType> clusterer;
clusterer.setHoughBinSize (cg_size_);//霍夫空间设置每个bin的大小
clusterer.setHoughThreshold (cg_thresh_);//阈值
clusterer.setUseInterpolation (true);
clusterer.setUseDistanceWeight (false);
clusterer.setInputCloud (model_keypoints);
clusterer.setInputRf (model_rf);
clusterer.setSceneCloud (scene_keypoints);
clusterer.setSceneRf (scene_rf);
clusterer.setModelSceneCorrespondences (model_scene_corrs);
//clusterer.cluster (clustered_corrs);辨认出聚类的对象
clusterer.recognize (rototranslations, clustered_corrs);
}
else // Using GeometricConsistency 使用几何一致性性质
{
pcl::GeometricConsistencyGrouping<PointType, PointType> gc_clusterer;
gc_clusterer.setGCSize (cg_size_);//设置几何一致性的大小
gc_clusterer.setGCThreshold (cg_thresh_);//阈值
gc_clusterer.setInputCloud (model_keypoints);
gc_clusterer.setSceneCloud (scene_keypoints);
gc_clusterer.setModelSceneCorrespondences (model_scene_corrs);
//gc_clusterer.cluster (clustered_corrs);
gc_clusterer.recognize (rototranslations, clustered_corrs);
}
//
// Output results 输出结果,找到输入模型是否在场景之中出现
//
std::cout << "Model instances found: " << rototranslations.size () << std::endl;
for (size_t i = 0; i < rototranslations.size (); ++i)
{
std::cout << "\n Instance " << i + 1 << ":" << std::endl;
std::cout << " Correspondences belonging to this instance: " << clustered_corrs[i].size () << std::endl;
// Print the rotation matrix and translation vector
//打印出相对于输入模型的旋转矩阵与平移矩阵
Eigen::Matrix3f rotation = rototranslations[i].block<3,3>(0, 0);
Eigen::Vector3f translation = rototranslations[i].block<3,1>(0, 3);
printf ("\n");
printf (" | %6.3f %6.3f %6.3f | \n", rotation (0,0), rotation (0,1), rotation (0,2));
printf (" R = | %6.3f %6.3f %6.3f | \n", rotation (1,0), rotation (1,1), rotation (1,2));
printf (" | %6.3f %6.3f %6.3f | \n", rotation (2,0), rotation (2,1), rotation (2,2));
printf ("\n");
printf (" t = < %0.3f, %0.3f, %0.3f >\n", translation (0), translation (1), translation (2));
}
//
// Visualization 可视化
//
pcl::visualization::PCLVisualizer viewer ("点云库PCL学习教程第二版-基于对应点聚类的3D模型识别");
viewer.addPointCloud (scene, "scene_cloud");//可视化场景点云
viewer.setBackgroundColor(255,255,255);
pcl::PointCloud<PointType>::Ptr off_scene_model (new pcl::PointCloud<PointType> ());
pcl::PointCloud<PointType>::Ptr off_scene_model_keypoints (new pcl::PointCloud<PointType> ());
if (show_correspondences_ || show_keypoints_)//可视化配准点
{
// We are translating the model so that it doesn't end in the middle of the scene representation
//对输入的模型进行旋转与平移,使其在可视化界面的中间位置
pcl::transformPointCloud (*model, *off_scene_model, Eigen::Vector3f (0,0,0), Eigen::Quaternionf (1, 0, 0, 0));
pcl::transformPointCloud (*model_keypoints, *off_scene_model_keypoints, Eigen::Vector3f (0,0,0), Eigen::Quaternionf (1, 0, 0, 0));
pcl::visualization::PointCloudColorHandlerCustom<PointType> off_scene_model_color_handler (off_scene_model, 0, 255, 0);
viewer.addPointCloud (off_scene_model, off_scene_model_color_handler, "off_scene_model");
}
if (show_keypoints_)//可视化关键点
{
pcl::visualization::PointCloudColorHandlerCustom<PointType> scene_keypoints_color_handler (scene_keypoints, 0, 0, 255);
viewer.addPointCloud (scene_keypoints, scene_keypoints_color_handler, "scene_keypoints");
viewer.setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 5, "scene_keypoints");
pcl::visualization::PointCloudColorHandlerCustom<PointType> off_scene_model_keypoints_color_handler (off_scene_model_keypoints, 0, 0, 255);
viewer.addPointCloud (off_scene_model_keypoints, off_scene_model_keypoints_color_handler, "off_scene_model_keypoints");
viewer.setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 5, "off_scene_model_keypoints");
}
for (size_t i = 0; i < rototranslations.size (); ++i)
{
pcl::PointCloud<PointType>::Ptr rotated_model (new pcl::PointCloud<PointType> ());
pcl::transformPointCloud (*model, *rotated_model, rototranslations[i]);
std::stringstream ss_cloud;
ss_cloud << "instance" << i;
pcl::visualization::PointCloudColorHandlerCustom<PointType> rotated_model_color_handler (rotated_model, 255, 0, 0);
viewer.addPointCloud (rotated_model, rotated_model_color_handler, ss_cloud.str ());
if (show_correspondences_)//显示配准连接
{
for (size_t j = 0; j < clustered_corrs[i].size (); ++j)
{
std::stringstream ss_line;
ss_line << "correspondence_line" << i << "_" << j;
PointType& model_point = off_scene_model_keypoints->at (clustered_corrs[i][j].index_query);
PointType& scene_point = scene_keypoints->at (clustered_corrs[i][j].index_match);
// We are drawing a line for each pair of clustered correspondences found between the model and the scene
viewer.addLine<PointType, PointType> (model_point, scene_point, 0, 255, 0, ss_line.str ());
}
}
}
while (!viewer.wasStopped ())
{
viewer.spinOnce ();
}
return (0);
}
3.报错
结果还是有很多问题,虽然有结果,先扔到这里,后续回来再消化
4.问题解决
【报错】User Error 1001: argument to num_threads clause must be positive
【解决方法】在计算法线时添加代码:norm_est.setNumberOfThreads(1);
【参考】关于Error1001