【PCL】教程 implicit_shape_model.cpp 3D点云数据的对象识别 利用隐式形状模型进行训练和识别...

6415877795f1f5d4540772f4ed252b20.png

ism_test_cat.pcd

a47db2d580c13a227bf90e25040898da.png

参数:ism_train_cat.pcd      0      ism_train_horse.pcd    1      ism_train_lioness.pcd  2      ism_train_michael.pcd  3      ism_train_wolf.pcd     4      ism_test_cat.pcd       0

这里红点表示对应感兴趣类别的对象预测中心

./ism_test ism_train_cat.pcd 0 ism_train_horse.pcd 1 ism_train_lioness.pcd 2 ism_train_michael.pcd 3 ism_train_wolf.pcd 4 ism_test_cat.pcd 0
./ism_test ism_train_cat.pcd 0 ism_train_horse.pcd 1 ism_train_lioness.pcd 2 ism_train_michael.pcd 3 ism_train_wolf.pcd 4 ism_test_horse.pcd 1

这段代码是一个使用PCL(Point Cloud Library)的C++程序,用于3D点云数据的识别训练和测试。程序的流程可以分为几个主要步骤:

  1. 参数检查:程序首先检查传入的参数数量,确保至少有一个训练云和一个测试云,每个云都需要有一个类别ID

  2. 训练数据准备:

  • 使用pcl::NormalEstimation为每个训练点云计算法线

  • 读取训练点云文件,计算法线,然后将点云、法线以及类别ID分别存储

特征提取:接着,使用pcl::FPFHEstimation为每个点计算FPFH(Fast Point Feature Histograms)特征

隐式形状模型训练:之后,使用pcl::ism::ImplicitShapeModelEstimation完成隐式形状模型的训练。这个过程中,将使用到上一步骤计算得到的特征,同时输入训练用的点云数据、法线以及类别ID。

模型保存与加载:训练得到的模型将保存到文件中,可以在后续进行加载和使用。

测试数据处理:与训练数据类似,为测试数据云计算法线

对象识别:利用训练好的模型,对测试数据进行对象识别。识别的过程中会生成一个投票列表,通过算法找出最强的峰值。

可视化:为测试点云着色,将识别的对象标记为不同的颜色,并使用pcl::visualization::CloudViewer显示结果。

整体而言,这段代码主要实现了使用PCL库进行3D点云数据的对象识别,包括数据准备、特征提取、模型训练、对象识别以及结果可视化等步骤。通过计算点云的法线和FPFH特征,利用隐式形状模型进行训练和识别,最终在视窗中显示带有识别结果的点云,以不同颜色区分识别出的对象和背景点云。这个程序适用于需要对3D点云进行对象分类和识别的场景。

#include <iostream> // 包含标准输入输出流库
#include <pcl/io/pcd_io.h> // 包含处理PCD(点云数据)文件输入输出操作的库
#include <pcl/features/normal_3d.h> // 包含计算点云法线特征的库
#include <pcl/features/feature.h> // 包含处理点云特征的基本方法的库
#include <pcl/visualization/cloud_viewer.h> // 包含点云可视化的库
#include <pcl/features/fpfh.h> // 包含计算FPFH(快速点特征直方图)特征的库
#include <pcl/features/impl/fpfh.hpp> // 包含FPFH特征实现的库
#include <pcl/recognition/implicit_shape_model.h> // 包含隐式形状模型(ISM)识别算法的库
#include <pcl/recognition/impl/implicit_shape_model.hpp> // 包含隐式形状模型实现的库


