点云获取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

相关文章

使用Python实现获取网页指定内容

《使用Python实现获取网页指定内容》在当今互联网时代,网页数据抓取是一项非常重要的技能,本文将带你从零开始学习如何使用Python获取网页中的指定内容,希望对大家有所帮助... 目录引言1. 网页抓取的基本概念2. python中的网页抓取库3. 安装必要的库4. 发送HTTP请求并获取网页内容5. 解

C++常见容器获取头元素的方法大全

《C++常见容器获取头元素的方法大全》在C++编程中,容器是存储和管理数据集合的重要工具,不同的容器提供了不同的接口来访问和操作其中的元素,获取容器的头元素(即第一个元素)是常见的操作之一,本文将详细... 目录一、std::vector二、std::list三、std::deque四、std::forwa

使用Python高效获取网络数据的操作指南

《使用Python高效获取网络数据的操作指南》网络爬虫是一种自动化程序,用于访问和提取网站上的数据,Python是进行网络爬虫开发的理想语言,拥有丰富的库和工具,使得编写和维护爬虫变得简单高效,本文将... 目录网络爬虫的基本概念常用库介绍安装库Requests和BeautifulSoup爬虫开发发送请求解

Android App安装列表获取方法(实践方案)

《AndroidApp安装列表获取方法(实践方案)》文章介绍了Android11及以上版本获取应用列表的方案调整,包括权限配置、白名单配置和action配置三种方式,并提供了相应的Java和Kotl... 目录前言实现方案         方案概述一、 androidManifest 三种配置方式

Python如何获取域名的SSL证书信息和到期时间

《Python如何获取域名的SSL证书信息和到期时间》在当今互联网时代,SSL证书的重要性不言而喻,它不仅为用户提供了安全的连接,还能提高网站的搜索引擎排名,那我们怎么才能通过Python获取域名的S... 目录了解SSL证书的基本概念使用python库来抓取SSL证书信息安装必要的库编写获取SSL证书信息

Win32下C++实现快速获取硬盘分区信息

《Win32下C++实现快速获取硬盘分区信息》这篇文章主要为大家详细介绍了Win32下C++如何实现快速获取硬盘分区信息,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 实现代码CDiskDriveUtils.h#pragma once #include <wtypesbase

Android如何获取当前CPU频率和占用率

《Android如何获取当前CPU频率和占用率》最近在优化App的性能,需要获取当前CPU视频频率和占用率,所以本文小编就来和大家总结一下如何在Android中获取当前CPU频率和占用率吧... 最近在优化 App 的性能,需要获取当前 CPU视频频率和占用率,通过查询资料,大致思路如下:目前没有标准的

java两个List的交集,并集方式

《java两个List的交集,并集方式》文章主要介绍了Java中两个List的交集和并集的处理方法,推荐使用Apache的CollectionUtils工具类,因为它简单且不会改变原有集合,同时,文章... 目录Java两个List的交集,并集方法一方法二方法三总结java两个List的交集,并集方法一

golang获取prometheus数据(prometheus/client_golang包)

《golang获取prometheus数据(prometheus/client_golang包)》本文主要介绍了使用Go语言的prometheus/client_golang包来获取Prometheu... 目录1. 创建链接1.1 语法1.2 完整示例2. 简单查询2.1 语法2.2 完整示例3. 范围值

javaScript在表单提交时获取表单数据的示例代码

《javaScript在表单提交时获取表单数据的示例代码》本文介绍了五种在JavaScript中获取表单数据的方法:使用FormData对象、手动提取表单数据、使用querySelector获取单个字... 方法 1:使用 FormData 对象FormData 是一个方便的内置对象,用于获取表单中的键值