我有一个网格模型(.ply 文件),我已经用 VTK 渲染它并更改了相机视点。
从新的相机视点如何渲染 RGB 和深度图像?我认为这可能是光线追踪,但不确定
编辑
我想生成一个真实的深度图,而不仅仅是深度的可视化。可视化可以通过使用 Z 缓冲区中的值和 0-255 之间的缩放来实现,但这并不提供真正的深度信息。
为了从相机获得真实世界的深度,我做了以下工作:
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
是真实世界坐标中的相机位置。这似乎可以解决问题
如果您已经设置并渲染了应用程序,这里有几行,这必须在网格至少渲染一次之后
// 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 公共维基
要检索 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 上测试。