Ubuntu 20.04.06 PCL C++学习记录(二十)

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

学习背景

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

学习内容

基于颜色的区域增长细分

源代码及所用函数

源代码

cpp 复制代码
#include<iostream>
#include<thread>//引入线程相关的类和函数,使得程序能够并发执行多个线程
#include<vector>
#include<pcl/point_types.h>
#include<pcl/io/pcd_io.h>
#include<pcl/search/search.h>
#include<pcl/search/kdtree.h>
#include<pcl/visualization/cloud_viewer.h>
#include<pcl/filters/filter_indices.h>//提供了一组用于对点云数据进行滤波和索引操作的工具和算法
#include<pcl/segmentation/region_growing_rgb.h>

using namespace std::chrono_literals;

int main()
{

    pcl::search::Search<pcl::PointXYZRGB>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZRGB>);
    /******************************************读取文件****************************************/
    pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZRGB>);
    if (pcl::io::loadPCDFile<pcl::PointXYZRGB>("/home/jojo/PointCloud/region_growing_rgb_tutorial.pcd",*cloud) == -1)
    {
        std::cout << "文件读取失败" << std::endl;
        return -1;
    }
    /**********************************************去除无效点**********************************/
    pcl::IndicesPtr indices(new std::vector<int>);
    pcl::removeNaNFromPointCloud(*cloud,*indices);

    /**********************************************基于颜色的区域增长细分*************************/
    pcl::RegionGrowingRGB<pcl::PointXYZRGB> reg;
    reg.setInputCloud (cloud);
    reg.setIndices (indices);
    reg.setSearchMethod (tree);
    reg.setDistanceThreshold(10);//设置距离阈值为 10,表示在区域生长过程中,两个点之间的欧氏距离小于等于 10 时,它们属于同一个区域
    reg.setPointColorThreshold(6);//设置点颜色阈值为 6,表示在区域生长过程中,两个点之间的颜色差异小于等于 6 时,它们属于同一个区域。
                                  //颜色差异通常使用欧氏距离度量,即 RGB 颜色空间中的距离。
    reg.setRegionColorThreshold(5);//设置区域颜色阈值为 5,表示在区域生长过程中,一个区域内所有点的平均颜色与种子点的颜色差异小于等于 5 时,该区域被认为是有效的。
    reg.setMinClusterSize (600);

    std::vector <pcl::PointIndices> clusters;
    reg.extract(clusters);
    pcl::PointCloud<pcl::PointXYZRGB>::Ptr colored_cloud = reg.getColoredCloud();
    pcl::visualization::CloudViewer viewer("Cluster Viewer");
    viewer.showCloud(colored_cloud);
    while (!viewer.wasStopped())
    {
        std::this_thread::sleep_for(100us);
    }
    return (0);


}

CMakeLists.txt

cpp 复制代码
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 (region_growing_rgb_segmentation region_growing_rgb_segmentation.cpp)
target_link_libraries (region_growing_rgb_segmentation ${PCL_LIBRARIES})#将PCL库链接到可执行文件目标。

运行结果

函数

补充内容

  • std::chrono_literals 是 C++11 引入的一个命名空间,它提供了一组字面量后缀,用于方便地表示时间段(duration)。这些字面量后缀可以与整数或浮点数一起使用,以指定时间段的大小。
    要使用 std::chrono_literals,需要在 C++ 代码中包含 头文件,并使用 using namespace std::chrono_literals; 语句将该命名空间引入当前作用域。
相关推荐
肆忆_1 天前
# 用 5 个问题学懂 C++ 虚函数(入门级)
c++
不想写代码的星星1 天前
虚函数表:C++ 多态背后的那个男人
c++
端平入洛3 天前
delete又未完全delete
c++
端平入洛4 天前
auto有时不auto
c++
西岸行者5 天前
学习笔记:SKILLS 能帮助更好的vibe coding
笔记·学习
哇哈哈20215 天前
信号量和信号
linux·c++
多恩Stone5 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
欧云服务器5 天前
怎么让脚本命令可以同时在centos、debian、ubuntu执行?
ubuntu·centos·debian
智渊AI5 天前
Ubuntu 20.04/22.04 下通过 NVM 安装 Node.js 22(LTS 稳定版)
ubuntu·node.js·vim
蜡笔小马5 天前
21.Boost.Geometry disjoint、distance、envelope、equals、expand和for_each算法接口详解
c++·算法·boost