vtkImageTracerWidget not receiving events on Mac (pyQt5). bug?

Hi,
I posted this in support last week but with no luck. I am re-posting here as this is a potential bug based on differing results on Linux/Mac and with and without PyQt rendering.

I have trouble with vtkImageTracerWidget on Mac. The code below shows an image (zeros) and a vtkImageTracerWidget is active on to (so you can draw an outline). This works on Linux, but on Mac the ImagetracerWidget appears to not receive the mouse events and the mouse just adjusts window level in the image as if the ImageTracerWidget was not enabled.
It may be related to PyQt since it does work for Mac without Qt - using just vtk.vtkRenderWindow().

I was hoping that someone would have an idea of how to troubleshoot further!
Thanks!
Soren

#!/usr/bin/env python3
import vtk
import numpy as np
from PyQt5.QtWidgets import QApplication,  QMainWindow, QFrame, QVBoxLayout
import sys
from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
from vtk.util import numpy_support


class MainWindow(QMainWindow):
    def __init__(self, parent=None):
            QMainWindow.__init__(self, parent)

            self.frame = QFrame()
            self.vl = QVBoxLayout()
            self.vtkWidget = QVTKRenderWindowInteractor(self)
            self.vl.addWidget(self.vtkWidget)
            self.frame.setLayout(self.vl)
            self.setCentralWidget(self.frame)

            self.ren = vtk.vtkRenderer()
            self.ren.SetBackground(0., .0, 1)
            self.ren.GetActiveCamera().SetPosition(0, 0, 1)
            self.ren.GetActiveCamera().SetViewUp(0, 1, 0)

            self.vtkWidget.GetRenderWindow().AddRenderer(self.ren)
            self.iren = self.vtkWidget.GetRenderWindow().GetInteractor()
            style = vtk.vtkInteractorStyleImage()
            style.SetInteractionModeToImage3D()
            self.iren.SetInteractorStyle(style)

            self.imageVTK = vtk.vtkImageData()
            self.imageactor = vtk.vtkImageActor()
            self.imageactor.SetInputData(self.imageVTK)
            #
            self.imageVTK.SetOrigin((0, 0, 0))
            self.imageVTK.SetSpacing(1, 1, 1)

            rgb_data=np.zeros((256,256,3),np.uint8)
            self.imageVTK.SetDimensions((rgb_data.shape[1], rgb_data.shape[0], 1))
            self.montage_VTKdata = numpy_support.numpy_to_vtk(num_array=np.flip(rgb_data, axis=0).ravel(),
                                                              deep=True)
            self.montage_VTKdata.SetNumberOfComponents(3)
            self.imageVTK.GetPointData().SetScalars(self.montage_VTKdata)

            self.ren.AddActor(self.imageactor)

            self.tracer = vtk.vtkImageTracerWidget()
            self.tracer.GetLineProperty().SetLineWidth(1)
            self.tracer.GetGlyphSource().SetRotationAngle(45.0)
            self.tracer.GetGlyphSource().SetScale(3.0)
            self.tracer.GetGlyphSource().Modified()
            self.tracer.SetViewProp(self.imageactor)
            self.tracer.SetCaptureRadius(15.0)
            self.tracer.SetInputData(self.imageVTK)
            self.tracer.ProjectToPlaneOn()
            self.tracer.SnapToImageOff()
            self.tracer.AutoCloseOn()
            self.tracer.SetInteractor(self.iren)
            self.tracer.PlaceWidget()
            self.tracer.On()


            self.imageactor.Update()
            self.ren.ResetCamera()
            self.show()
            self.iren.Initialize()
            self.iren.Start()

if __name__ == "__main__":

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

Hi Soren, welcome to the forum.

I tried your example code on my own MacBook, and the drawing functionality of vtkImageTracerWidget worked fine. Can you give more details about your system?

I am using a recent checkout of the VTK master branch (late 2020), and python3.7 (from the python.org package), on macOS Catalina.

Can you try the QVTKRenderWindowInteractor from the master branch to see if it fixes your issue?

Hi David,
Thanks for your help!
I built the head of the master branch but the issue seems to persist. I do see the little dot in the middle of the image and can turn it on/off with “i”, but left clicking only engages window level control, not drawing.
I am on an older Mac OS: Mojave 10.14.6 and use python 3.7.6.
My previous attempt was with vtk via pip. I uninstalled this, confirmed it was not loading and then set the PYTHONPATH to the newly built packages.

Do you think it can be related to Mojave? I just don’t want to upgrade to Big Sur as Horos apparently is not playing well with that. I could try Catalina if you think that is likely to solve it.
Could it be PyQt related at all? Which version are you using there?

Thanks again!
Soren

I’m using PyQt5-5.15.2.

This seems to be a retina issue. Yesterday when it was working fine for me, I was using an external monitor. Today, I tried on my laptop screen and I see the same problem as you: trying to draw causes window/level.

This seems to be a problem with QVTKRenderWindowInteractor.py itself. If I change its _setEventInformation() method and set scale=1 (instead of using _getPixelRatio()), then the interaction works.

Hi David,
Yes allows the ROI to be drawn - thanks!
I replaced the function on load rather than altering the source - I hope the effect should be the same.
It does seem there is a problem with auto-close of the outline now, but I will investigate in more detail before adapting the minimal example and re-posting if it turns out to not work.

Thanks in any case!
Soren

def _setEventInformation(self, x, y, ctrl, shift,
                         key, repeat=0, keysum=None):
    scale = 1
    self._Iren.SetEventInformation(int(round(x * scale)),
                                   int(round((self.height() - y - 1) * scale)),
                                   ctrl, shift, key, repeat, keysum)

setattr(QVTKRenderWindowInteractor, "_setEventInformation", _setEventInformation)
1 Like

Just following up - the closed events works fine too!

Thanks for the fix!
Soren