从网格渲染RGB-D图像

时间:2013-07-15 12:43:21

标签: c++ graphics 3d rendering vtk

我有一个网格模型(.ply文件),我用VTK渲染它并更改了摄像机视点。

从新的相机视角,如何渲染RGB和深度图像?我认为这可能是光线追踪,但不确定

编辑

我想生成一个真实的深度图,而不仅仅是深度的可视化。可视化可以通过使用Z缓冲区中的值并在0-255之间缩放来实现,但这不提供真实的深度信息。

3 个答案:

答案 0 :(得分:4)

为了从相机中获取真实世界的深度,我已经完成了以下工作:

double z = render_win->GetZbufferDataAtPoint(x,y);
worldPicker->Pick(x, y, z, renderer);
worldPicker->GetPickPosition(coords);
double euclidean_distance = sqrt(pow((coords[0] - cam_pos[0]), 2) + pow(coords[1] - cam_pos[1], 2) + pow(coords[2] - cam_pos[2], 2));

其中cam_pos是真实世界坐标中的摄像机位置。这似乎可以解决问题

答案 1 :(得分:2)

如果你已经有应用程序设置和渲染,这里有几行,这必须是在网格渲染至少一次之后

// Screenshot  
vtkSmartPointer<vtkWindowToImageFilter> windowToImageFilter = 
vtkSmartPointer<vtkWindowToImageFilter>::New();
windowToImageFilter->SetInput(renderWindow);
windowToImageFilter->SetMagnification(3); //set the resolution of the output image (3     times the current resolution of vtk render window)
windowToImageFilter->SetInputBufferTypeToRGBA(); //also record the alpha (transparency)   channel
windowToImageFilter->Update();

vtkSmartPointer<vtkPNGWriter> writer = 
  vtkSmartPointer<vtkPNGWriter>::New();
writer->SetFileName("screenshot2.png");
writer->SetInputConnection(windowToImageFilter->GetOutputPort());
writer->Write();

这来自VTK Public Wiki

答案 2 :(得分:1)

要检索RGB图像,您可以使用vtkWindowToImageFilter类,允许从vtkWindow读取数据。 对于深度图像,我尝试了一种解决方案,即使用vtkDepthSortPolyData修改网格上的颜色,再次从窗口中读取。

参见上面的例子:

# define VTK_CREATE(type, nom) vtkSmartPointer<type> nom = vtkSmartPointer<type>::New()
int main ()
   {
      VTK_CREATE(vtkPLYReader, reader);
      reader->SetFileName ("mesh.ply");
      reader->Update ();

      // create depth sort algo
      VTK_CREATE(vtkDepthSortPolyData, sort);
      sort->SetInput(reader->GetOutput ());

      // init stuff for rendering
      VTK_CREATE(vtkPolyDataMapper, mapper);
      VTK_CREATE(vtkActor, actor);
      VTK_CREATE(vtkRenderer, rend);
      VTK_CREATE(vtkRenderWindow, rw);
      VTK_CREATE(vtkRenderWindowInteractor, inte);

      VTK_CREATE(vtkCamera, cam);
      sort->SetDirectionToBackToFront (); // camera direction
      sort->SetCamera (cam); // set camera or runtime warning
      sort->SortScalarsOn ();
      sort->Update ();

      mapper->SetScalarVisibility(true);

      // limit max scalar (nb Color)
      mapper->SetScalarRange (0, sort->GetOutput ()->GetNumberOfCells ());
      mapper->SetInputConnection(sort->GetOutputPort ());
      mapper->Update ();

      actor->SetMapper(mapper);
      actor->RotateY (59); // transform with a rotation to see depth
      actor->GetProperty()->SetColor(1, 0, 0);
      sort->SetProp3D (actor); // set the actor to the algo

      rend->SetActiveCamera (cam);
      rw->AddRenderer(rend);
      rend->AddActor(actor);
      inte->SetRenderWindow (rw);

      inte->Initialize ();

      // read depth image from vtk Window
      VTK_CREATE (vtkWindowToImageFilter, screen);
      screen->SetInput (rw);
      screen->Update ();
      // start rendering for visualization
      rw->Render ();

      // save the depth img as a png
      VTK_CREATE(vtkPNGWriter, writer);
      writer->SetFileName ("output.png");
      writer->SetInputConnection (screen->GetOutputPort ());
      writer->Write ();

      inte->Start ();
      return 0;
    }

在VTK 5.10上测试。