open3d实现搜索在一个球内部的点云

news2025/4/16 22:12:17

目录

  • 写在前面
  • 原理
  • 代码
  • 结果
  • 参考

写在前面

1、本文内容
给定一个点云P和一个圆心为center,半径为r的球,搜索出P中属于球内的点

2、平台/环境
使用open3d, cmake,适用windows/linux
3、转载请注明出处:
https://blog.csdn.net/qq_41102371/article/details/131666033

原理

利用open3d的kdtree,以点云P建立kdtree,对给定点进行进行半径搜索,在搜索之前先用边长=2*radius的box裁剪以加速搜索

代码

目录结构如下,请将sphere_search.cpp和CMakeLists.txt放入src
在这里插入图片描述
sphere_search.cpp

#include <iostream>
#include <chrono>

#include <open3d/Open3D.h>

std::shared_ptr<open3d::geometry::PointCloud> PointInSphere(std::shared_ptr<open3d::geometry::PointCloud> pcd, Eigen::Vector3d center, double radius, bool vis)
{
    auto search_s = std::chrono::high_resolution_clock::now();
    std::shared_ptr<open3d::geometry::PointCloud> points_in_sphere(new open3d::geometry::PointCloud);

    if (nullptr == pcd || pcd->IsEmpty())
    {
        *points_in_sphere = *pcd;
        return points_in_sphere;
    }
    std::cout << "point size: " << pcd->points_.size() << " search center: " << center.transpose() << " search radius: " << radius << std::endl;
    if (radius <= 0.0)
    {
        *points_in_sphere = *pcd;
        return points_in_sphere;
    }
    // 先用边长等于2*radius的正方体裁剪,得到更少的点云,然后再搜索更快
    open3d::geometry::OrientedBoundingBox obb;
    obb.center_ = center;
    obb.extent_ = Eigen::Vector3d(radius * 2, radius * 2, radius * 2);
    std::vector<std::size_t> idx_crop = obb.GetPointIndicesWithinBoundingBox(pcd->points_);
    auto pcd_crop = pcd->SelectByIndex(idx_crop);

    auto crop_e = std::chrono::high_resolution_clock::now();
    auto crop_cost = std::chrono::duration_cast<std::chrono::microseconds>(crop_e - search_s).count() / 1000.0;
    std::cout << "crop_cost: " << crop_cost << " ms" << std::endl;

    open3d::geometry::KDTreeFlann target_kdtree;
    target_kdtree.SetGeometry(*pcd_crop);
    std::vector<int> indices;
    std::vector<double> dis;
    target_kdtree.SearchRadius(center, radius, indices, dis);
    if (indices.empty())
    {
        std::cout << "no point in sphere, please check your center or radius" << std::endl;
        return points_in_sphere;
    }
    else
    {
        std::cout << indices.size() << " pints searched in pcd" << std::endl;
    }
    // std::vector<int>转std::vector<std::size_t>
    std::vector<std::size_t> idx_in_sphere;
    for (auto i : indices)
    {
        idx_in_sphere.push_back(i);
    }
    points_in_sphere = pcd_crop->SelectByIndex(idx_in_sphere);

    auto search_e = std::chrono::high_resolution_clock::now();
    auto search_cost = std::chrono::duration_cast<std::chrono::microseconds>(search_e - search_s).count() / 1000.0;
    std::cout << "total cost: " << search_cost << " ms" << std::endl;

    if (vis)
    {
        auto coordinate = open3d::geometry::TriangleMesh::CreateCoordinateFrame(1.0);
        auto pcd_remain = pcd->SelectByIndex(idx_crop, true);
        auto pcd_inbox_remain = pcd_crop->SelectByIndex(idx_in_sphere, true);
        // pcd_remain->PaintUniformColor({1, 0, 0});
        pcd_inbox_remain->PaintUniformColor({0, 1, 0});
        points_in_sphere->PaintUniformColor({1, 1, 0});
        obb.color_ = Eigen::Vector3d(0, 0, 1);
        open3d::visualization::DrawGeometries({coordinate, pcd_remain, 
        pcd_inbox_remain, points_in_sphere,
        std::make_shared<open3d::geometry::OrientedBoundingBox>(obb)}, "sphere search result");
    }
    return points_in_sphere;
}
int main(int argc, char *argv[])
{
    int idx = open3d::utility::GetProgramOptionAsInt(argc, argv, "--idx", 0);
    bool vis = open3d::utility::ProgramOptionExists(argc, argv, "--vis");
    double radius = open3d::utility::GetProgramOptionAsDouble(argc, argv, "--radius", 0);
    std::string path_pcd = open3d::utility::GetProgramOptionAsString(argc, argv, "--path_pcd", "");

    if (path_pcd.empty())
    {
        std::cout << "please input pcd path with --path_pcd" << std::endl;
        return 0;
    }
    std::shared_ptr<open3d::geometry::PointCloud> pcd(new open3d::geometry::PointCloud);
    open3d::io::ReadPointCloud(path_pcd, *pcd);
    std::cout << "read " << path_pcd << " with " << pcd->points_.size() << " points" << std::endl;
    // 使用索引以点云内某一点为圆心
    if (open3d::utility::ProgramOptionExists(argc, argv, "--idx"))
    {
        PointInSphere(pcd, pcd->points_[idx], radius, true);
    }
    else
    {
        // 另外指定圆心
        PointInSphere(pcd, {0, 0, 0}, radius, vis);
    }
    return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.18)
