Open3d学习计划——高级篇 7(颜色映射)
将颜色映射到从深度相机重建的几何形状。由于彩色帧和深度帧不一定是完美对齐的,所以使用彩色图像进行纹理映射的结果会导致一个模糊的彩色映射。Open3d提供了基于[Zhou2014]中的彩色映射优化算法。下面的教程将会提供彩色映射优化算法的示例。
输入
下面的代码读取彩色和深度图像对,并且生成 rgbd_image
。注意convert_rgb_to_intensity
标志位设置为 False
。只是为了保留8-bit彩色通道而不是使用单通道浮点型图像。
在应用彩色映射优化之前,最好先对RGBD图像进行可视化。debug_mode
选择是否可视化RGBD图像。
def sorted_alphanum(file_list_ordered):
convert = lambda text: int(text) if text.isdigit() else text
alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)]
return sorted(file_list_ordered, key=alphanum_key)
def get_file_list(path, extension=None):
if extension is None:
file_list = [path + f for f in os.listdir(path) if os.path.isfile(join(path, f))]
else:
file_list = [
path + f
for f in os.listdir(path)
if os.path.isfile(os.path.join(path, f)) and os.path.splitext(f)[1] == extension
]
file_list = sorted_alphanum(file_list)
return file_list
path = o3dtut.download_fountain_dataset()
debug_mode = False
rgbd_images = []
depth_image_path = get_file_list(os.path.join(path, "depth/"),
extension=".png")
color_image_path = get_file_list(os.path.join(path, "image/"),
extension=".jpg")
assert (len(depth_image_path) == len(color_image_path))
for i in range(len(depth_image_path)):
depth = o3d.io.read_image(os.path.join(depth_image_path[i]))
color = o3d.io.read_image(os.path.join(color_image_path[i]))
rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
color, depth, convert_rgb_to_intensity=False)
if debug_mode:
pcd = o3d.geometry.PointCloud.create_from_rgbd_image(
rgbd_image,
o3d.camera.PinholeCameraIntrinsic(
o3d.camera.PinholeCameraIntrinsicParameters.
PrimeSenseDefault))
o3d.visualization.draw_geometries([pcd])
rgbd_images.append(rgbd_image)
下面的代码读取相机轨迹和网格数据
camera = o3d.io.read_pinhole_camera_trajectory(
os.path.join(path, "scene/key.log"))
mesh = o3d.io.read_triangle_mesh(
os.path.join(path, "scene", "integrated.ply"))
为了可视化出相机的姿态不适合颜色映射,下面的代码故意设置迭代次数为0,也就是不对其映射做优化。color_map_optimization
使用对应的相机姿态和RGBD图像来绘制网格。如果没有优化的话,可以看到纹理很模糊。
# Before full optimization, let's just visualize texture map
# with given geometry, RGBD images, and camera poses.
option = o3d.color_map.ColorMapOptimizationOption()
option.maximum_iteration = 0
with o3d.utility.VerbosityContextManager(o3d.utility.VerbosityLevel.Debug) as cm:
o3d.color_map.color_map_optimization(mesh, rgbd_images, camera, option)
o3d.visualization.draw_geometries([mesh])
刚性优化
下一步优化相机参数来获得清晰的彩色图像。
下面的代码设置最大迭代次数为300。
# Optimize texture and save the mesh as texture_mapped.ply
# This is implementation of following paper
# Q.-Y. Zhou and V. Koltun,
# Color Map Optimization for 3D Reconstruction with Consumer Depth Cameras,
# SIGGRAPH 2014
option.maximum_iteration = 100 if is_ci else 300
option.non_rigid_camera_coordinate = False
with o3d.utility.VerbosityContextManager(o3d.utility.VerbosityLevel.Debug) as cm:
o3d.color_map.color_map_optimization(mesh, rgbd_images, camera, option)
o3d.visualization.draw_geometries([mesh])
残差表示图像强度不一致,残差越低表示颜色映射质量越好。ColorMapOptimizationOption
默认使用刚性优化,去优化相机的六维姿态。
非刚性优化
为了有一个更好的映射质量,需要使用非刚性优化。要启用非刚性优化只需要在调用color_map_optimization
前将option.non_rigid_camera_coordinate
设置为True
。除了六维相机姿态以外,非刚性优化甚至考虑了由锚点表示的局部图像变形。这种方式更加灵活并且会有着更高的彩色映射质量。残差也会小于刚性优化的情况。
option.maximum_iteration = 100 if is_ci else 300
option.non_rigid_camera_coordinate = True
with o3d.utility.VerbosityContextManager(o3d.utility.VerbosityLevel.Debug) as cm:
o3d.color_map.color_map_optimization(mesh, rgbd_images, camera, option)
o3d.visualization.draw_geometries([mesh])