当前位置: 首页 > news >正文

ransac拟合平面,代替open3d的segment_plane

0.open3d打包太大了,所以决定网上找找代码

使用open3d拟合平面并且求平面的法向量,open3d打包大概1个g的大小。

import open3d as o3dpcd = o3d.geometry.PointCloud()pcd.points = o3d.utility.Vector3dVector(points)## 使用RANSAC算法拟合平面plane_model, inliers = pcd.segment_plane(distance_threshold, ransac_n, num_iterations, probability)plane_normal = np.array(plane_model[:3])plane_normal /= np.linalg.norm(plane_normal)X_normal = [1, 0, 0]Y_normal = [0, 1, 0]Z_normal = [0, 0, 1]# 计算夹角(单位为弧度)angle = np.arccos(np.dot(plane_normal, X_normal))# 将夹角转换为角度X_angel = degrees(angle)# 计算夹角(单位为弧度)angle = np.arccos(np.dot(plane_normal, Y_normal))# 将夹角转换为角度Y_angel = degrees(angle)# 计算夹角(单位为弧度)angle = np.arccos(np.dot(plane_normal, Z_normal))# 将夹角转换为角度Z_angel = degrees(angle)

1.找了一个git上的代码

https://github.com/leomariga/pyRANSAC-3D/blob/master/pyransac3d/plane.py

import randomimport numpy as npclass Plane:"""Implementation of planar RANSAC.Class for Plane object, which finds the equation of a infinite plane using RANSAC algorithim.Call `fit(.)` to randomly take 3 points of pointcloud to verify inliers based on a threshold.![Plane](https://raw.githubusercontent.com/leomariga/pyRANSAC-3D/master/doc/plano.gif "Plane")---"""def __init__(self):self.inliers = []self.equation = []def fit(self, pts, thresh=0.05, minPoints=100, maxIteration=1000):"""Find the best equation for a plane.:param pts: 3D point cloud as a `np.array (N,3)`.:param thresh: Threshold distance from the plane which is considered inlier.:param maxIteration: Number of maximum iteration which RANSAC will loop over.:returns:- `self.equation`:  Parameters of the plane using Ax+By+Cy+D `np.array (1, 4)`- `self.inliers`: points from the dataset considered inliers---"""n_points = pts.shape[0]best_eq = []best_inliers = []for it in range(maxIteration):# Samples 3 random pointsid_samples = random.sample(range(0, n_points), 3)pt_samples = pts[id_samples]# We have to find the plane equation described by those 3 points# We find first 2 vectors that are part of this plane# A = pt2 - pt1# B = pt3 - pt1vecA = pt_samples[1, :] - pt_samples[0, :]vecB = pt_samples[2, :] - pt_samples[0, :]# Now we compute the cross product of vecA and vecB to get vecC which is normal to the planevecC = np.cross(vecA, vecB)# The plane equation will be vecC[0]*x + vecC[1]*y + vecC[0]*z = -k# We have to use a point to find kvecC = vecC / np.linalg.norm(vecC)k = -np.sum(np.multiply(vecC, pt_samples[1, :]))plane_eq = [vecC[0], vecC[1], vecC[2], k]# Distance from a point to a plane# https://mathworld.wolfram.com/Point-PlaneDistance.htmlpt_id_inliers = []  # list of inliers idsdist_pt = (plane_eq[0] * pts[:, 0] + plane_eq[1] * pts[:, 1] + plane_eq[2] * pts[:, 2] + plane_eq[3]) / np.sqrt(plane_eq[0] ** 2 + plane_eq[1] ** 2 + plane_eq[2] ** 2)# Select indexes where distance is biggers than the thresholdpt_id_inliers = np.where(np.abs(dist_pt) <= thresh)[0]if len(pt_id_inliers) > len(best_inliers):best_eq = plane_eqbest_inliers = pt_id_inliersself.inliers = best_inliersself.equation = best_eqreturn self.equation, self.inliers

2.改进代码

2.1 提速

用的时候发现代码的速度比open3d的慢了50ms左右。找了一圈找到方法了
https://zhuanlan.zhihu.com/p/62238520
就是替换循环次数

