From vtkRenderWindow to OpenCV Mat using python

Hi guys, I’ve tried many different ways to transform a vtkRenderWindow into an OpenCV Mat but I could get it and the python wrap documentation is not very solid (or am I missing some websites?).

Do you know how could I get that?

I tried something like this:

image_data=renWin.GetRGBAPixelData(0, 0, width-1, height-1, 1)

but returns null,

I tried with vtkWindowToImageFilter but the passage from window_to_image_filter.GetOutput() to numpy array or np.frombuffer fails everytime.

Can you please point me some other direction?

Hi,

The documentation highlights the unusual behavior of that class:

A vtkWindow doesn’t behave like other parts of the VTK pipeline: its modification time doesn’t get updated when an image is rendered. As a result, naive use of vtkWindowToImageFilter will produce an image of the first image that the window rendered, but which is never updated on subsequent window updates. This behavior is unexpected and in general undesirable.
To force an update of the output image, call vtkWindowToImageFilter’s Modified() method after rendering to the window.

(emphasis added)

Source: VTK: vtkWindowToImageFilter Class Reference

So, please, make sure you did as recommended in the docs.

I hope that helps,

Paulo