Ubuntu 20.04.06 PCL C++学习记录(二十一)【切记使用rm * -rf前先确认是否是对应文件夹】

本文详细介绍了在PCL库中使用条件欧氏聚类对点云进行分割的过程,包括数据预处理、特征提取、自定义区域生长函数的应用以及最终结果的可视化。作者提供了C++代码示例和CMakeLists.txt配置文件,展示了如何在1.10.0版本的PCL和CMake3.16环境中进行操作。
摘要由CSDN通过智能技术生成

@[TOC]PCL中点云分割模块的学习

学习背景

参考书籍:《点云库PCL从入门到精通》以及官方代码PCL官方代码链接,,PCL版本为1.10.0,CMake版本为3.16,测试点云下载地址

学习内容

根据欧几里得距离和需要保持的用户可自定义条件对点进行聚类,点云文件可从上述地址下载。

源代码及所用函数

源代码

#include <pcl/point_types.h>
#include <pcl/io/pcd_io.h>
#include <pcl/console/time.h>
#include <pcl/filters/voxel_grid.h>
#include <pcl/features/normal_3d.h>
#include <pcl/segmentation/conditional_euclidean_clustering.h>


typedef pcl::PointXYZI PointTypeIO;
typedef pcl::PointXYZINormal PointTypeFull;

bool enforceIntensitySimilarity (const PointTypeFull& point_a, const PointTypeFull& point_b, float /*squared_distance*/)

{
  if (std::abs (point_a.intensity - point_b.intensity) < 5.0f)
    return (true);
  else
    return (false);
}


bool enforceNormalOrIntensitySimilarity (const PointTypeFull& point_a, const PointTypeFull& point_b, float /*squared_distance*/)

{

  Eigen::Map<const Eigen::Vector3f> point_a_normal = point_a.getNormalVector3fMap (), point_b_normal = point_b.getNormalVector3fMap ();

  if (std::abs (point_a.intensity - point_b.intensity) < 5.0f)

    return (true);

  if (std::abs (point_a_normal.dot (point_b_normal)) > std::cos (30.0f / 180.0f * static_cast<float> (M_PI)))

    return (true);

  return (false);

}


bool

customRegionGrowing (const PointTypeFull& point_a, const PointTypeFull& point_b, float squared_distance)

{

  Eigen::Map<const Eigen::Vector3f> point_a_normal = point_a.getNormalVector3fMap (), point_b_normal = point_b.getNormalVector3fMap ();

  if (squared_distance < 10000)

  {

    if (std::abs (point_a.intensity - point_b.intensity) < 8.0f)

      return (true);

    if (std::abs (point_a_normal.dot (point_b_normal)) > std::cos (30.0f / 180.0f * static_cast<float> (M_PI)))

      return (true);

  }

  else

  {

    if (std::abs (point_a.intensity - point_b.intensity) < 3.0f)

      return (true);

  }

  return (false);

}


int

main ()

