使用机器学习 最近邻算法(Nearest Neighbors)进行点云分析 (scikit-learn Open3D numpy)

使用 NearestNeighbors 进行点云分析

在数据分析和机器学习领域,最近邻算法(Nearest Neighbors)是一种常用的非参数方法。它广泛应用于分类、回归和聚类分析等任务。下面将介绍如何使用 scikit-learn 库中的 NearestNeighbors 类来进行点云数据的处理,并通过 Open3D 库进行可视化展示。

在这里插入图片描述

最近邻算法简介

最近邻算法是一种基于距离的算法,它通过计算数据点之间的距离来查找给定数据点的最近邻居。常用的距离度量包括欧氏距离、曼哈顿距离和余弦相似度等。最近邻算法的优点在于简单易懂且无需假设数据的分布形式,适用于各种类型的数据。

代码示例

使用 NearestNeighbors 查找点云数据的最近邻,并使用 Open3D 进行可视化。

在这里插入图片描述

步骤一:导入必要的库
import open3d as o3d
import numpy as np
from sklearn.neighbors import NearestNeighbors
import time
步骤二:定义函数来创建点与点之间的连接线
def create_lines_from_points(points, k_neighbors=6, color=[0, 1, 0]):
    if len(points) < 2:
        return None
    
    start_time = time.time()
    neighbors = NearestNeighbors(n_neighbors=k_neighbors)
    neighbors.fit(points)
    distances, indices = neighbors.kneighbors(points)
    end_time = time.time()
    print(f"Nearest neighbors computation time: {end_time - start_time:.4f} seconds")

    start_time = time.time()
    lines = []
    for i in range(len(points)):
        for j in indices[i]:
            if i < j:  # 避免重复的线
                lines.append([i, j])
    end_time = time.time()
    print(f"Line creation time: {end_time - start_time:.4f} seconds")

    colors = [color for i in range(len(lines))]
    
    line_set = o3d.geometry.LineSet()
    line_set.points = o3d.utility.Vector3dVector(points)
    line_set.lines = o3d.utility.Vector2iVector(lines)
    line_set.colors = o3d.utility.Vector3dVector(colors)
    return line_set
步骤三:加载点云数据

使用点云数据文件 .pcd 的内容。

pcd_file = """\
VERSION 0.7
FIELDS x y z
SIZE 4 4 4
TYPE F F F
COUNT 1 1 1
WIDTH 28
HEIGHT 1
VIEWPOINT 0 0 0 1 0 0 0
POINTS 28
DATA ascii
0.301945 -0.1810271 1.407832
0.3025161 -0.1733161 1.322455
0.3003909 -0.167791 1.717239
0.2926154 -0.1333728 1.246899
0.2981626 -0.1311488 1.376031
0.300947 -0.1268353 1.719725
0.2944916 -0.1170874 1.545582
0.3008177 -0.09701672 1.395218
0.2989618 -0.08497152 1.699149
0.3039065 -0.07092351 1.32867
0.3031552 -0.05290076 1.509094
0.2906472 0.02252534 1.617192
0.2972519 0.02116165 1.457043
0.3024158 0.02067187 1.402361
0.2987708 0.01975626 1.286629
0.3014581 0.06462696 1.304869
0.289153 0.1107126 1.859879
0.2879259 0.1625713 1.583842
0.2952633 0.1989845 1.431798
0.3078183 -0.1622952 1.816048
0.3001072 -0.147239 1.970708
0.2990342 -0.1194922 1.950798
0.2979593 -0.09225944 1.931052
0.2929263 0.02492997 1.965327
0.3061717 0.1117098 1.621875
0.3004842 0.03407142 1.999085
0.3023082 -0.1527775 1.553968
0.3008434 0.250506 1.55337
"""

# 解析点云数据
lines = pcd_file.strip().split("\n")
points = []
for line in lines[11:]:
    points.append([float(value) for value in line.split()])