project(SphereSearch)

option(STATIC_WINDOWS_RUNTIME "Use static (MT/MTd) Windows runtime" ON)

if(STATIC_WINDOWS_RUNTIME)
  set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
else()
  set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")
endif()

# set(Open3D_ROOT /root/open3D_install/) #linux
find_package(Open3D REQUIRED)
include_directories(
  ${Open3D_INCLUDE_DIRS}
)

add_executable(sphere_search ./sphere_search.cpp)
target_link_libraries(sphere_search ${Open3D_LIBRARIES})

compile.bat
请为替换自己的open3d目录,该目录包含open3d的cmake文件

cmake -DCMAKE_BUILD_TYPE=Release -DOpen3D_DIR="D:\carlos\install\open3d141\CMake" -S ./src -B ./build
cmake --build ./build --config Release --target ALL_BUILD

run.bat

.\build\Release\sphere_search.exe .\build\Release\sphere_search.exe --path_pcd D:\carlos\my_tools\data\bun000.pcd --radius 0.05 --vis --idx 0                                     

结果

黄色是搜索到的点云
在这里插入图片描述
在这里插入图片描述

参考

主要做激光/影像三维重建,配准、分割等常用点云算法,技术交流、咨询可私信

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/741411.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【代理模式】JDK动态代理示例代码

简介 JDK动态代理是一种基于JDK自带的Proxy类和InvocationHandler接口的代理方式。它可以在运行时动态地创建一个代理对象&#xff0c;该代理对象可以代替真实对象执行某些操作&#xff0c;从而实现对真实对象的代理。 与Java静态代理相比&#xff0c;JDK动态代理具有更高的灵…

[SSM]MyBatis的高级映射及延迟加载

目录 十二、MyBatis的高级映射及延迟加载 12.1多对一 级联属性映射 association 分步查询 12.2多对一延迟加载 12.3一对多 collection 分步查询 12.4一对多延迟加载 十二、MyBatis的高级映射及延迟加载 12.1多对一 多种方式&#xff0c;常见的三种方式&#xff1a; …

【DBA课程-笔记】第2章:MongoDB数据数据查询与分析

目录 一、课程大纲 二、MongoDB 条件查询 1. 制造 MongoDB 测试数据 2. MongoDB 数据查询 与 SQL对应关系 3. MongoDB 查询运算符 4. MongoDB 数据查询、条件查询、过滤 5. MongoDB 条件查询命令 6. MongoDB 数据查询数组条件 A. 精确匹配数组元素&#xff1a; B. 无…

【STM32MP135 - ST官方源码移植】第三章:OPTEE源码移植教程

STM32MP135 OPTEE源码移植教程 一、解压optee的源码压缩包二、拷贝新的设备树文件三、修改Makefile.sdk文件&#xff08;1&#xff09;增加stm32mp135d-atk设备树编译&#xff08;2&#xff09;修改编译器为arm-none-linux-gnueabihf&#xff08;3&#xff09;使用buildroot工具…

J2EEtdtXML介绍

目录 一.XML 什么是格式良好的XML XML的作用 标准的XML格式 二.元素定义 2.1 在XML加入DTD声明 2.2 元素的分类 2.3 元素的限制 三.属性定义 语法 属性类型type 属性描述 tdt解析 四.XML和JSON的区别 五.思维导图 一.XML 什么是格式良好的XML …

请求响应-数组集合参数的接受

数组集合参数 数组参数&#xff1a;请求参数名与形参数组名称相同且请求参数为多个&#xff0c;定义数组类型形参即可接受参数 具体关键代码如下&#xff1a; 在psotman中发出对应请求&#xff0c;结果如下&#xff1a; 集合参数&#xff1a;请求参数与形参集合名称相同且请求…

利用Python给图像添加标签

一、需求&#xff1a; 给指定的图片添加标签 二、代码&#xff1a; # !/usr/bin/env python # coding: utf-8import tkinter as tk from tkinter import filedialog, messagebox import os import json from google.protobuf.json_format import MessageToJson from clarifa…

分布式系统常见问题