{

  // Data containers used

  pcl::PointCloud<PointTypeIO>::Ptr cloud_in (new pcl::PointCloud<PointTypeIO>), cloud_out (new pcl::PointCloud<PointTypeIO>);

  pcl::PointCloud<PointTypeFull>::Ptr cloud_with_normals (new pcl::PointCloud<PointTypeFull>);

  pcl::IndicesClustersPtr clusters (new pcl::IndicesClusters), small_clusters (new pcl::IndicesClusters), large_clusters (new pcl::IndicesClusters);

  pcl::search::KdTree<PointTypeIO>::Ptr search_tree (new pcl::search::KdTree<PointTypeIO>);

  pcl::console::TicToc tt;


  // Load the input point cloud

  std::cerr << "Loading...\n", tt.tic ();

  pcl::io::loadPCDFile ("/home/jojo/PointCloud/Statues_4.pcd", *cloud_in);

  std::cerr << ">> Done: " << tt.toc () << " ms, " << cloud_in->size () << " points\n";


  // Downsample the cloud using a Voxel Grid class

  std::cerr << "Downsampling...\n", tt.tic ();

  pcl::VoxelGrid<PointTypeIO> vg;

  vg.setInputCloud (cloud_in);

  vg.setLeafSize (80.0, 80.0, 80.0);

  vg.setDownsampleAllData (true);

  vg.filter (*cloud_out);

  std::cerr << ">> Done: " << tt.toc () << " ms, " << cloud_out->size () << " points\n";


  // Set up a Normal Estimation class and merge data in cloud_with_normals

  std::cerr << "Computing normals...\n", tt.tic ();

  pcl::copyPointCloud (*cloud_out, *cloud_with_normals);

  pcl::NormalEstimation<PointTypeIO, PointTypeFull> ne;

  ne.setInputCloud (cloud_out);

  ne.setSearchMethod (search_tree);

  ne.setRadiusSearch (300.0);

  ne.compute (*cloud_with_normals);

  std::cerr << ">> Done: " << tt.toc () << " ms\n";


  // Set up a Conditional Euclidean Clustering class

  std::cerr << "Segmenting to clusters...\n", tt.tic ();

  pcl::ConditionalEuclideanClustering<PointTypeFull> cec (true);

  cec.setInputCloud (cloud_with_normals);

  cec.setConditionFunction (&customRegionGrowing);

  cec.setClusterTolerance (500.0);

  cec.setMinClusterSize (cloud_with_normals->size () / 1000);

  cec.setMaxClusterSize (cloud_with_normals->size () / 5);

  cec.segment (*clusters);

  cec.getRemovedClusters (small_clusters, large_clusters);

  std::cerr << ">> Done: " << tt.toc () << " ms\n";


  // Using the intensity channel for lazy visualization of the output

  for (const auto& small_cluster : (*small_clusters))

    for (const auto& j : small_cluster.indices)

      (*cloud_out)[j].intensity = -2.0;

  for (const auto& large_cluster : (*large_clusters))

    for (const auto& j : large_cluster.indices)

      (*cloud_out)[j].intensity = +10.0;

  for (const auto& cluster : (*clusters))

  {

    int label = rand () % 8;

    for (const auto& j : cluster.indices)

      (*cloud_out)[j].intensity = label;

  }


  // Save the output point cloud

  std::cerr << "Saving...\n", tt.tic ();

  pcl::io::savePCDFile ("output.pcd", *cloud_out);

  std::cerr << ">> Done: " << tt.toc () << " ms\n";


  return (0);

}

CMakeLists.txt

cmake_minimum_required(VERSION 3.16 FATAL_ERROR)#指定CMake的最低版本要求为3.16
project(project)#设置项目名称
find_package(PCL 1.10 REQUIRED)#查找PCL库,要求版本为1.10或更高。
include_directories(${PCL_INCLUDE_DIRS})#将PCL库的头文件目录添加到包含路径中
link_directories(${PCL_LIBRARY_DIRS})#将PCL库的库文件目录添加到链接器搜索路径中。
add_definitions(${PCL_DEFINITIONS})#添加PCL库的编译器定义
add_executable (conditional_euclidean_clustering conditional_euclidean_clustering.cpp)
target_link_libraries (conditional_euclidean_clustering ${PCL_LIBRARIES})#将PCL库链接到可执行文件目标。

运行结果

注意:当使用 PCL 的标准 PCD 查看器打开输出点云时,按“5”将切换到强度通道可视化。 太小的簇将被涂成红色,太大的簇将被涂成蓝色,而实际的簇/感兴趣的物体将被随机着色为黄色和青色, 如果不按则都为一个颜色。
在这里插入图片描述

函数

补充内容

  • std::cout << “降采样中\n”,tt.tic();和std::cout << “降采样中\n”,tt.tic()<<std::endl;的区别
    1. std::cout << “降采样中\n”, tt.tic();
      这个语句使用了逗号运算符 ,。逗号运算符会按照从左到右的顺序依次计算其左右两侧的表达式,并返回右侧表达式的值。在这个语句中,首先会输出字符串 “降采样中\n”,然后计算 tt.tic(),但是 tt.tic() 的返回值会被丢弃,因为它没有被使用或输出。
    2. std::cout << “降采样中\n”, tt.tic() << std::endl;
      这个语句也使用了逗号运算符 ,。同样地,它会先输出字符串 “降采样中\n”,然后计算 tt.tic()。但是,这里的 tt.tic() 的返回值会被传递给 std::cout,然后再输出一个换行符 std::endl。

第二个语句不仅会输出字符串 “降采样中\n”,还会输出 tt.tic() 的返回值,并在最后添加一个换行符

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值