int main (int argc, char** argv)
{
  if (argc < 5 || argc % 2 == 0) // 判断命令行参数数量是否正确
    return (-1);


  unsigned int number_of_training_clouds = (argc - 3) / 2; // 计算训练云的数量


  pcl::NormalEstimation<pcl::PointXYZ, pcl::Normal> normal_estimator; // 法线估计器的初始化
  normal_estimator.setRadiusSearch (25.0); // 设置搜索半径


  std::vector<pcl::PointCloud<pcl::PointXYZ>::Ptr> training_clouds; // 用于存储训练用的点云数据
  std::vector<pcl::PointCloud<pcl::Normal>::Ptr> training_normals; // 用于存储训练数据对应的法线信息
  std::vector<unsigned int> training_classes; // 用于存储训练数据对应的类别ID


  for (unsigned int i_cloud = 0; i_cloud < number_of_training_clouds - 1; i_cloud++)
  {
    pcl::PointCloud<pcl::PointXYZ>::Ptr tr_cloud(new pcl::PointCloud<pcl::PointXYZ> ());
    if ( pcl::io::loadPCDFile <pcl::PointXYZ> (argv[i_cloud * 2 + 1], *tr_cloud) == -1 ) // 加载训练用的点云文件
      return (-1);


    pcl::PointCloud<pcl::Normal>::Ptr tr_normals (new pcl::PointCloud<pcl::Normal>); // 法线信息的容器
    normal_estimator.setInputCloud (tr_cloud); // 设置输入的点云为上述加载的点云
    normal_estimator.compute (*tr_normals); // 计算法线信息


    unsigned int tr_class = static_cast<unsigned int> (strtol (argv[i_cloud * 2 + 2], 0, 10)); // 从命令行参数中获取对应的类别ID


    training_clouds.push_back (tr_cloud); // 将点云数据存入训练集容器
    training_normals.push_back (tr_normals); // 将法线信息存入训练集容器
    training_classes.push_back (tr_class); // 将类别ID存入训练集容器
  }


  pcl::FPFHEstimation<pcl::PointXYZ, pcl::Normal, pcl::Histogram<153> >::Ptr fpfh
    (new pcl::FPFHEstimation<pcl::PointXYZ, pcl::Normal, pcl::Histogram<153> >); // 初始化FPFH特征估计器
  fpfh->setRadiusSearch (30.0); // 设置FPFH的搜索半径
  pcl::Feature< pcl::PointXYZ, pcl::Histogram<153> >::Ptr feature_estimator(fpfh); // 特征估计器的初始化


  pcl::ism::ImplicitShapeModelEstimation<153, pcl::PointXYZ, pcl::Normal> ism; // 初始化隐式形状模型估计器
  ism.setFeatureEstimator(feature_estimator); // 设置特征估计器
  ism.setTrainingClouds (training_clouds); // 设置训练用的点云数据
  ism.setTrainingNormals (training_normals); // 设置训练用的法线信息
  ism.setTrainingClasses (training_classes); // 设置训练用的类别ID
  ism.setSamplingSize (2.0f); // 设置采样大小


  pcl::ism::ImplicitShapeModelEstimation<153, pcl::PointXYZ, pcl::Normal>::ISMModelPtr model (new pcl::features::ISMModel); // 初始化ISM模型
  ism.trainISM (model); // 训练ISM模型


  std::string file ("trained_ism_model.txt"); // 指定模型保存的文件名
  model->saveModelToFile (file); // 保存模型到文件


  model->loadModelFromfile (file); // 从文件加载模型


  unsigned int testing_class = static_cast<unsigned int> (strtol (argv[argc - 1], 0, 10)); // 获取测试数据的类别ID
  pcl::PointCloud<pcl::PointXYZ>::Ptr testing_cloud (new pcl::PointCloud<pcl::PointXYZ> ());
  if ( pcl::io::loadPCDFile <pcl::PointXYZ> (argv[argc - 2], *testing_cloud) == -1 ) // 加载测试用的点云文件
    return (-1);


  pcl::PointCloud<pcl::Normal>::Ptr testing_normals (new pcl::PointCloud<pcl::Normal>); // 初始化存储测试数据法线信息的容器
  normal_estimator.setInputCloud (testing_cloud); // 设置输入的点云为测试点云
  normal_estimator.compute (*testing_normals); // 计算法线信息


  pcl::features::ISMVoteList<pcl::PointXYZ>::Ptr vote_list = ism.findObjects (
    model,
    testing_cloud,
    testing_normals,
    testing_class); // 使用训练好的ISM模型进行物体识别


  double radius = model->sigmas_[testing_class] * 10.0; // 根据类别ID计算搜索半径
  double sigma = model->sigmas_[testing_class]; // 获取当前类别的标准差
  std::vector<pcl::ISMPeak, Eigen::aligned_allocator<pcl::ISMPeak> > strongest_peaks; // 初始化存储搜索到的最强峰值的容器
  vote_list->findStrongestPeaks (strongest_peaks, testing_class, radius, sigma); // 搜索最强峰值


  pcl::PointCloud <pcl::PointXYZRGB>::Ptr colored_cloud (new pcl::PointCloud<pcl::PointXYZRGB>); // 初始化用于可视化的有色云
  colored_cloud->height = 0;
  colored_cloud->width = 1;


  pcl::PointXYZRGB point; // 初始化绘制点的颜色为白色
  point.r = 255;
  point.g = 255;
  point.b = 255;


  for (std::size_t i_point = 0; i_point < testing_cloud->size (); i_point++) // 遍历测试数据的点
  {
    point.x = (*testing_cloud)[i_point].x; // 设置点的坐标
    point.y = (*testing_cloud)[i_point].y;
    point.z = (*testing_cloud)[i_point].z;
    colored_cloud->points.push_back (point); // 将点添加到有色云中
  }
  colored_cloud->height += testing_cloud->size (); // 更新有色云的高度


  point.r = 255; // 设置最强峰值点的颜色为红色
  point.g = 0;
  point.b = 0;
  for (std::size_t i_vote = 0; i_vote < strongest_peaks.size (); i_vote++) // 遍历所有最强峰值点
  {
    point.x = strongest_peaks[i_vote].x; // 设置点的坐标
    point.y = strongest_peaks[i_vote].y;
    point.z = strongest_peaks[i_vote].z;
    colored_cloud->points.push_back (point); // 将点添加到有色云中
  }
  colored_cloud->height += strongest_peaks.size (); // 更新有色云的高度
  //strongest_peaks.size ()==1; 
  pcl::visualization::CloudViewer viewer ("Result viewer"); // 初始化云查看器
  viewer.showCloud (colored_cloud); // 在查看器中显示有色云
  while (!viewer.wasStopped ()) // 持续显示直到用户关闭查看器
  {
  }


  return (0); // 程序正常结束
}

