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

相关文章

2、PF-Net点云补全

2、PF-Net 点云补全 PF-Net论文链接:PF-Net PF-Net (Point Fractal Network for 3D Point Cloud Completion)是一种专门为三维点云补全设计的深度学习模型。点云补全实际上和图片补全是一个逻辑,都是采用GAN模型的思想来进行补全,在图片补全中,将部分像素点删除并且标记,然后卷积特征提取预测、判别器判别,来训练模型,生成的像

Android Environment 获取的路径问题

1. 以获取 /System 路径为例 /*** Return root of the "system" partition holding the core Android OS.* Always present and mounted read-only.*/public static @NonNull File getRootDirectory() {return DIR_ANDR

JS和jQuery获取节点的兄弟,父级,子级元素

原文转自http://blog.csdn.net/duanshuyong/article/details/7562423 先说一下JS的获取方法,其要比JQUERY的方法麻烦很多,后面以JQUERY的方法作对比。 JS的方法会比JQUERY麻烦很多,主要则是因为FF浏览器,FF浏览器会把你的换行也当最DOM元素。 <div id="test"><div></div><div></div

vcpkg子包路径批量获取

获取vcpkg 子包的路径,并拼接为set(CMAKE_PREFIX_PATH “拼接路径” ) import osdef find_directories_with_subdirs(root_dir):# 构建根目录下的 "packages" 文件夹路径root_packages_dir = os.path.join(root_dir, "packages")# 如果 "packages"

Weex入门教程之4,获取当前全局环境变量和配置信息(屏幕高度、宽度等)

$getConfig() 获取当前全局环境变量和配置信息。 Returns: config (object): 配置对象;bundleUrl (string): bundle 的 url;debug (boolean): 是否是调试模式;env (object): 环境对象; weexVersion (string): Weex sdk 版本;appName (string): 应用名字;

MFC中App,Doc,MainFrame,View各指针的互相获取

纸上得来终觉浅,为了熟悉获取方法,我建了个SDI。 首先说明这四个类的执行顺序是App->Doc->Main->View 另外添加CDialog类获得各个指针的方法。 多文档的获取有点小区别,有时间也总结一下。 //  App void CSDIApp::OnApp() {      //  App      //  Doc     CDocument *pD

android两种日志获取log4j

android   log4j 加载日志使用方法; 先上图: 有两种方式: 1:直接使用架包 加载(两个都要使用); 架包:android-logging-log4j-1.0.3.jar 、log4j-1.2.15.jar  (说明:也可以使用架包:log4j-1.2.17.jar)  2:对架包输入日志的二次封装使用; 1:直接使用 log4j 日志框架获取日志信息: A:配置 日志 文

17 通过ref代替DOM用来获取元素和组件的引用

重点 ref :官网给出的解释是: ref: 用于注册对元素或子组件的引用。引用将在父组件的$refs 对象下注册。如果在普通DOM元素上使用,则引用将是该元素;如果在子组件上使用,则引用将是组件实例: <!-- vm.$refs.p will be the DOM node --><p ref="p">hello</p><!-- vm.$refs.child will be the c

react笔记 8-19 事件对象、获取dom元素、双向绑定

1、事件对象event 通过事件的event对象获取它的dom元素 run=(event)=>{event.target.style="background:yellowgreen" //event的父级为他本身event.target.getAttribute("aid") //这样便获取到了它的自定义属性aid}render() {return (<div><h2>{

react笔记 8-18 事件 方法 定义方法 获取/改变数据 传值

1、定义方法并绑定 class News extends React.Component {constructor(props) {super(props)this.state = {msg:'home组件'}}run(){alert("我是一个run") //方法写在类中}render() {return (<div><h2>{this.state.msg}</h2><button onCli