point_cloud_transformation.py
1. 平移
import open3d as o3d
import numpy as np
import copy
def translate():
"""平移"""
# 1. read pcd
armadillo_data = o3d.data.ArmadilloMesh()
pcd = o3d.io.read_triangle_mesh(armadillo_data.path).sample_points_poisson_disk(5000)
# 2. translate
pcd_tx = copy.deepcopy(pcd).translate((150, 0, 0)) # xyz
pcd_ty = copy.deepcopy(pcd).translate((0, 200, 0))
# 3. view
print('Displaying original and translated geometries ...')
o3d.visualization.draw([{
"name": "Original Geometry",
"geometry": pcd
}, {
"name": "Translated (in X) Geometry",
"geometry": pcd_tx
}, {
"name": "Translated (in Y) Geometry",
"geometry": pcd_ty
}],
show_ui=True)
2. 旋转
def rotate():
"""旋转xyz轴"""
# 1. read pcd
armadillo_data = o3d.data.ArmadilloMesh()
pcd = o3d.io.read_triangle_mesh(
armadillo_data.path).sample_points_poisson_disk(5000)
pcd_r = copy.deepcopy(pcd).translate((200, 0, 0)) # 沿着x平移
# 2. rotate
R = pcd.get_rotation_matrix_from_xyz((np.pi / 2, 0, np.pi / 4)) # 绕着x旋转90,再绕着z旋转45
pcd_r.rotate(R) # R: 3x3
# 3. view
print('Displaying original and rotated geometries ...')
o3d.visualization.draw([{
"name": "Original Geometry",
"geometry": pcd
}, {
"name": "Rotated Geometry",
"geometry": pcd_r
}],
show_ui=True)
3. 缩放
def scale():
"""缩放"""
# 1. read pcd
armadillo_data = o3d.data.ArmadilloMesh()
pcd = o3d.io.read_triangle_mesh(armadillo_data.path).sample_points_poisson_disk(5000)
pcd_s = copy.deepcopy(pcd).translate((200, 0, 0)) # 沿着x平移
# 2. scale: 点个数不变,改变的是点之间的距离
pcd_s.scale(scale=0.5, center=pcd_s.get_center()) # pcd2.scale(2.0, (40, 0, 0))#点云放大两倍,质心平移至(-40, 0, 0)
# # numpy 方式实现 scale
# points = np.array(pcd_s.points)
# points = points / 2.0 # 缩小到原来的一半
# points[:, 0] = points[:, 0] + 40 # 质心平移到x=40处
# 3. view
print('Displaying original and scaled geometries ...')
o3d.visualization.draw([{
"name": "Original Geometry",
"geometry": pcd
}, {
"name": "Scaled Geometry",
"geometry": pcd_s
}],
show_ui=True)
4. 变换
旋转+平移
def transform():
"""transform: 平移 + 旋转"""
# 1. read pcd
armadillo_data = o3d.data.ArmadilloMesh()
pcd = o3d.io.read_triangle_mesh(
armadillo_data.path).sample_points_poisson_disk(5000)
# 2.
T = np.eye(4) # T: 4x4
T[:3, :3] = pcd.get_rotation_matrix_from_xyz((0, np.pi / 3, np.pi / 2)) # 先绕着y旋转60,绕着z旋转90
T[0, 3] = 150 # 再沿着x轴平移150
T[1, 3] = 200 # 再沿着y轴平移200
print(T)
pcd_t = copy.deepcopy(pcd).transform(T)
# 3. view
print('Displaying original and transformed geometries ...')
o3d.visualization.draw([{
"name": "Original Geometry",
"geometry": pcd
}, {
"name": "Transformed Geometry",
"geometry": pcd_t
}],
show_ui=True)
T4x4变换矩阵:前面3x3控制旋转,最后一列控制平移。
[[ 3.06161700e-17 -5.00000000e-01 8.66025404e-01 1.50000000e+02]
[ 1.00000000e+00 6.12323400e-17 0.00000000e+00 2.00000000e+02]
[-5.30287619e-17 8.66025404e-01 5.00000000e-01 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00 0.00000000e+00 1.00000000e+00]]