这段代码是一个使用PCL(Point Cloud Library)和隐式形状模型(ISM)进行点云识别的程序。主要实现了以下功能:

  1. 加载训练和测试数据的点云文件。

  2. 计算点云的法线信息。

  3. 使用FPFH算法进行特征估计

  4. 利用隐式形状模型对训练数据进行训练,并保存模型。

  5. 从文件加载训练好的模型。

  6. 使用加载的模型对测试数据进行识别。

  7. 根据识别结果,将识别到的对象在点云中标出,并通过可视化显示结果。

此代码片段适用于需要进行物体识别和分类的场合,特别是在利用3D点云数据进行机器人视觉或智能制造等领域。

pcl::NormalEstimation<pcl::PointXYZ, pcl::Normal>
    normal_estimator

d770c74ac8ed5368000eba5c75d49ca3.png

e7523540e0635826d4407b8c0ea951b3.png

pcl::FPFHEstimation<pcl::PointXYZ, pcl::Normal, pcl::Histogram<153>>::Ptr fpfh(
    new pcl::FPFHEstimation<pcl::PointXYZ,
                            pcl::Normal,
                            pcl::Histogram<153>>);

dd6b416cf5822f1090f9f5c86a1a61f7.png

pcl::Feature<pcl::PointXYZ, pcl::Histogram<153>>::Ptr
   feature_estimator(fpfh); // 特征估计器的初始化

cf8b41f741482bcc2d328ca2207b1c19.png

pcl::ism::ImplicitShapeModelEstimation<153, pcl::PointXYZ, pcl::Normal>
    ism;

f3b3b73dbd61df224176fc635f4ea0e2.png

pcl::ism::ImplicitShapeModelEstimation<153, pcl::PointXYZ, pcl::Normal>::ISMModelPtr
    model(new pcl::features::ISMModel);

6a5c27c78200de01dfe12ebdd944aff0.png

pcl::features::ISMVoteList<pcl::PointXYZ>::Ptr vote_list =
      ism.findObjects(model,
                      testing_cloud,
                      testing_normals,
                      testing_class);

e1777bf76a6a682f5dd853940bd330ff.png

vote_list->findStrongestPeaks(
     strongest_peaks, testing_class, radius, sigma);

9eaca1046b8aed54b3b9d18f62a96a0a.png

参考网址

隐式形状模型 —— 点云库 0.0 文档 --- Implicit Shape Model — Point Cloud Library 0.0 documentation (pcl.readthedocs.io) https://pcl.readthedocs.io/projects/tutorials/en/latest/implicit_shape_model.html#compiling-and-running-the-program

  • 21
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值