import randomimport numpy as npclass Plane:"""Implementation of planar RANSAC.Class for Plane object, which finds the equation of a infinite plane using RANSAC algorithim.Call `fit(.)` to randomly take 3 points of pointcloud to verify inliers based on a threshold.![Plane](https://raw.githubusercontent.com/leomariga/pyRANSAC-3D/master/doc/plano.gif "Plane")---"""def __init__(self):self.inliers = []self.equation = []def fit(self, pts, thresh=0.05, minPoints=100, maxIteration=1000, P=0.99):"""Find the best equation for a plane.:param pts: 3D point cloud as a `np.array (N,3)`.:param thresh: Threshold distance from the plane which is considered inlier.:param maxIteration: Number of maximum iteration which RANSAC will loop over.:param P: desired probability that we get a good sample:returns:- `self.equation`:  Parameters of the plane using Ax+By+Cy+D `np.array (1, 4)`- `self.inliers`: points from the dataset considered inliers---"""n_points = pts.shape[0]best_eq = []best_inliers = []i = 0while True:if i < maxIteration:i += 1# Samples 3 random pointsid_samples = random.sample(range(0, n_points), 3)pt_samples = pts[id_samples]# We have to find the plane equation described by those 3 points# We find first 2 vectors that are part of this plane# A = pt2 - pt1# B = pt3 - pt1vecA = pt_samples[1, :] - pt_samples[0, :]vecB = pt_samples[2, :] - pt_samples[0, :]# Now we compute the cross product of vecA and vecB to get vecC which is normal to the planevecC = np.cross(vecA, vecB)# The plane equation will be vecC[0]*x + vecC[1]*y + vecC[0]*z = -k# We have to use a point to find kvecC = vecC / np.linalg.norm(vecC)k = -np.sum(np.multiply(vecC, pt_samples[1, :]))plane_eq = [vecC[0], vecC[1], vecC[2], k]# Distance from a point to a plane# https://mathworld.wolfram.com/Point-PlaneDistance.htmlpt_id_inliers = []  # list of inliers idsdist_pt = (plane_eq[0] * pts[:, 0] + plane_eq[1] * pts[:, 1] + plane_eq[2] * pts[:, 2] +plane_eq[3]) / np.sqrt(plane_eq[0] ** 2 + plane_eq[1] ** 2 + plane_eq[2] ** 2)# Select indexes where distance is biggers than the thresholdpt_id_inliers = np.where(np.abs(dist_pt) <= thresh)[0]#https://www.cse.psu.edu/~rtc12/CSE486/lecture15.pdf#speed upif len(pt_id_inliers) > len(best_inliers):maxIteration = math.log(1 - P) / math.log(1 - pow(len(pt_id_inliers) / n_points, 3))best_eq = plane_eqbest_inliers = pt_id_inliersself.inliers = best_inliersself.equation = best_eqif len(pt_id_inliers) > minPoints:breakreturn self.equation, self.inliers

2.2 提升精度

经过测试发现,拟合的平面的精度还是比open3d差。然后使用最小二乘法在求一次平面了

def ransac_fitplan(pts, thresh=5,num_iterations=1000):# # 希望的得到正确模型的概率n_points = pts.shape[0]best_inliers = []P = 0.9999i=0while True:if i<num_iterations:i+=1# 随机在数据中红选出两个点去求解模型id_samples = random.sample(range(0, n_points), 3)pt_samples = pts[id_samples]vecA = pt_samples[1, :] - pt_samples[0, :]vecB = pt_samples[2, :] - pt_samples[0, :]# Now we compute the cross product of vecA and vecB to get vecC which is normal to the planevecC = np.cross(vecA, vecB)# The plane equation will be vecC[0]*x + vecC[1]*y + vecC[0]*z = -k# We have to use a point to find kvecC = vecC / np.linalg.norm(vecC)k = -np.sum(np.multiply(vecC, pt_samples[1, :]))plane_eq = [vecC[0], vecC[1], vecC[2], k]pt_id_inliers = []  # list of inliers idsdist_pt = (plane_eq[0] * pts[:, 0] + plane_eq[1] * pts[:, 1] + plane_eq[2] * pts[:, 2] + plane_eq[3]) / np.sqrt(plane_eq[0] ** 2 + plane_eq[1] ** 2 + plane_eq[2] ** 2)# Select indexes where distance is biggers than the thresholdpt_id_inliers = np.where(np.abs(dist_pt) <= thresh)[0]if len(pt_id_inliers) > len(best_inliers):num_iterations = math.log(1 - P) / math.log(1 - pow(len(pt_id_inliers) / n_points, 3))best_inliers = pt_id_inliers# 判断是否当前模型已经符合超过一半的点if len(pt_id_inliers) > 0.5*n_points:breakelse:break# 最小二乘法拟合平面X = np.column_stack((pts[:, :2], np.ones(pts.shape[0])))coefficients, _, _, _ = lstsq(X[best_inliers, :], pts[best_inliers, 2])return coefficients,best_inliers

