点云获取pcl点云以某个点云的已经分块得区域的交集

2024-06-07 03:12

本文主要是介绍点云获取pcl点云以某个点云的已经分块得区域的交集,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

首先将点云分块得到区域后,获取每个块的box的最大最小点云,然后提取box内的点云。

                pcl::IndicesPtr indexes(new pcl::Indices());pcl::getPointsInBox(*cloud_1, min_pt, max_pt, *indexes);// --------------------------取框内和框外点------------------------------------pcl::ExtractIndices<PointType> extr;extr.setInputCloud(cloud);  // 设置输入点云extr.setIndices(indexes);   // 设置索引pcl::PointCloud<PointType>::Ptr cloud_in_box(new pcl::PointCloud<PointType>());extr.filter(*cloud_in_box); // 提取对应索引的点云cout << "方框内点的个数为:" << cloud_in_box->points.size() << endl;pcl::PointCloud<PointType>::Ptr cloud_out_box(new pcl::PointCloud<PointType>);extr.setNegative(true);    // 提取对应索引之外的点extr.filter(*cloud_out_box);cout << "方框外点的个数为:" << cloud_out_box->points.size() << endl;

但是不知道为什么每次提取的indexes都不对,提取的区域大部分都是包含整个点云的点,很奇怪。于是看了下提取box点云的源码

pcl::IndicesPtr indexes(new pcl::Indices());pcl::getPointsInBox(*cloud_1, min_pt, max_pt, *indexes);
//
template <typename PointT> inline void
pcl::getPointsInBox (const pcl::PointCloud<PointT> &cloud, Eigen::Vector4f &min_pt, Eigen::Vector4f &max_pt,Indices &indices)
{indices.resize (cloud.size ());int l = 0;// If the data is dense, we don't need to check for NaNif (cloud.is_dense){for (std::size_t i = 0; i < cloud.size (); ++i){// Check if the point is inside boundsif (cloud[i].x < min_pt[0] || cloud[i].y < min_pt[1] || cloud[i].z < min_pt[2])continue;if (cloud[i].x > max_pt[0] || cloud[i].y > max_pt[1] || cloud[i].z > max_pt[2])continue;indices[l++] = int (i);}}// NaN or Inf values could exist => check for themelse{for (std::size_t i = 0; i < cloud.size (); ++i){// Check if the point is invalidif (!std::isfinite (cloud[i].x) || !std::isfinite (cloud[i].y) || !std::isfinite (cloud[i].z))continue;// Check if the point is inside boundsif (cloud[i].x < min_pt[0] || cloud[i].y < min_pt[1] || cloud[i].z < min_pt[2])continue;if (cloud[i].x > max_pt[0] || cloud[i].y > max_pt[1] || cloud[i].z > max_pt[2])continue;indices[l++] = int (i);}}indices.resize (l);
}

不知道为什么不行,但看到只是比价点的大小是否在box内,按照源码修改,完美解决。

