0

I'm integrating vtk with qt, i have vtkWindowToImageFilter with input set to vtkGenericOpenGLRenderWindow, how come i extract the image foreground only?

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

the vtkImageData include the background how can i get ride of it?

Error
  • 820
  • 1
  • 11
  • 34

1 Answers1

1

My recommendation is to render everything again except for the background. Something like the following

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();

The following render call will show the background again.

Jens Munk
  • 4,627
  • 1
  • 25
  • 40
  • hi, while your solution seams reasonable, i already solved it by adding alpha channel to vtkimagedata , and it worked, i appreciate it, i will not mark it solved unless i test your solution. thanks. – Error May 28 '21 at 20:35
  • That's another way of doing it. I use the above approach in many situations, where I need to make a screen dump without a cursor object or some annotations. The `Update` triggers a render call, so no additional render calls are needed – Jens Munk May 28 '21 at 20:42