相关文章:

ransac拟合平面,代替open3d的segment_plane

0.open3d打包太大了&#xff0c;所以决定网上找找代码 使用open3d拟合平面并且求平面的法向量&#xff0c;open3d打包大概1个g的大小。 import open3d as o3dpcd o3d.geometry.PointCloud()pcd.points o3d.utility.Vector3dVector(points)## 使用RANSAC算法拟合平面plane_m…...

Docker技术--Docker镜像管理

1.Docker镜像特性 ①.镜像创建容器的特点 Docker在创建容器的时候需要指定镜像,每一个镜像都有唯一的标识:image_id,也可也使用镜像名称和版本号做唯一的标识,如果不指定版本号,那么默认使用的是最新的版本标签(laster)。 ②.镜像分层机制 Docker镜像是分层构建的,并通过…...

生态环境保护3D数字展厅提供了一个线上环保知识学习平台

在21世纪的今天&#xff0c;科技与环保的交汇点提供了无数令人兴奋的可能性。其中&#xff0c;生态环境保护3D数字展厅就是一个绝佳的例子。这个展厅以其独特的3D技术&#xff0c;为我们带来了一个全新的、互动的学习环境&#xff0c;让我们能够更直观地了解和理解我们的环境。…...

OPENCV实现计算描述子

1、计算描述子 kp,des = sift.computer(img,kp) 2、其作用是进行特征匹配 3、同时计算关键点和描述 3.1、kp,des = sift.detectAnd Computer(img,...)...

Android View动画之LayoutAnimation的使用

接前篇 Android View动画整理 &#xff0c;本篇介绍 LayoutAnimation 的使用。 参考《安卓开发艺术探索》。 View 动画作用于 View 。 LayoutAnimation 则作用于 ViewGroup &#xff0c; 为 ViewGoup 指定一个动画&#xff0c;ViewGoup 的子 View 出场时就具体动画效果。 简言…...

低代码与低代码平台的概念解析

随着数字化转型和软件需求的不断增长&#xff0c;传统的手写代码开发方式已经无法满足迅速推出应用程序的需求。为了加快软件开发的速度并降低技术门槛&#xff0c;低代码开发模式应运而生。本文将介绍低代码的概念&#xff0c;探讨什么是低代码什么是低代码平台&#xff1f; 一…...

玩转Mysql系列 - 第8篇:详解排序和分页(order by limit),及存在的坑

这是Mysql系列第7篇。 环境&#xff1a;mysql5.7.25&#xff0c;cmd命令中进行演示。 代码中被[]包含的表示可选&#xff0c;|符号分开的表示可选其一。 本章内容 详解排序查询 详解limit limit存在的坑 分页查询中的坑 排序查询&#xff08;order by&#xff09; 电商…...

Django实现音乐网站 ⒂

使用Python Django框架制作一个音乐网站&#xff0c; 本篇主要是歌手详情页-基本信息、单曲列表功能开发实现内容。 目录 歌手基本信息 增加路由 显示视图 模板显示 推荐歌手跳转详情 歌手增加基本信息 表模型增加字段 数据表更新 基本信息增加内容渲染 歌手单曲列表…...

爬虫逆向实战(二十八)--某税网第一步登录

一、数据接口分析 主页地址&#xff1a;某税网 1、抓包 通过抓包可以发现登录接口是factorAccountLogin 2、判断是否有加密参数 请求参数是否加密&#xff1f; 通过查看载荷模块可以发现有一个datagram 和 一个signature加密参数 请求头是否加密&#xff1f; 通过查看“标…...

【Dots之003】SystemAPI.Query相关基础笔记

