日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

点云数据常用处理:python实现

發布時間:2023/12/20 python 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 点云数据常用处理: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实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。