//
// Created by wzs on 2024/6/6.
//#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <stdlib.h> //rand()头文件
#include <pcl/io/pcd_io.h>
#include <pcl/octree/octree.h>
#include <boost/thread/thread.hpp>
#include <pcl/visualization/pcl_visualizer.h>#include <pcl/filters/extract_indices.h>
#include <pcl/common/common.h>
#include "pcl/common/centroid.h"
#include "pcl/common/distances.h"using namespace std;typedef pcl::PointXYZ PointType;int main()
{string path("../test_data/1/3.pcd");// 原始点云所在文件夹string outpath("../test_data/1/voxel_map");        // 分块保存路径文件夹名string path_1("../test_data/1/3_slope.pcd");string outpath_1("../test_data/1/voxel_map_1");        // 分块保存路径文件夹名string outpath_2("../test_data/1/voxel_map_2");        // 分块保存路径文件夹名float resolution = 1.0;        // 设置体素分辨率//-------------------------- 加载点云 --------------------------pcl::PointCloud<PointType>::Ptr cloud(new pcl::PointCloud<PointType>);if (pcl::io::loadPCDFile(path, *cloud) < 0){PCL_ERROR("Couldn't read file \n");
//        system("pause");return -1;}pcl::PointCloud<PointType>::Ptr cloud_1(new pcl::PointCloud<PointType>);if (pcl::io::loadPCDFile(path_1, *cloud_1) < 0){PCL_ERROR("cloud_slopen't read file \n");
//        system("pause");return -1;}// -----------------------获取分块点云保存路径----------------------------------printf("开始进行点云分块!!\n");
//    string::size_type iPos = path.find_last_of('//') + 1;
//    string filename = path.substr(iPos, path.length() - iPos);
//    string name = filename.substr(0, filename.rfind("."));// ---------------------使用八叉树快速构建体素索引------------------------------pcl::octree::OctreePointCloud<PointType> octree(resolution);octree.setInputCloud(cloud);octree.addPointsFromInputCloud(); // 从点云中构建八叉树pcl::Indices pointIdxVec;         // 体素内点的索引//-----------------------------开始分块-----------------------------------------
//    boost::shared_ptr<pcl::visualization::PCLVisualizer>viewer(new pcl::visualization::PCLVisualizer("planar segment")); ;
//    viewer->setBackgroundColor(0, 0, 0);
//    viewer->setWindowName("点云分块");pcl::PointCloud<PointType>::Ptr seg_cloud(new pcl::PointCloud<PointType>);
//    pcl::PointCloud<PointType>::Ptr seg_cloud_1(new pcl::PointCloud<PointType>);
//    pcl::PointCloud<PointType>::Ptr seg_cloud_1_0(new pcl::PointCloud<PointType>);int num_voxel = 0;for (auto iter = octree.leaf_breadth_begin(); iter != octree.leaf_breadth_end(); ++iter){num_voxel++;}float mean_voxel_size_ = cloud_1->size() / num_voxel;int begin = 0;// 构建八叉树叶子节点迭代器,遍历八叉树for (auto iter = octree.leaf_breadth_begin(); iter != octree.leaf_breadth_end(); ++iter){auto key = iter.getCurrentOctreeKey(); // 获取当前迭代器八叉树节点的八叉树键auto it_key = octree.findLeaf(key.x, key.y, key.z); // 检查是否存在于八叉树中if (it_key != nullptr){pointIdxVec.clear();//从八叉树叶子节点中获取单个叶子容器中的点索引pointIdxVec = iter.getLeafContainer().getPointIndicesVector();if (pointIdxVec.size() == 0) // 体素内点个数为0则跳过{continue;}else{seg_cloud->clear();std::stringstream ss;ss << outpath << "//"<< "_block_" << begin + 1 << ".pcd";pcl::copyPointCloud(*cloud, pointIdxVec, *seg_cloud);pcl::io::savePCDFileBinary(ss.str(), *seg_cloud);cout << "第[" << begin + 1 << "]块点云分割完毕!  " << seg_cloud->size() << "    " << mean_voxel_size_ << endl;pcl::PointCloud<PointType>::Ptr cloud_in_box(new pcl::PointCloud<PointType>());PointType point_min_, point_max_;pcl::getMinMax3D(*seg_cloud,point_min_,point_max_);
//                cout << "Min x: " << point_min_.x << endl;
//                cout << "Min y: " << point_min_.y << endl;
//                cout << "Min z: " << point_min_.z << endl;
//                cout << "Max x: " << point_max_.x << endl;
//                cout << "Max y: " << point_max_.y << endl;
//                cout << "Max z: " << point_max_.z << endl;
//                Eigen::Vector4f min_pt = { point_min_.x,point_min_.y,point_min_.z,0 };
//                Eigen::Vector4f max_pt = { point_max_.x,point_max_.y,point_max_.z,0 };for (std::size_t i = 0; i < cloud_1->size (); ++i){// Check if the point is inside boundsif (cloud_1->points[i].x < point_min_.x || cloud_1->points[i].y < point_min_.y || cloud_1->points[i].z < point_min_.y)continue;if (cloud_1->points[i].x > point_max_.x || cloud_1->points[i].y > point_max_.y || cloud_1->points[i].z > point_max_.z)continue;cloud_in_box->emplace_back(cloud_1->points[i]);}cout << "方框内点的个数为:" << cloud_in_box->points.size() << endl;std::stringstream ss_1;ss_1 << outpath_1 << "//"<< "_block_" << begin + 1 << ".pcd";if(cloud_in_box->size() > 0)pcl::io::savePCDFileBinary(ss_1.str(), *cloud_in_box);//center distanceEigen::Vector4f centroid_seg_, centroid_i_box_;PointType centroid_seg_p_, centroid_i_box_p_;pcl::compute3DCentroid(*seg_cloud,centroid_seg_);pcl::compute3DCentroid(*cloud_in_box,centroid_i_box_);centroid_seg_p_.x = centroid_seg_[0];centroid_seg_p_.y = centroid_seg_[1];centroid_seg_p_.z = centroid_seg_[2];centroid_i_box_p_.x = centroid_i_box_[0];centroid_i_box_p_.y = centroid_i_box_[1];centroid_i_box_p_.z = centroid_i_box_[2];float dis_centroid_seg_in_box_ = pcl::euclideanDistance(centroid_seg_p_,centroid_i_box_p_);cout << "dis_centroid_seg_in_box_:   " << dis_centroid_seg_in_box_ << endl;std::stringstream ss_2;ss_2 << outpath_2 << "//"<< "_block_" << begin + 1 << ".pcd";if (dis_centroid_seg_in_box_ > 0.1 && cloud_in_box->size() > mean_voxel_size_){pcl::io::savePCDFileBinary(ss_2.str(), *cloud_in_box);cout << begin + 1 << endl;}begin++;}seg_cloud->clear(); // 每分割一次,清空一下容器,进而提高电脑工作效率}}printf("点云体素分块完毕!!!");return 0;
}

