点云数据常用处理:python实现
目录
- 数据集增强
- 仿射变换
- 平移变换
- 旋转变换
- 尺度变换
- 仿射变换
- 添加噪声
- 高斯噪声
- 随机噪声
- 下采样
- 指定体素
- 指定点数
- 数据标准化
C++版本实现见:点云数据常用处理:C++实现
数据集增强
仿射变换
平移变换
import numpy as np
import random#文件名
old_file=r"rabbit.txt"
new_file=r"rabbit_change.txt"#平移参数
x_offset=random.uniform(-10, 10)
y_offset=random.uniform(-10, 10)
z_offset=random.uniform(-10, 10)#变换矩阵
transformation_matrix=np.array([[1,0,0,x_offset],[0,1,0,y_offset],[0,0,1,z_offset],[0,0,0,1]])#加载文件
old_array=np.loadtxt(old_file)
old_xyz=old_array[:,:3]#补充数据为齐次项
ones_data=np.ones(old_xyz.shape[0])
old_xyz=np.insert(old_xyz,3,values=ones_data,axis=1)#变换数据
new_xyz = np.dot(transformation_matrix,old_xyz.T)
new_array=np.concatenate((new_xyz.T[:,:3],old_array[:,3:]),axis=1)
np.savetxt(new_file,new_array,fmt='%.06f')
旋转变换
import numpy as np
import random#文件名
old_file=r"rabbit.txt"
new_file=r"rabbit_change.txt"#旋转角度
roate_x=random.uniform(-np.pi/10, np.pi/10)
roate_y=random.uniform(-np.pi/10, np.pi/10)
roate_z=random.uniform(-np.pi/10, np.pi/10)roate_x_matrix=np.array([[1,0,0,0],[0,np.cos(roate_x),-np.sin(roate_x),0],[0,np.sin(roate_x),np.cos(roate_x),0],[0,0,0,1]])
roate_y_matrix=np.array([[np.cos(roate_y),0,np.sin(roate_y),0],[0,1,0,0],[-np.sin(roate_y),0,np.cos(roate_y),0],[0,0,0,1]])
roate_z_matrix=np.array([[np.cos(roate_z),-np.sin(roate_z),0,0],[np.sin(roate_z),np.cos(roate_z),0,0],[0,0,1,0],[0,0,0,1]])#变换矩阵
transformation_matrix=dot(roate_z_matrix).dot(roate_y_matrix).dot(roate_x_matrix)#加载文件
old_array=np.loadtxt(old_file)
old_xyz=old_array[:,:3]#补充数据为齐次项
ones_data=np.ones(old_xyz.shape[0])
old_xyz=np.insert(old_xyz,3,values=ones_data,axis=1)#变换数据
new_xyz = np.dot(transformation_matrix,old_xyz.T)
new_array=np.concatenate((new_xyz.T[:,:3],old_array[:,3:]),axis=1)
np.savetxt(new_file,new_array,fmt='%.06f')
尺度变换
import numpy as np
import random#文件名
old_file=r"rabbit.txt"
new_file=r"rabbit_change.txt"#缩放参数
scale=0.1#变换矩阵
transformation_matrix=np.array([[scale,0,0,0],[0,scale,0,0],[0,0,scale,0],[0,0,0,1] ])#加载文件
old_array=np.loadtxt(old_file)
old_xyz=old_array[:,:3]#补充数据为齐次项
ones_data=np.ones(old_xyz.shape[0])
old_xyz=np.insert(old_xyz,3,values=ones_data,axis=1)#变换数据
new_xyz = np.dot(transformation_matrix,old_xyz.T)
new_array=np.concatenate((new_xyz.T[:,:3],old_array[:,3:]),axis=1)
np.savetxt(new_file,new_array,fmt='%.06f')
仿射变换
上面三种变换综合可以写成:
import numpy as np
import random#文件名
old_file=r"rabbit.txt"
new_file=r"rabbit_change.txt"#平移参数
x_offset=random.uniform(-10, 10)
y_offset=random.uniform(-10, 10)
z_offset=random.uniform(-10, 10)#缩放参数
scale=0.1#旋转角度
roate_x=random.uniform(-np.pi/10, np.pi/10)
roate_y=random.uniform(-np.pi/10, np.pi/10)
roate_z=random.uniform(-np.pi/10, np.pi/10)roate_x_matrix=np.array([[1,0,0,0],[0,np.cos(roate_x),-np.sin(roate_x),0],[0,np.sin(roate_x),np.cos(roate_x),0],[0,0,0,1]])
roate_y_matrix=np.array([[np.cos(roate_y),0,np.sin(roate_y),0],[0,1,0,0],[-np.sin(roate_y),0,np.cos(roate_y),0],[0,0,0,1]])
roate_z_matrix=np.array([[np.cos(roate_z),-np.sin(roate_z),0,0],[np.sin(roate_z),np.cos(roate_z),0,0],[0,0,1,0],[0,0,0,1]])#变换矩阵
transformation_matrix=np.array([[scale,0,0,x_offset],[0,scale,0,y_offset],[0,0,scale,z_offset],[0,0,0,1]]).dot(roate_z_matrix).dot(roate_y_matrix).dot(roate_x_matrix)#加载文件
old_array=np.loadtxt(old_file)
old_xyz=old_array[:,:3]#补充数据为齐次项
ones_data=np.ones(old_xyz.shape[0])
old_xyz=np.insert(old_xyz,3,values=ones_data,axis=1)#变换数据
new_xyz = np.dot(transformation_matrix,old_xyz.T)
new_array=np.concatenate((new_xyz.T[:,:3],old_array[:,3:]),axis=1)
np.savetxt(new_file,new_array,fmt='%.06f')
仿射变换:
PCL 仿射变换,实现点云平移旋转
添加噪声
高斯噪声
import numpy as np#文件名
old_file=r"rabbit.txt"
new_file=r"rabbit_change.txt"#高斯噪声
def guass_noise(point, sigma=1, clip=1):point = point.reshape(-1,3)Row, Col = point.shapenoisy_point = np.clip(sigma * np.random.randn(Row, Col), -1*clip, clip)noisy_point += pointreturn noisy_point#加载文件
old_array=np.loadtxt(old_file)
old_xyz=old_array[:,:3]#保存文件
new_xyz=guass_noise(old_xyz)
new_array=np.concatenate((new_xyz,old_array[:,3:]),axis=1)
np.savetxt(new_file,new_array,fmt='%.06f')
随机噪声
import numpy as np#文件名
old_file=r"rabbit.txt"
new_file=r"rabbit_change.txt"#随机噪声
def guass_noise(point, sigma=1, clip=1):point = point.reshape(-1,3)Row, Col = point.shapenoisy_point = np.clip(sigma * np.random.rand(Row, Col), -1*clip, clip)noisy_point += pointreturn noisy_point#加载文件
old_array=np.loadtxt(old_file)
old_xyz=old_array[:,:3]#保存文件
new_xyz=guass_noise(old_xyz)
new_array=np.concatenate((new_xyz,old_array[:,3:]),axis=1)
np.savetxt(new_file,new_array,fmt='%.06f')
下采样
指定体素
import numpy as np#文件名
old_file=r"rabbit.txt"
new_file=r"rabbit_change.txt"#体素滤波
def voxel_filter(point_cloud, leaf_size):filtered_points = []# 计算边界点x_min, y_min, z_min = np.amin(point_cloud, axis=0) #计算x y z 三个维度的最值x_max, y_max, z_max = np.amax(point_cloud, axis=0)# 计算 voxel grid维度Dx = (x_max - x_min) // leaf_size + 1Dy = (y_max - y_min) // leaf_size + 1Dz = (z_max - z_min) // leaf_size + 1# 计算每个点的voxel索引h = list() # h为保存索引的列表for i in range(len(point_cloud)):hx = (point_cloud[i][0] - x_min) // leaf_sizehy = (point_cloud[i][1] - y_min) // leaf_sizehz = (point_cloud[i][2] - z_min) // leaf_sizeh.append(hx + hy * Dx + hz * Dx * Dy)h = np.array(h)#筛选点h_indice = np.argsort(h) #返回h里面的元素按从小到大排序的索引h_sorted = h[h_indice]begin = 0for i in range(len(h_sorted) - 1): # 0~9999if h_sorted[i] != h_sorted[i + 1]:point_idx = h_indice[begin: i + 1]filtered_points.append(np.mean(point_cloud[point_idx], axis=0))begin = i+1#把点云格式改成array,并对外返回filtered_points = np.array(filtered_points, dtype=np.float64)return filtered_points#加载文件
old_array=np.loadtxt(old_file)
old_xyz=old_array[:,:3]#保存文件
new_xyz=voxel_filter(old_xyz, 1)
np.savetxt(new_file,new_xyz,fmt='%.06f')
调用open3d的api:
import open3d as o3d
import numpy as npold_file=r"rabbit.txt"
new_file=r"rabbit_change.txt"cloud=o3d.io.read_point_cloud(old_file,format='xyzrgb')
cloud_downsampled=cloud.voxel_down_sample(1)
cloud_downsampled = np.array(cloud_downsampled.points)
np.savetxt(new_file,cloud_downsampled,fmt='%.06f')
调用pcl的api:
import pcl
import numpy as npold_file=r"rabbit.txt"
new_file=r"rabbit_change.txt"cloud = pcl.PointCloud()
pcd = np.genfromtxt(old_file, delimiter=" ")
pcd = np.array(pcd, dtype = np.float32)
pcd=pcd[:,:3]
cloud.from_array(pcd)cloud_down=pcl.ApproximateVoxelGrid()
pcl.ApproximateVoxelGrid.set_InputCloud(cloud_down, cloud)
pcl.ApproximateVoxelGrid.set_leaf_size(cloud_down, 1, 1, 1)
cloud_downsampled=pcl.ApproximateVoxelGrid.filter(cloud_down)
cloud_downsampled = np.array(cloud_downsampled.to_array())
np.savetxt(new_file,cloud_downsampled,fmt='%.06f')
指定点数
import numpy as np#文件名
old_file=r"rabbit.txt"
new_file=r"rabbit_change.txt"#加载文件
old_array = np.loadtxt(old_file).astype(np.float32)
points_num =2048
choice = np.random.choice(old_array.shape[0], points_num, replace=True)#保存文件
new_array = old_array[choice, :]
np.savetxt(new_file,new_array,fmt='%.06f')
数据标准化
import numpy as np#文件名
old_file=r"rabbit.txt"
new_file=r"rabbit_change.txt"#加载文件
old_array=np.loadtxt(old_file)
old_xyz=old_array[:,:3]#去中心化
centroid = np.mean(old_xyz, axis=0)
new_xyz = old_xyz - centroid#尺度归一化
m = np.max(np.sqrt(np.sum(new_xyz**2, axis=1)))
new_xyz = new_xyz / m#保存文件
new_array=np.concatenate((new_xyz,old_array[:,3:]),axis=1)
np.savetxt(new_file,new_array,fmt='%.06f')
点云数据常用处理:python实现相关推荐
- 点云数据常用处理:C++实现
目录 数据集增强 仿射变换 平移变换 旋转变换 尺度变换 仿射变换 添加噪声 高斯噪声 随机噪声 下采样 指定体素 指定点数 数据标准化 python版本实现见:点云数据常用处理:python实现 数 ...
- 利用MQTT协议与阿里云数据交互的python程序
利用MQTT协议与阿里云数据交互的python程序 设计目的 功能要求和关键问题 环境配置问题 本地程序如何连接云上设备 云上的数据交互问题 界面设计问题 阿里云相关操作 本地程序 设计目的 设计开发 ...
- python数据分析师下载_2020云开见明Python数据分析师特训营,全套课程资源下载...
课程名称 Python数据分析师特训营(python数据分析从入门到精通) 课程预期目标: 01熟练撞我python语法和常用数据结构 02熟练掌握数据分析相关库的运用 03岁数据分析相关流程和常用方 ...
- 数据代码分享|PYTHON用NLP自然语言处理LSTM神经网络TWITTER推特灾难文本数据、词云可视化...
全文下载链接:http://tecdat.cn/?p=28877 作者:Yunfan Zhang Twitter是一家美国社交网络及微博客服务的网站,致力于服务公众对话.迄今为止,Twitter的日活 ...
- 阿里云物联网平台数据解析(python)
阿里云物联网平台数据解析(python) DTU上传设备返回报文消息,通过数据解析后显示各功能数值,这里以智能电表DLT645规约为例进行解析 因为是做光伏的,所以对电表的需求比较多,之前查找了好多文 ...
- 微信小程序云开发——常用功能2:操作云数据库一键批量导入数据(导入json文件)
微信小程序云开发--常用功能2:操作云数据库一键批量导入数据(导入json文件) 今天我们要添加100条数据.下面的过程是先创建一条记录,然后导出这条数据看json文件中是如何编辑字段的,然后仿照这个 ...
- Python+vtk 实现激光点云数据可视化学习(2021.7.12)
Python+VTK实现激光点云数据可视化学习 2021.7.12 1.激光点云与VTK简介 2.配置Python环境(Conda+PyCharm+Python3.6+VTK) 3.点云数据(大约60 ...
- Python处理时空数据常用库案例及练习
本文完整代码.数据集下载.在线运行可以访问这个链接:时空数据Python常用包案例 配套习题与答案可以访问这个链接:时空数据Python常用包案例 - 实操练习题(附答案) Python处理时空数据会 ...
- 【云驻共创】华为云AI之用Python定制我的《本草纲目女孩》
文章目录 前言 一.华为云ModelArts-Notebook介绍 1.华为云ModelArts-Notebook 1.1 Jupyter Notebook是什么 1.2 JupyterLab是什么 ...
- 15 个常用的python 开源框架
注册登录 shaomine 博客园 首页 新随笔 联系 订阅 正在上传-重新上传取消 管理 随笔 - 579 文章 - 1 评论 - 69 阅读 - 253万 15个最受欢迎 ...
最新文章
- P2119 魔法阵(优化枚举,数学运算优化)难度⭐⭐⭐★
- w7怎么查看电脑配置_学室内设计,对电脑配置有何要求?不懂戳这!
- 经典Golang语法50问!
- mediawiki mysql_MediaWiki
- 为什么分布式一定要有redis,redis的一些优缺点
- Eclipse的详细安装步骤
- oracle中将两个字段合并在一起_日期合并
- 关于最新上映电影的评论
- 数据可视化大屏分析流程是怎样
- 光谱共焦传感器在PCB板子上的应用
- 如何应对项目现场EasyNVR视频平台内ini配置文件丢失的现象?
- java社交框架,java毕业设计_springboot框架的社交平台
- Windows 11 LTSC 数字激活方法/HEU KMS Activator 数字激活/LTSC公key激活
- web版ppt制作插件impress.js源码注释翻译
- 谁说大象不能跳舞读后感
- 十进制转换为其他进制(c语言)
- 2021哈工大网安837考研初试经验贴
- 高速USB转8串口产品设计-RS232串口
- c语言入门之我是中国人;
- 穿越沙漠问题(C++倒推法)