points = np.array(points)
步骤四:创建连接线并进行可视化
# 创建连接线并进行可视化
line_set = create_lines_from_points(points, k_neighbors=6, color=[0, 1, 0])
o3d.visualization.draw_geometries([line_set])

结论

以上展示了如何使用 scikit-learn 中的 NearestNeighbors 类来计算点云数据的最近邻,并使用 Open3D 库将结果进行可视化。这种方法可以用于点云数据的分析、物体检测以及3D建模等多个领域。

完整代码

import open3d as o3d
import numpy as np
from sklearn.neighbors import NearestNeighbors
import time

def create_lines_from_points(points, k_neighbors=6, color=[0, 1, 0]):
    if len(points) < 2:
        return None
    
    start_time = time.time()
    neighbors = NearestNeighbors(n_neighbors=k_neighbors)
    neighbors.fit(points)
    distances, indices = neighbors.kneighbors(points)
    end_time = time.time()
    print(f"Nearest neighbors computation time: {end_time - start_time:.4f} seconds")

    start_time = time.time()
    lines = []
    for i in range(len(points)):
        for j in indices[i]:
            if i < j:  # avoid duplicate lines
                lines.append([i, j])
    end_time = time.time()
    print(f"Line creation time: {end_time - start_time:.4f} seconds")

    colors = [color for i in range(len(lines))]
    
    line_set = o3d.geometry.LineSet()
    line_set.points = o3d.utility.Vector3dVector(points)
    line_set.lines = o3d.utility.Vector2iVector(lines)
    line_set.colors = o3d.utility.Vector3dVector(colors)
    return line_set

# Load point cloud data from a .pcd file
pcd_file = """\
VERSION 0.7
FIELDS x y z
SIZE 4 4 4
TYPE F F F
COUNT 1 1 1
WIDTH 28
HEIGHT 1
VIEWPOINT 0 0 0 1 0 0 0
POINTS 28
DATA ascii
0.301945 -0.1810271 1.407832
0.3025161 -0.1733161 1.322455
0.3003909 -0.167791 1.717239
0.2926154 -0.1333728 1.246899
0.2981626 -0.1311488 1.376031
0.300947 -0.1268353 1.719725
0.2944916 -0.1170874 1.545582
0.3008177 -0.09701672 1.395218
0.2989618 -0.08497152 1.699149
0.3039065 -0.07092351 1.32867
0.3031552 -0.05290076 1.509094
0.2906472 0.02252534 1.617192
0.2972519 0.02116165 1.457043
0.3024158 0.02067187 1.402361
0.2987708 0.01975626 1.286629
0.3014581 0.06462696 1.304869
0.289153 0.1107126 1.859879
0.2879259 0.1625713 1.583842
0.2952633 0.1989845 1.431798
0.3078183 -0.1622952 1.816048
0.3001072 -0.147239 1.970708
0.2990342 -0.1194922 1.950798
0.2979593 -0.09225944 1.931052
0.2929263 0.02492997 1.965327
0.3061717 0.1117098 1.621875
0.3004842 0.03407142 1.999085
0.3023082 -0.1527775 1.553968
0.3008434 0.250506 1.55337
"""

# Parse the point cloud data
lines = pcd_file.strip().split("\n")
points = []
for line in lines[11:]:
    points.append([float(value) for value in line.split()])
points = np.array(points)

# Create lines from points and visualize
line_set = create_lines_from_points(points, k_neighbors=6, color=[0, 1, 0])
o3d.visualization.draw_geometries([line_set])

在这里插入图片描述

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

NSSCTF_RE(一)暑期

[SWPUCTF 2021 新生赛]简单的逻辑 nss上附件都不对 没看明白怎么玩的 dnspy分析有三个 AchievePoint , game.Player.Bet - 22m; for (int i 0; i < Program.memory.Length; i) { byte[] array Program.memory; int num i; array[num] ^ 34; } Environment.SetEnvironment…

全自主巡航无人机项目思路:STM32/PX4 + ROS + AI 实现从传感融合到智能规划的端到端解决方案

