如何从 vtkWindowToImageFilter 中提取前景?

how to extract the foreground from vtkWindowToImageFilter?

我正在将 vtk 与 qt 集成,我有 vtkWindowToImageFilter 输入设置为 vtkGenericOpenGLRenderWindow,为什么我只提取图像前景?

vtkWindowToImageFilter *w2if = vtkWindowToImageFilter::New();
w2if->ReadFrontBufferOff();
w2if->SetInput(renderWindow);
w2if->Update();
vtkImageData *img = w2if->GetOutput();

vtkImageData 包含背景 我怎样才能使用它?

我的建议是重新渲染除背景之外的所有内容。类似下面的内容

auto oldSB = renderWindow->GetSwapBuffers();
renderWindow->SwapBuffersOff();

// Hide the background (set visibility to false or whatever)
...
auto windowToImageFilter = vtkSmartPointer<vtkWindowToImageFilter>::New();
windowToImageFilter->SetInput(renderWindow);

windowToImageFilter->SetScale(1);
windowToImageFilter->SetInputBufferTypeToRGBA();

windowToImageFilter->ReadFrontBufferOff();
windowToImageFilter->Update(); // Issues a render on input

renderWindow->SetSwapBuffers(oldSB);
renderWindow->SwapBuffersOn();

// Show background again (set visibility to true or whatever)
...

auto img = windowToImageFilter->GetOutput();

下面的渲染调用将再次显示背景。