c++qtvtkqvtkwidget

how to extract the foreground from vtkWindowToImageFilter?


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?


Solution

  • 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.