1. 项目概述 本项目旨在设计并实现一款高度自主的自动巡航无人机系统。该系统能够按照预设路径自主飞行&#xff0c;完成各种巡航任务&#xff0c;如电力巡线、森林防火、边境巡逻和灾害监测等。 1.1 系统特点 基于STM32F4和PX4的高性能嵌入式飞控系统多传感器融合技术实现精…

机器学习(五) -- 监督学习(6) --逻辑回归

系列文章目录及链接 上篇&#xff1a;机器学习&#xff08;五&#xff09; -- 监督学习&#xff08;5&#xff09; -- 线性回归2 下篇&#xff1a;机器学习&#xff08;五&#xff09; -- 监督学习&#xff08;7&#xff09; --SVM1 前言 tips&#xff1a;标题前有“***”的内…

GuLi商城-商品服务-API-品牌管理-JSR303分组校验

注解:@Validated 实体类: package com.nanjing.gulimall.product.entity;import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import com.nanjing.common.valid.ListValue; import com.nanjing.common.valid.Updat…

[Vulnhub] Stapler wp-videos+ftp+smb+bash_history权限提升+SUID权限提升+Kernel权限提升

信息收集 IP AddressOpening Ports192.168.8.106TCP:21,22,53,80,123,137,138,139,666,3306, Using Nmap for scanning: $ nmap -p- 192.168.8.106 --min-rate 1000 -sC -sV The results are as follows: PORT STATE SERVICE VERSION 20/tcp closed ftp-data…

交换机和路由器的工作流程

1、交换机工作流程&#xff1a; 将接口中的电流识别为二进制&#xff0c;并转换成数据帧&#xff0c;交换机会记录学习该数据帧的源MAC地址&#xff0c;并将其端口关联起来记录在MAC地址表中。然后查看MAC地址表来查找目标MAC地址&#xff0c;会有一下一些情况&#xff1a; MA…

springboot新闻发布及管理系统-计算机毕业设计源码21929

新闻发布及管理系统的设计与实现 摘 要 新闻发布及管理系统的设计与实现&#xff0c;是当下信息社会发展的重要一环。随着互联网的普及和新闻媒体的数字化转型&#xff0c;一个高效、稳定且功能全面的新闻发布与管理平台显得尤为重要。SpringBoot框架以其简洁、快速和易于集成的…

T113-i系统启动速度优化方案

