6

我有一个网格模型,并且使用 VTK,从给定的相机位置(x,y,z)渲染了它的视图。我可以将其保存到 RGB 图像 (640x480),但我还想保存深度图,其中每个像素存储来自相机的深度值。

我已尝试按照此示例Zbuffer使用渲染窗口给出的值。问题是唯一存储范围[0,1]中的值。相反,我正在尝试创建合成范围图像,在其中存储每个像素与相机的深度/距离。类似于 Kinect 生成的图像,我试图从网格模型的特定视点创建一个。Zbufer

编辑- 添加一些代码

我当前的代码:

加载网格

string mesh_filename = "mesh.ply";
    vtkSmartPointer<vtkPLYReader> mesh_reader = read_mesh_ply(mesh_filename);

    vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
    mapper->SetInputConnection(mesh_reader->GetOutputPort());

    vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
    actor->SetMapper(mapper);

    vtkSmartPointer<vtkRenderer> renderer =  vtkSmartPointer<vtkRenderer>::New();
    vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
    renderWindow->AddRenderer(renderer);
    renderWindow->SetSize(640, 480);

    vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
    renderWindowInteractor->SetRenderWindow(renderWindow);

    //Add the actors to the scene
    renderer->AddActor(actor);
    renderer->SetBackground(1, 1, 1);

创建一个相机并将其放置在某个地方

vtkSmartPointer<vtkCamera> camera = vtkSmartPointer<vtkCamera>::New();

    renderer->SetActiveCamera(camera);
    camera->SetPosition(0,0,650);
    //Render and interact
    renderWindow->Render();

从 z 缓冲区获取结果

double b = renderer->GetZ(320, 240);

在本例中,这给出了 0.999995。由于值在 [0,1] 之间,我不知道如何解释,如您所见,我已将相机设置为在 z 轴上 650 个单位,因此我假设该像素的 z 距离(位于渲染 RGB 中的对象上)应接近 650。

4

1 回答 1

2

这个 python 片段说明了如何将 z 缓冲区值转换为实际距离。非线性映射定义如下:

numerator = 2.0 * z_near * z_far
denominator = z_far + z_near - (2.0 * z_buffer_data_numpy - 1.0) * (z_far - z_near)
depth_buffer_data_numpy = numerator / denominator

在这里,一个完整的例子:

import vtk
import numpy as np
from vtk.util import numpy_support
import matplotlib.pyplot as plt

vtk_renderer = vtk.vtkRenderer()
vtk_render_window = vtk.vtkRenderWindow()
vtk_render_window.AddRenderer(vtk_renderer)
vtk_render_window_interactor = vtk.vtkRenderWindowInteractor()
vtk_render_window_interactor.SetRenderWindow(vtk_render_window)
vtk_render_window_interactor.Initialize()

source = vtk.vtkCubeSource()
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(source.GetOutputPort())
actor = vtk.vtkActor()
actor.SetMapper(mapper)
actor.RotateX(60.0)
actor.RotateY(-35.0)
vtk_renderer.AddActor(actor)

vtk_render_window.Render()
active_vtk_camera = vtk_renderer.GetActiveCamera()
z_near, z_far = active_vtk_camera.GetClippingRange()

z_buffer_data = vtk.vtkFloatArray()
width, height = vtk_render_window.GetSize()
vtk_render_window.GetZbufferData(
    0, 0, width - 1, height - 1, z_buffer_data)
z_buffer_data_numpy = numpy_support.vtk_to_numpy(z_buffer_data)
z_buffer_data_numpy = np.reshape(z_buffer_data_numpy, (-1, width))
z_buffer_data_numpy = np.flipud(z_buffer_data_numpy)  # flipping along the first axis (y)

numerator = 2.0 * z_near * z_far
denominator = z_far + z_near - (2.0 * z_buffer_data_numpy - 1.0) * (z_far - z_near)
depth_buffer_data_numpy = numerator / denominator
non_depth_data_value = np.nan
depth_buffer_data_numpy[z_buffer_data_numpy == 1.0] = non_depth_data_value

print(np.nanmin(depth_buffer_data_numpy))
print(np.nanmax(depth_buffer_data_numpy))

plt.imshow(np.asarray(depth_buffer_data_numpy))
plt.show()

旁注:在我的系统上,有几次该imshow命令没有显示任何内容。重新运行脚本确实解决了这个问题。

资料来源:

http://web.archive.org
open3d

于 2020-07-08T10:40:32.757 回答