点群データの書き出し
o3d.io.write_point_cloud("ファイル名", 点群データ)
点群データの切り出し
def pcd_crop(pcd, p1, p2, axis_range, base_axis):
vol = o3d.visualization.read_selection_polygon_volume("cropped_emp.json")
d3 = ["X", "Y", "Z"]
axis_ind = d3.index(base_axis)
points = [[p1[0], p2[0]], [p1[0], p2[1]], [p1[1], p2[1]], [p1[1], p2[0]]]
for point in points:
point.insert(axis_ind, 0)
vol.bounding_polygon.append(np.array(point))
vol.axis_min = axis_range[0]
vol.axis_max = axis_range[1]
vol.orthogonal_axis = base_axis
return vol.crop_point_cloud(pcd)
点群データの加工
def process(pcd, rx, ry, rz, dx, dy, dz):
translation = np.array([[1, 0, 0, dx],
[0, 1, 0, dy],
[0, 0, 1, dz],
[0, 0, 0, 1]])
x_rot = np.array([[1, 0, 0, 0],
[0, math.cos(rx), -(math.sin(rx)), 0],
[0, math.sin(rx), math.cos(rx), 0],
[0, 0, 0, 1]])
y_rot = np.array([[math.cos(ry), 0, math.sin(ry), 0],
[0, 1, 0, 0],
[-(math.sin(ry)), 0, math.cos(ry), 0],
[0, 0, 0, 1]])
z_rot = np.array([[math.cos(rz), -(math.sin(rz)), 0, 0],
[math.sin(rz), math.cos(rz), 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]])
pcd.transform(x_rot)
pcd.transform(y_rot)
pcd.transform(z_rot)
pcd.transform(translation)
return pcd
点群データの読み込み
pcd = o3d.io.read_point_cloud("data.pcd")
点群データに色をつける
pcd.paint_uniform_color([1, 0.706, 0])
座標軸
mesh_frame = o3d.geometry.TriangleMesh.create_coordinate_frame(size=0.03, origin=[0, 0, 0])
OBJからPCDファイルへの変換
import os
import shutil
obj_path = "./pcd.obj" # 適切なPATHを設定
basename = os.path.basename(obj_path)
dir_name = os.path.splitext(basename)[0]
print(basename)
print(dir_name)
n_points = []
for i in range(1, 11):
n_points.append(i * 1000)
os.chdir(obj_path.replace(basename, ""))
os.mkdir(dir_name)
shutil.move(basename, dir_name)
os.chdir(dir_name)
print(os.getcwd())
os.system("ls")
for n_point in n_points:
cmd = "pcl_mesh_sampling %s %s -n_samples %s -leaf_size 0.0001 -no_vis_result" \
% (basename, dir_name + "_" + str(n_point) + ".pcd", str(n_point))
os.system(cmd)
print(cmd)
軸に沿ってジオメトリに外接する直方体の座標を取得する
pcd = read_pcd("***.pcd")
res = pcd.get_axis_aligned_bounding_box()
# bounding boxを表示するときの色を変えられる
res.color = (1, 0, 0)
o3d.visualization.draw_geometries([pcd, res])
print(res)
# AxisAlignedBoundingBox: min: (-9.66359, -13.8488, -17.5596), max: (19.3337, 3.84404, 17.1081)
# resの座標を求める場合
# bounding boxの最小、最大の座標をxyzの順で
print(res.min_bound, res.max_bound)
軸を無視してジオメトリに外接する直方体の座標を取得する
pcd = read_pcd("***.pcd")
obb = pcd.get_oriented_bounding_box()
print(obb)
# OrientedBoundingBox: center: (7.84214, -3.23862, -0.56824), extent: 36.059, 21.7868, 12.9326)
print(obb.center)
print(obb.R) # 回転行列