背景: 硬件:T113-i + emmc 软件:uboot2018 + linux5.4 + QT应用 分支:longan 问题: 全志T113-i的官方系统软件编译出的固件,开机启动时间10多秒,启动时间太长,远远超过行业内linux系统的开机速度,需要进一步优化。 T113-i 优化后启动速度实测数据 启动阶段启动时间(…

Python爬虫速成之路(3):下载图片

hello hello~ &#xff0c;这里是绝命Coding——老白~&#x1f496;&#x1f496; &#xff0c;欢迎大家点赞&#x1f973;&#x1f973;关注&#x1f4a5;&#x1f4a5;收藏&#x1f339;&#x1f339;&#x1f339; &#x1f4a5;个人主页&#xff1a;绝命Coding-CSDN博客 &a…

企业网络实验dhcp-snooping、ip source check,防非法dhcp服务器、自动获取ip(虚拟机充当DHCP服务器)、禁手动修改IP

文章目录 需求相关配置互通性配置配置vmware虚拟机&#xff08;dhcp&#xff09;分配IP服务配置dhcp relay&#xff08;dhcp中继&#xff09;配置dhcp-snooping&#xff08;防非法dhcp服务器&#xff09;配置ip source check&#xff08;禁手动修改IP&#xff09; DHCP中继&…

C语言之指针的奥秘(二)

一、数组名的理解 int arr[10]{1,2,3,4,5,6,7,8,9,10}; int *p&arr[0]; 这里使用 &arr[0] 的⽅式拿到了数组第⼀个元素的地址&#xff0c;但是其实数组名本来就是地址&#xff0c;而且是数组首元素的地址。如下&#xff1a; 我们发现数组名和数组⾸元素的地址打印出…

【Python学习笔记】Optuna + Transformer B站视频实践

【Python学习笔记】Optuna Transformer 实践 背景前摇&#xff08;省流可不看&#xff09;&#xff1a; 之前以泰坦尼克号数据集为案例&#xff0c;学习了Optuna的基本操作&#xff0c;为了进一步巩固知识和便于包装简历&#xff0c;决定找个唬人一点的项目练练手。 ————…

Linux:Linux网络总结(附下载链接)

文章目录 下载链接网络问题综合问题访问一个网页的全过程&#xff1f;WebSocket HTTPHTTP基本概念GET与POSTHTTP特性HTTP缓存技术HTTP的演变HTTP1.1 优化 HTTPSHTTP与HTTPS有哪些区别&#xff1f;HTTPS解决了HTTP的哪些问题&#xff1f;HTTPS如何解决的&#xff1f;HTTPS是如何…

【数据结构】手写堆 HEAP

heap【堆】掌握 手写上浮、下沉、建堆函数 对一组数进行堆排序 直接使用接口函数heapq 什么是堆&#xff1f;&#xff1f;&#xff1f;堆是一个二叉树。也就是有两个叉。下面是一个大根堆&#xff1a; 大根堆的每一个根节点比他的子节点都大 有大根堆就有小根堆&#xff1…

数据结构(4.1)——串的存储结构

串的顺序存储 串&#xff08;String&#xff09;的顺序存储是指使用一段连续的存储单元来存储字符串中的字符。 计算串的长度 静态存储(定长顺序存储) #define MAXLEN 255//预定义最大串为255typedef struct {char ch[MAXLEN];//每个分量存储一个字符int length;//串的实际长…

YOLOv8-OBB 旋转目标检测训练自己的数据

数据集制作 标注工具&#xff1a;X-AnyLabeling https://github.com/CVHub520/X-AnyLabeling 下载链接&#xff1a;https://pan.baidu.com/s/1UsnDucBDed8pU1RtaVZhQw?pwd5kel 数据标注可以参考&#xff1a;https://zhuanlan.zhihu.com/p/665036259 1. 选择导出方式为…

Ubuntu搭建Android架构so库交叉编译环境

目录 前言一、下载NDK并安装二、安装NDK三、配置交叉编译工具链四、编写交叉编译脚本 前言 需要将一些源码编译成Android可用的架构的so库 一、下载NDK并安装 https://developer.android.google.cn/ndk/downloads/ 二、安装NDK 将下载下来的android-ndk-r23b-linux.zip解压…

[GICv3] 3. 物理中断处理(Physical Interrupt Handling)

中断生命周期 ​​ 外设通过中断信号线生成中断&#xff0c;或者软件生成中断&#xff08;SGI&#xff09;。Distributor 和 ReDistributor 配合按照中断分组和中断优先级仲裁后将最高优先级的中断分发到 CPU interface。cpu interface 向中断发送到 PEPE 读取 IAR 寄存器&am…

力扣 24两两交换链表中节点

画图 注意有虚拟头结点 注意判断时先判断cur->next ! nullptr,再判断cur->next->next ! nullptr 注意末尾返回dumyhead->next&#xff0c;用新建result指针来接并返回 class Solution { public:ListNode* swapPairs(ListNode* head) {ListNode *dummyhead new …

高等数学第一讲:函数极限与连续

函数极限与连续 文章目录 函数极限与连续1.函数概念与特性1.1 函数定义 1.2 几种重要的基本函数类型1.2.1 反函数1.2.2 复合函数1.2.3 隐函数 1.3 函数的基本特性1.3.1 有界性1.3.2 单调性1.3.3 奇偶性1.3.4 周期性 2. 函数的极限2.1函数的极限的定义2.2 函数的极限的性质2.3 无…