1、SystemAPI.Query 注&#xff1a;SystemAPI.Query只能作为foreach中in的的子句 SystemAPI.Query<RefRO<LocalTransform>>().WithAll<Obstacle>()解析&#xff1a;对于每个具有LocalTransform和Obstacle的Entity&#xff1b;都会将LocalTransform的只读引…...

vue v-for 例子

vue v-for 例子 <!DOCTYPE html> <html lang"en"><head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, initial-scale1.0"><title>Document</title> </head&…...

206.Flink(一):flink概述,flink集群搭建,flink中执行任务,单节点、yarn运行模式,三种部署模式的具体实现

一、Flink概述 1.基本描述 Flink官网地址:Apache Flink — Stateful Computations over Data Streams | Apache Flink Flink是一个框架和分布式处理引擎,用于对无界和有界数据流进行有状态计算。 2.有界流和无界流 无界流(流): 有定义流的开始,没有定义结束。会无休止…...

科技探究之旅--亲子研学活动

2023年8月26日&#xff0c;广州市从化区齐家社会工作服务中心&#xff08;以下简称“齐家”&#xff09;的“星乐园-乡村儿童公益辅导服务项目”组织了新开村及西湖村助学点24对亲子到广州市白云区文搏3D打印基地进行“科技探究之旅--亲子研学”活动&#xff0c;旨在发现、点燃…...

华为云Stack的学习(三)

四、华为云Stack公共组件 1.华为云Stack公共负载均衡方案介绍 1.1 LVS原理 LVS是四层负载均衡&#xff0c;建立在OSI模型的传输层之上&#xff0c;所以效率非常高。 LVS有两种转发模式&#xff1a; NAT模式的转发主要通过修改IP地址&#xff08;位于OSI模型的第三层网络层&…...

大数据平台三大优势详解-行云管家

大数据平台三大优势详解 1、轻松进行数据共享 企业在管理以及快速发展过程中&#xff0c;有着越来越多的数据需要进行管理&#xff0c;如果单独管理则工作量巨大&#xff0c;且难免出现问题&#xff0c;同时共享难。因此需要大数据平台对数据进行统一管理&#xff0c;以及轻松…...

智慧景区方案:AI与视频融合技术如何助力景区监管智能化升级?

随着经济的发展&#xff0c;人们对生活的需求也不再局限于温饱层面&#xff0c;越来越多的人们开始追求文化、艺术的高层次需求&#xff0c;旅游也逐渐成为人们日常放松的一种方式。由于我国人口多、易扎堆等特点&#xff0c;景区的运营监管方式也亟需改革。TSINGSEE青犀智能分…...

HTML基础--Form表单--内联元素

目录 Form表单 表单元素 创建表单 () 文本输入 () 密码输入 单选按钮 () 和 复选框 () 下拉列表 () 和 选项 ()提交按钮 () 重置按钮 () 块元素与行内元素&#xff08;内联元素&#xff09; Form表单 HTML中的表单&#xff08;<form>&#xff09;是一个重要的元…...

【月度刷题计划同款】常规状压 DP 启发式搜索

题目描述 这是 LeetCode 上的 「1879. 两个数组最小的异或值之和」 &#xff0c;难度为 「困难」。 Tag : 「状压 DP」、「动态规划」、「启发式搜索」 给你两个整数数组 nums1 和 nums2&#xff0c;它们长度都为 n。 两个数组的 异或值之和 为 (nums1[0] XOR nums2[0]) (nums…...

C#: Json序列化和反序列化,集合为什么多出来一些元素?

如下面的例子&#xff0c;很容易看出问题&#xff1a; 如果类本身的无参构造函数&#xff0c; 就添加了一些元素&#xff0c;序列化&#xff0c;再反序列化&#xff0c;会导致元素增加。 如果要避免&#xff0c;必须添加&#xff1a; new JsonSerializerSettings() { Object…...

Docker教程-centos快速安装和配置Docker

# step 1: 安装必要的一些系统工具 sudo yum install -y yum-utils device-mapper-persistent-data lvm2# Step 2: 添加软件源信息 sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo# Step 3: 更新并安装 Docker-CE sudo …...

书匠策AI降重降AIGC实测|官网www.shujiangce.com |微信公众号搜一搜 书匠策AI