一.概述 分布式系统存在网络&#xff0c;时钟&#xff0c;以及许多不可预测的故障。分布式事务&#xff0c;一致性与共识问题&#xff0c;迄今为止仍没有得到很好的解决方案。要想完美地解决分布式系统中的问题不太可能&#xff0c;但是实践中应对特定问题仍有许多可靠的解决方…

rapo雷栢鼠标MT750L说明书(含驱动)

驱动下载&#xff1a;点击跳转至本站下载

Dbeaver客户端存入的DateTime与实际有偏差

前几天调试项目的时候&#xff0c;发现数据库里存入的时间DateTime与我写入的时间有偏差 这里有两列&#xff0c;Name是字符串类型的&#xff0c;Time是DateTime类型的&#xff0c;Name是以当前时间戳命令的&#xff0c;Time是直接存的当前时间&#xff0c;理论上两者是应该一…

PHP之 Socket实践

一 Socket简介 1.1 Socket(套接宇)&#xff0c;用来描述IP地址和端口,是通信链的句柄&#xff0c;应用程序可以通过Socket向网络发送请求或者应答网络请求。 1.2 Socket是支持TCP/IP协议的网络通信的基本操作单元&#xff0c;是对网络通信过程中端点的抽象表示&#xff0c;包…

Vue之v-for(包含key内部原理讲解)

文章目录 前言一、v-for二、key1.介绍2.使用3.原理4.总结 总结 前言 v-for&#xff1a;列表渲染 一、v-for v-for将JSON数据中的数组或对象渲染出列表的样式呈现。 直接见代码实例&#xff0c;如下&#xff1a; <!DOCTYPE html> <html lang"en"><h…

多肽试剂84211-54-1,Cyclo(7-aminoheptanoyl-Phe-D-Trp-Lys-Thr[Bzl]),特点说明

----------资料编辑|陕西新研博美生物科技有限公司小编MISSwu--------- 多肽试剂 | 基础知识概述&#xff08;部分&#xff09;: 英文名称&#xff1a;Cyclo(7-aminoheptanoyl-Phe-D-Trp-Lys-Thr[Bzl]) CAS号&#xff1a;84211-54-1 分子式&#xff1a;C44H57N7O6 分子量&…

UE5_ 地编_siki海岛

1.开启水插件 制作流体 2.开启大陆地形的插件

《Kali渗透基础》08. 弱点扫描(二)

kali渗透 1&#xff1a;OpenVAS / GVM1.1&#xff1a;介绍1.2&#xff1a;安装1.3&#xff1a;使用 2&#xff1a;Nessus2.1&#xff1a;介绍2.2&#xff1a;安装2.3&#xff1a;使用 3&#xff1a;Nexpose 本系列侧重方法论&#xff0c;各工具只是实现目标的载体。 命令与工具…

JAVA并发专题(2)之JMMsynchronizedvolatile详解

一、什么是JMM模型 Java内存模型(Java Memory Model简称JMM)是一种抽象的概念&#xff0c;并不真实存在&#xff0c;它描述的是一组规则或规范&#xff0c;通过这组规范定义了程序中各个变量&#xff08;包括实例字段&#xff0c;静态字段和构成数组对象的元素&#xff09;的访…

Chapter 5: Loops and Iterations | Python for Everybody 讲义笔记_En

文章目录 Python for Everybody课程简介Loops and IterationsUpdating variablesThe while statementInfinite loopsFinishing iterations with continueDefinite loops using forLoop patternsCounting and summing loopsMaximum and minimum loopsDebuggingGlossary Python f…

Linux命令简单学习

文件 输出并覆盖到哪个文件夹 1>文件 正常输出并覆盖 2>文件 输出错误流到某个文件夹 nohup 不强杀不退出 软连接,注意这里需要绝对路径 %% 一个文字的 %%a 当前locale 的星期名缩写(例如&#xff1a; 日&#xff0c;代表星期日)%A 当前locale 的星期名全称 (如&…

wsl 1和wsl 2在形式上的区别

完整的比较请参考&#xff1a; 比较 WSL 版本 | Microsoft Learn 如果是已经安装完成&#xff0c;但安装的是wsl &#xff0c;之后想由 wsl 1升级到wsl 2&#xff0c;请参考&#xff1a;WSL1升级至WSL2_wsl1升级wsl2_goldVitaminC的博客-CSDN博客 在形式上&#xff0c;wsl 1是…

Android AccessibilityService 实现《李跳跳》功能

AccessibilityService&#xff08;无障碍服务&#xff09;是 Android 操作系统中的一个功能&#xff0c;旨在帮助用户具有视觉、听觉或运动上的障碍更轻松地使用设备。它是 Android 提供的一种特殊服务&#xff0c;可以接收设备上发生的各种事件&#xff0c;并提供自定义的反馈…