效果图如下:

这篇关于点云获取pcl点云以某个点云的已经分块得区域的交集的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1038008

相关文章

springboot的controller中如何获取applicatim.yml的配置值

《springboot的controller中如何获取applicatim.yml的配置值》本文介绍了在SpringBoot的Controller中获取application.yml配置值的四种方式,... 目录1. 使用@Value注解(最常用)application.yml 配置Controller 中

golang实现nacos获取配置和服务注册-支持集群详解

《golang实现nacos获取配置和服务注册-支持集群详解》文章介绍了如何在Go语言中使用Nacos获取配置和服务注册,支持集群初始化,客户端结构体中的IpAddresses可以配置多个地址,新客户... 目录golang nacos获取配置和服务注册-支持集群初始化客户端可选参数配置new一个客户端 支

Python版本信息获取方法详解与实战

《Python版本信息获取方法详解与实战》在Python开发中,获取Python版本号是调试、兼容性检查和版本控制的重要基础操作,本文详细介绍了如何使用sys和platform模块获取Python的主... 目录1. python版本号获取基础2. 使用sys模块获取版本信息2.1 sys模块概述2.1.1

Java发送SNMP至交换机获取交换机状态实现方式

《Java发送SNMP至交换机获取交换机状态实现方式》文章介绍使用SNMP4J库(2.7.0)通过RCF1213-MIB协议获取交换机单/多路状态,需开启SNMP支持,重点对比SNMPv1、v2c、v... 目录交换机协议SNMP库获取交换机单路状态获取交换机多路状态总结交换机协议这里使用的交换机协议为常

MyBatis/MyBatis-Plus同事务循环调用存储过程获取主键重复问题分析及解决

《MyBatis/MyBatis-Plus同事务循环调用存储过程获取主键重复问题分析及解决》MyBatis默认开启一级缓存,同一事务中循环调用查询方法时会重复使用缓存数据,导致获取的序列主键值均为1,... 目录问题原因解决办法如果是存储过程总结问题myBATis有如下代码获取序列作为主键IdMappe

C#使用iText获取PDF的trailer数据的代码示例

《C#使用iText获取PDF的trailer数据的代码示例》开发程序debug的时候,看到了PDF有个trailer数据,挺有意思,于是考虑用代码把它读出来,那么就用到我们常用的iText框架了,所... 目录引言iText 核心概念C# 代码示例步骤 1: 确保已安装 iText步骤 2: C# 代码程

Spring Boot中获取IOC容器的多种方式

《SpringBoot中获取IOC容器的多种方式》本文主要介绍了SpringBoot中获取IOC容器的多种方式,包括直接注入、实现ApplicationContextAware接口、通过Spring... 目录1. 直接注入ApplicationContext2. 实现ApplicationContextA

python获取指定名字的程序的文件路径的两种方法

《python获取指定名字的程序的文件路径的两种方法》本文主要介绍了python获取指定名字的程序的文件路径的两种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 最近在做项目,需要用到给定一个程序名字就可以自动获取到这个程序在Windows系统下的绝对路径,以下

JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法

《JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法》:本文主要介绍JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法,每种方法结合实例代码给大家介绍的非常... 目录引言:为什么"相等"判断如此重要?方法1:使用some()+includes()(适合小数组)方法2

SpringBoot 获取请求参数的常用注解及用法

《SpringBoot获取请求参数的常用注解及用法》SpringBoot通过@RequestParam、@PathVariable等注解支持从HTTP请求中获取参数,涵盖查询、路径、请求体、头、C... 目录SpringBoot 提供了多种注解来方便地从 HTTP 请求中获取参数以下是主要的注解及其用法:1