# the speed is too slow for vtkWindowToImageFilter, how to speed it?

**URL:** https://discourse.vtk.org/t/the-speed-is-too-slow-for-vtkwindowtoimagefilter-how-to-speed-it/2396
**Category:** Support
**Created:** [January 9, 2020, 1:49pm UTC](https://discourse.vtk.org/t/the-speed-is-too-slow-for-vtkwindowtoimagefilter-how-to-speed-it/2396 "2020-01-09T13:49:48Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![zhang-qiang-github](https://discourse.vtk.org/user_avatar/discourse.vtk.org/zhang-qiang-github/32/2519_2.png) [@zhang-qiang-github](https://discourse.vtk.org/u/zhang-qiang-github)
#### Post date: [January 9, 2020, 1:49pm UTC](https://discourse.vtk.org/t/the-speed-is-too-slow-for-vtkwindowtoimagefilter-how-to-speed-it/2396/1 "2020-01-09T13:49:48Z")

</div>

Currently, we need to use vtk in my project. We want to obtain the off sreen rendering picture and show it. We use vtkWindowToImageFilter to obtain the rendering picture. However, we find it’s too slow. My code is:

```auto
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys, os
import numpy as np
import vtk, qimage2ndarray

class vtkLabel(QLabel):
    def __init__ (self):
        super(vtkLabel, self). __init__ ()
        cone = vtk.vtkConeSource()
        mapper = vtk.vtkPolyDataMapper()
        mapper.SetInputConnection(cone.GetOutputPort())
        actor = vtk.vtkActor()
        actor.SetMapper(mapper)

        ren = vtk.vtkRenderer()
        ren.AddActor(actor)
        renWin = vtk.vtkRenderWindow()
        renWin.AddRenderer(ren)
        renWin.SetOffScreenRendering(1)
        imageFilter = vtk.vtkWindowToImageFilter()
        imageFilter.SetInput(renWin)

        self.ren = ren
        self.renWin = renWin
        self.imageFilter = imageFilter

    def resizeEvent(self, QMouseEvent):
        super(vtkLabel, self).resizeEvent(QMouseEvent)
        print(self.width(), self.height())
        self.renWin.SetSize(self.width(), self.height())
        self.renWin.Render()
        self.imageFilter.Modified()
        self.imageFilter.Update()
        displayImg = self.imageFilter.GetOutput()

        dims = displayImg.GetDimensions()
        from vtk.util.numpy_support import vtk_to_numpy
        numImg = vtk_to_numpy(displayImg.GetPointData().GetScalars())
        numImg = numImg.reshape(dims[1], dims[0], 3)
        numImg = numImg.transpose(0, 1, 2)
        numImg = np.flipud(numImg)

        displayQImg = qimage2ndarray.array2qimage(numImg)
        pixmap = QPixmap.fromImage(displayQImg)
        self.pixmap = pixmap

    def paintEvent(self, QPaintEvent):
        super(vtkLabel, self).paintEvent(QPaintEvent)
        painter = QPainter(self)
        width = self.width()
        height = self.height()
        painter.drawPixmap(QPoint(0, 0), self.pixmap, QRect(0, 0, width, height))

class MainWindow(QMainWindow):
    def __init__ (self, parent=None):
        super(MainWindow, self). __init__ (parent)
        self.resize(500,500)
        self.imageLabel = vtkLabel()
        self.setCentralWidget(self.imageLabel)

app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()

```

In this code, we find the speed is slow when we change the window size. And the weird thing is that the speed would be fast if I delete/comment the code “renWin.SetOffScreenRendering(1)”.

How can I speed up this code? Any suggestion is appreciated.

---

<div class="post-metadata">

### Author: ![Alex1](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/a/898d66/32.png) [@Alex1](https://discourse.vtk.org/u/Alex1)
#### Post date: [January 9, 2020, 4:09pm UTC](https://discourse.vtk.org/t/the-speed-is-too-slow-for-vtkwindowtoimagefilter-how-to-speed-it/2396/2 "2020-01-09T16:09:24Z")

</div>

As far as I know OffScreenRendering is very slow because it renders without hardware acceleration only on the CPU and not on the GPU. So far I have not seen a way to make OffScreenRendering faster.

---

<div class="post-metadata">

### Author: ![Michael](https://discourse.vtk.org/user_avatar/discourse.vtk.org/michael/32/16_2.png) [@Michael](https://discourse.vtk.org/u/Michael)
#### Post date: [January 9, 2020, 5:16pm UTC](https://discourse.vtk.org/t/the-speed-is-too-slow-for-vtkwindowtoimagefilter-how-to-speed-it/2396/3 "2020-01-09T17:16:15Z")

</div>

What are you trying to achieve?  
It looks that you are copying the result of VTK to a QT widget.  
You might be interested by QVTKOpenGLNativeWidget

---

<div class="post-metadata">

### Author: ![zhang-qiang-github](https://discourse.vtk.org/user_avatar/discourse.vtk.org/zhang-qiang-github/32/2519_2.png) [@zhang-qiang-github](https://discourse.vtk.org/u/zhang-qiang-github)
#### Post date: [January 10, 2020, 12:37am UTC](https://discourse.vtk.org/t/the-speed-is-too-slow-for-vtkwindowtoimagefilter-how-to-speed-it/2396/4 "2020-01-10T00:37:42Z")

</div>

But why “renWin.SetOffScreenRendering(1)” make so much difference? Have you tried my code? The code without renWin.SetOffScreenRendering(1) would be much faster than that with renWin.SetOffScreenRendering(1).

---

<div class="post-metadata">

### Author: ![zhang-qiang-github](https://discourse.vtk.org/user_avatar/discourse.vtk.org/zhang-qiang-github/32/2519_2.png) [@zhang-qiang-github](https://discourse.vtk.org/u/zhang-qiang-github)
#### Post date: [January 10, 2020, 12:38am UTC](https://discourse.vtk.org/t/the-speed-is-too-slow-for-vtkwindowtoimagefilter-how-to-speed-it/2396/5 "2020-01-10T00:38:18Z")

</div>

Yes, I want to embed VTK into QT. However, I don’t want to a QVTK class, because QT is more convenient to add more complicate operation than VTK

---

<div class="post-metadata">

### Author: ![Michael](https://discourse.vtk.org/user_avatar/discourse.vtk.org/michael/32/16_2.png) [@Michael](https://discourse.vtk.org/u/Michael)
#### Post date: [January 10, 2020, 3:21pm UTC](https://discourse.vtk.org/t/the-speed-is-too-slow-for-vtkwindowtoimagefilter-how-to-speed-it/2396/6 "2020-01-10T15:21:29Z")

</div>

Using OffScreen framebuffers is not necessarily slower. But using vtkWindowToImageFilter is slow because you will copy VTK result from GPU memory to CPU memory and it will affect badly the performance.  
Running complicated operation to the framebuffer as a post processing pass can be done very efficiently on the GPU using `vtkRenderPass` but you will need basic OpenGL knowledge.

---

<div class="post-metadata">

### Author: ![zhang-qiang-github](https://discourse.vtk.org/user_avatar/discourse.vtk.org/zhang-qiang-github/32/2519_2.png) [@zhang-qiang-github](https://discourse.vtk.org/u/zhang-qiang-github)
#### Post date: [January 10, 2020, 4:56pm UTC](https://discourse.vtk.org/t/the-speed-is-too-slow-for-vtkwindowtoimagefilter-how-to-speed-it/2396/7 "2020-01-10T16:56:06Z")

</div>

> [@Michael](#):
>
> `vtkRenderPass`

Could you please provide an example to use vtkRenderPass?

---

<div class="post-metadata">

### Author: ![Michael](https://discourse.vtk.org/user_avatar/discourse.vtk.org/michael/32/16_2.png) [@Michael](https://discourse.vtk.org/u/Michael)
#### Post date: [January 13, 2020, 12:29pm UTC](https://discourse.vtk.org/t/the-speed-is-too-slow-for-vtkwindowtoimagefilter-how-to-speed-it/2396/8 "2020-01-13T12:29:08Z")

</div>

`vtkToneMappingPass`  
You can also read the test to understand how to enable render passes: `TestToneMappingPass`

---

<div class="post-metadata">

### Author: ![zhang-qiang-github](https://discourse.vtk.org/user_avatar/discourse.vtk.org/zhang-qiang-github/32/2519_2.png) [@zhang-qiang-github](https://discourse.vtk.org/u/zhang-qiang-github)
#### Post date: [January 13, 2020, 12:44pm UTC](https://discourse.vtk.org/t/the-speed-is-too-slow-for-vtkwindowtoimagefilter-how-to-speed-it/2396/9 "2020-01-13T12:44:04Z")

</div>

Thank you very much. I can understand the render pass, but I don’t know how to get the picture in render window by using vtkRenderPass. Could you please provide a demo?

---

<div class="post-metadata">

### Author: ![Michael](https://discourse.vtk.org/user_avatar/discourse.vtk.org/michael/32/16_2.png) [@Michael](https://discourse.vtk.org/u/Michael)
#### Post date: [January 13, 2020, 12:46pm UTC](https://discourse.vtk.org/t/the-speed-is-too-slow-for-vtkwindowtoimagefilter-how-to-speed-it/2396/10 "2020-01-13T12:46:48Z")

</div>

```
vtkNew<vtkRenderer> renderer;

// custom passes
vtkNew<vtkCameraPass> cameraP;
vtkNew<vtkSequencePass> seq;
vtkNew<vtkOpaquePass> opaque;
vtkNew<vtkLightsPass> lights;

vtkNew<vtkRenderPassCollection> passes;
passes->AddItem(lights);
passes->AddItem(opaque);
seq->SetPasses(passes);
cameraP->SetDelegatePass(seq);

vtkNew<vtkToneMappingPass> toneMappingP;
toneMappingP->SetDelegatePass(cameraP);

// The following code set the render pass to the renderer
vtkOpenGLRenderer::SafeDownCast(renderer)->SetPass(toneMappingP);

renWin->AddRenderer(renderer);
```

---

<div class="post-metadata">

### Author: ![zhang-qiang-github](https://discourse.vtk.org/user_avatar/discourse.vtk.org/zhang-qiang-github/32/2519_2.png) [@zhang-qiang-github](https://discourse.vtk.org/u/zhang-qiang-github)
#### Post date: [January 13, 2020, 1:07pm UTC](https://discourse.vtk.org/t/the-speed-is-too-slow-for-vtkwindowtoimagefilter-how-to-speed-it/2396/11 "2020-01-13T13:07:16Z")

</div>

The code is clear. But how can I capture the picture from render window?

---

<div class="post-metadata">

### Author: ![Michael](https://discourse.vtk.org/user_avatar/discourse.vtk.org/michael/32/16_2.png) [@Michael](https://discourse.vtk.org/u/Michael)
#### Post date: [January 13, 2020, 1:08pm UTC](https://discourse.vtk.org/t/the-speed-is-too-slow-for-vtkwindowtoimagefilter-how-to-speed-it/2396/12 "2020-01-13T13:08:50Z")

</div>

What do you mean by _capture the picture_?

---

<div class="post-metadata">

### Author: ![zhang-qiang-github](https://discourse.vtk.org/user_avatar/discourse.vtk.org/zhang-qiang-github/32/2519_2.png) [@zhang-qiang-github](https://discourse.vtk.org/u/zhang-qiang-github)
#### Post date: [January 13, 2020, 1:13pm UTC](https://discourse.vtk.org/t/the-speed-is-too-slow-for-vtkwindowtoimagefilter-how-to-speed-it/2396/13 "2020-01-13T13:13:25Z")

</div>

> [@zhang-qiang-github](#):
>
> def resizeEvent(self, QMouseEvent):  
> super(vtkLabel, self).resizeEvent(QMouseEvent) print(self.width(), self.height())  
> self.renWin.SetSize(self.width(), self.height()) self.renWin.Render()  
> self.imageFilter.Modified() self.imageFilter.Update() displayImg =  
> self.imageFilter.GetOutput()

Just like those code in the resizeEvent, when the size of window is change, I want to obtain the picture in render window and then show it in the QT window.  
In summary, I want to use the VTK for render, and use QT for all operation.

---

<div class="post-metadata">

### Author: ![Michael](https://discourse.vtk.org/user_avatar/discourse.vtk.org/michael/32/16_2.png) [@Michael](https://discourse.vtk.org/u/Michael)
#### Post date: [January 13, 2020, 1:21pm UTC](https://discourse.vtk.org/t/the-speed-is-too-slow-for-vtkwindowtoimagefilter-how-to-speed-it/2396/14 "2020-01-13T13:21:09Z")

</div>

If you want to keep using QT to modify the VTK image, you will have to accept the performance issues.  
If the performances are a real issue, consider using render passes GPU shaders instead of QT to modify the image.

---

<div class="post-metadata">

### Author: ![zhang-qiang-github](https://discourse.vtk.org/user_avatar/discourse.vtk.org/zhang-qiang-github/32/2519_2.png) [@zhang-qiang-github](https://discourse.vtk.org/u/zhang-qiang-github)
#### Post date: [January 14, 2020, 1:05pm UTC](https://discourse.vtk.org/t/the-speed-is-too-slow-for-vtkwindowtoimagefilter-how-to-speed-it/2396/15 "2020-01-14T13:05:18Z")

</div>

Have you run the posted code? Could you please explain why renWin.SetOffScreenRendering(1) will make so large difference?

---

<div class="post-metadata">

### Author: ![Eric\_Baxter](https://discourse.vtk.org/user_avatar/discourse.vtk.org/eric_baxter/32/4380_2.png) [@Eric\_Baxter](https://discourse.vtk.org/u/Eric_Baxter)
#### Post date: [December 7, 2021, 6:27pm UTC](https://discourse.vtk.org/t/the-speed-is-too-slow-for-vtkwindowtoimagefilter-how-to-speed-it/2396/16 "2021-12-07T18:27:47Z")

</div>

Hi, Michael, it sounds like I am trying to do something similar. I need to get the RGBA data from the renderer to use in other code downstream. It sounds like the performance is limited by the copy performance from GPU to CPU memory. The performance I’m currently getting is too slow. Are there any alternatives? I’ve tried using GetRGBACharPixelData() on the window but the data is not yielding the upsampling that would be applied by the WindowToImageFilter. Is there an easy way to tell VTK to use CPU only for certain things? Thanks for your help.