&#x1f9ea; 一个实验室级别的"论文手术台" 各位正在跟毕业论文死磕的朋友&#xff0c;我今天不讲方法论&#xff0c;不拆写作技巧&#xff0c;我要给你们开一间"论文急诊室"。 你有没有经历过这种绝望&#xff1a;辛辛苦苦写完一万字&#xff0c;查重…...

cursor接入外部大模型教程!新手必看

一、接入前准备 在开始之前&#xff0c;请先登录你的大模型平台&#xff0c;这里使用 我自用的举例官网地址&#xff0c;创建并复制你的 API Key。 这里添加令牌&#xff0c;有名称和分组&#xff0c;简单举例&#xff0c;填入名称 cursor-claude&#xff0c; 一个key只能选一…...

日志分析 Elasticsearch 和 logstach.filebeat.

一、Elasticsearch 到底是啥&#xff1f;简单说&#xff0c;ES 就是一个能飞速搜索和分析海量数据的搜索引擎。类似百度、谷歌&#xff0c;但它是给你公司内部的数据用的。比如&#xff1a;淘宝搜商品&#xff0c;输入“手机 拍照好”&#xff0c;毫秒级给你结果——背后就是 E…...

STM32 ADC实战避坑:轮询、中断、DMA到底怎么选?我的项目血泪经验

STM32 ADC实战避坑&#xff1a;轮询、中断、DMA到底怎么选&#xff1f;我的项目血泪经验 在嵌入式开发中&#xff0c;ADC&#xff08;模数转换器&#xff09;是连接模拟世界与数字世界的关键桥梁。无论是电池电压监测、环境光传感还是工业控制中的各种模拟量采集&#xff0c;AD…...

【技术解读】xNIDS:如何为深度学习入侵检测系统“翻译”可执行的主动防御规则?

1. 深度学习入侵检测的"黑盒困境"&#xff1a;为什么需要翻译器&#xff1f; 第一次接触深度学习入侵检测系统&#xff08;DL-NIDS&#xff09;时&#xff0c;我被它的检测准确率惊艳到了——某些场景下能达到99%以上的识别率。但当我试图把它部署到实际生产环境时&a…...

基于VSCode Remote-SSH的嵌入式Linux开发环境配置与实战

1. 嵌入式开发流程的痛点与优化思路 作为一名在嵌入式行业摸爬滚打了十多年的老工程师&#xff0c;我太清楚传统开发流程里那些让人头疼的环节了。我们最熟悉的模式&#xff0c;就是在PC上写好代码&#xff0c;用交叉编译工具链生成目标板&#xff08;比如ARM架构的开发板&…...

从Pikachu靶场看CSRF Token防护:为什么你的Token机制可能被绕过?聊聊设计缺陷与加固思路

从Pikachu靶场看CSRF Token防护&#xff1a;为什么你的Token机制可能被绕过&#xff1f;聊聊设计缺陷与加固思路 在Web安全领域&#xff0c;CSRF&#xff08;跨站请求伪造&#xff09;攻击一直是开发者需要重点防范的威胁之一。而CSRF Token作为最常用的防护手段&#xff0c;其…...

别再死记硬背公式了!用VisionMaster的N点标定,手把手教你搞定相机和机械手‘对齐’

视觉标定实战&#xff1a;用工具思维破解N点标定难题 在工业自动化领域&#xff0c;相机与机械手的协同工作就像两个语言不通的人试图完成精密舞蹈——标定就是为他们建立共同的坐标系词典。传统教材常将标定过程简化为数学公式的堆砌&#xff0c;导致许多工程师陷入"会推…...

华为HCIA-Datacom认证 第七章第八章 案例教程

华为HCIA-Datacom认证 第七章&第八章 案例教程 一、背景延续:小明的网络运维新课题 前几次网络改造完成后,公司的办公网络已经稳定运行了一阵子。小明也从当初的手忙脚乱成长为一名能独立处理基础网络问题的工程师。然而,随着公司网络的不断扩展,新的管理需求随之而来…...

哔咔漫画下载器:如何轻松构建个人离线漫画图书馆?

哔咔漫画下载器&#xff1a;如何轻松构建个人离线漫画图书馆&#xff1f; 【免费下载链接】picacomic-downloader 哔咔漫画 picacomic pica漫画 bika漫画 PicACG 多线程下载器&#xff0c;带图形界面 带收藏夹&#xff0c;已打包exe 下载速度飞快 项目地址: https://gitcode.…...