点云获取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整合mybatisPlus实现批量插入并获取ID详解

《SpringBoot整合mybatisPlus实现批量插入并获取ID详解》这篇文章主要为大家详细介绍了SpringBoot如何整合mybatisPlus实现批量插入并获取ID,文中的示例代码讲解详细... 目录【1】saveBATch(一万条数据总耗时:2478ms)【2】集合方式foreach(一万条数

python获取网页表格的多种方法汇总

《python获取网页表格的多种方法汇总》我们在网页上看到很多的表格,如果要获取里面的数据或者转化成其他格式,就需要将表格获取下来并进行整理,在Python中,获取网页表格的方法有多种,下面就跟随小编... 目录1. 使用Pandas的read_html2. 使用BeautifulSoup和pandas3.

SpringBoot UserAgentUtils获取用户浏览器的用法

《SpringBootUserAgentUtils获取用户浏览器的用法》UserAgentUtils是于处理用户代理(User-Agent)字符串的工具类,一般用于解析和处理浏览器、操作系统以及设备... 目录介绍效果图依赖封装客户端工具封装IP工具实体类获取设备信息入库介绍UserAgentUtils

C# foreach 循环中获取索引的实现方式

《C#foreach循环中获取索引的实现方式》:本文主要介绍C#foreach循环中获取索引的实现方式,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录一、手动维护索引变量二、LINQ Select + 元组解构三、扩展方法封装索引四、使用 for 循环替代

Linux下如何使用C++获取硬件信息

《Linux下如何使用C++获取硬件信息》这篇文章主要为大家详细介绍了如何使用C++实现获取CPU,主板,磁盘,BIOS信息等硬件信息,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录方法获取CPU信息:读取"/proc/cpuinfo"文件获取磁盘信息:读取"/proc/diskstats"文

Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案

《Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案》:本文主要介绍Vue3组件中getCurrentInstance()获取App实例,但是返回nu... 目录vue3组件中getCurrentInstajavascriptnce()获取App实例,但是返回n

SpringMVC获取请求参数的方法

《SpringMVC获取请求参数的方法》:本文主要介绍SpringMVC获取请求参数的方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下... 目录1、通过ServletAPI获取2、通过控制器方法的形参获取请求参数3、@RequestParam4、@

Python获取C++中返回的char*字段的两种思路

《Python获取C++中返回的char*字段的两种思路》有时候需要获取C++函数中返回来的不定长的char*字符串,本文小编为大家找到了两种解决问题的思路,感兴趣的小伙伴可以跟随小编一起学习一下... 有时候需要获取C++函数中返回来的不定长的char*字符串,目前我找到两种解决问题的思路,具体实现如下:

golang获取当前时间、时间戳和时间字符串及它们之间的相互转换方法

《golang获取当前时间、时间戳和时间字符串及它们之间的相互转换方法》:本文主要介绍golang获取当前时间、时间戳和时间字符串及它们之间的相互转换,本文通过实例代码给大家介绍的非常详细,感兴趣... 目录1、获取当前时间2、获取当前时间戳3、获取当前时间的字符串格式4、它们之间的相互转化上篇文章给大家介

Python获取中国节假日数据记录入JSON文件

《Python获取中国节假日数据记录入JSON文件》项目系统内置的日历应用为了提升用户体验,特别设置了在调休日期显示“休”的UI图标功能,那么问题是这些调休数据从哪里来呢?我尝试一种更为智能的方法:P... 目录节假日数据获取存入jsON文件节假日数据读取封装完整代码项目系统内置的日历应用为了提升用户体验,