【Python-Open3D学习笔记】003点云上色
1. 点云上色示例
pcd.paint_uniform_color([1, 0, 0])
import open3d as o3d
import numpy as np
# 创建一个示例点云
pcd = o3d.geometry.PointCloud()
pcd.points = o3d.utility.Vector3dVector(np.random.rand(2000, 3))
# 设置颜色为红色
pcd.paint_uniform_color([1, 0, 0])
# 可视化点云
o3d.visualization.draw_geometries([pcd], window_name="test", width=1024, height=768)
2. 实际数据案例
直接修改数据中的RGB
值
import os
import open3d as o3d
import pandas as pd
# 读取数据
root_path = r"E:\07-code\tunnelProjectTemp\outer\data\init\regions"
root = {}
with os.scandir(root_path) as entries:
for entry in entries:
if entry.is_file() and entry.name.endswith('.csv'):
root[entry.name[:-4]] = pd.read_csv(entry.path)
# 拼接数据
res_df = pd.concat(list(root.values()), ignore_index=True)
# 修改颜色
res_df['R'] = 78 / 255.0
res_df['G'] = 238 / 255.0
res_df['B'] = 148 / 255.0
# 构建点云数据
pcd = o3d.geometry.PointCloud()
pcd.points = o3d.utility.Vector3dVector(res_df[['X', 'Y', 'Z']].values)
pcd.colors = o3d.utility.Vector3dVector(res_df[['R', 'G', 'B']].values)
# 可视化点云
o3d.visualization.draw_geometries([pcd], window_name="test", width=1024, height=768)
3. 小结
不管是采用PointCloud类中内置的颜色设置方法,还是直接操作原始数据设定颜色,需要注意的是所选取的对RGB值必须在 [0, 1]
范围内,否则颜色不能正常显示,因此需要对RGB值进行归一化操作。
# 修改颜色
res_df['R'] = 78 / 255.0
res_df['G'] = 238 / 255.0
res_df['B'] = 148 / 255.0