[vtkusers] VTK Displaying images shows just garbled pixels

j-P cervellone at gmail.com
Sun Jun 26 15:59:13 EDT 2016


Hello all,

I would like to use VTK to display some images but the code fails. 
The aim is to integrate the VTK code into a qt application. I downloaded the
samplecode from the website and tried to play with it
While the code using the inbuilt vtkImageViewer2  works (see code example
1), 
unfortunately the code in example 2 just shows garbled pixels. 

I know that in vtk the mapper and actor have to match but i cant figure out
which to use in what order. Example 1 is just to exclude that the ITK to VTK
bridge fails at some point

Thank you for any help you can spare

JP

#==================BEGIN EXAMPLE 1==================
#!/usr/bin/python
from itk import ITKVtkGlue
import vtk
import itk

itkType= itk.Image[itk.UC,2]

def toVTK(f):
    toVtk = ITKVtkGlue.ImageToVTKImageFilter[itkType].New()
    toVtk.SetInput(f)
    toVtk.Update()
    return toVtk.GetOutput()
    
def readSingleFile(filename="test.tif"):
    reader = itk.ImageFileReader[itkType].New()
    reader.SetFileName(filename)
    reader.Update()
    return reader.GetOutput()

f = readSingleFile()

viewer = vtk.vtkImageViewer2()
viewer.SetInputData(toVTK(f))
viewer.GetRenderWindow().SetSize(400,400)
viewer.GetRenderer().SetBackground(0.5,0.5,0.5)
viewInt = vtk.vtkRenderWindowInteractor()
viewer.SetupInteractor(viewInt)
viewer.Render()
viewInt.Start()
#================END EXAMPLE 1==========================



#=================BEGIN EXAMPLE 2======================
#!/usr/bin/env python
# Modified from the code contributed by Eric E Monson
 
from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import QApplication
import vtk
from vtk.qt4.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
import sys
import itk
from itk import ITKVtkGlue

itkType = itk.Image[itk.UC,2]
 
class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(603, 553)
        self.centralWidget = QtGui.QWidget(MainWindow)
        self.gridlayout = QtGui.QGridLayout(self.centralWidget)
        self.vtkWidget = QVTKRenderWindowInteractor(self.centralWidget)
        self.gridlayout.addWidget(self.vtkWidget, 0, 0, 1, 1)
        MainWindow.setCentralWidget(self.centralWidget)
 
class SimpleView(QtGui.QMainWindow):
 
    def __init__(self, parent = None):
        QtGui.QMainWindow.__init__(self, parent)
        f = self.readSingleFile("test.tif", itkType)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.ren = vtk.vtkRenderer()
        self.ui.vtkWidget.GetRenderWindow().AddRenderer(self.ren)
        self.iren = self.ui.vtkWidget.GetRenderWindow().GetInteractor()
        
        # Create a mapper
        mapper = vtk.vtkImageSliceMapper()#vtk.vtkPolyDataMapper()
       
mapper.SetInputData(self.toVTK(f))#SetInputConnection(source.GetOutputPort())
        mapper.Update()
        # Create an actor
        actor = vtk.vtkImageActor()
        actor.SetMapper(mapper)
        actor.Update()
 
        self.ren.AddActor(actor)
        
    def toVTK(self, f):
        toVtk = ITKVtkGlue.ImageToVTKImageFilter[itkType].New()
        toVtk.SetInput(f)
        toVtk.Update()
        return toVtk.GetOutput()
    
    def readSingleFile(self, filename, itkType):
        reader = itk.ImageFileReader[itkType].New()
        reader.SetFileName(filename)
        reader.Update()

        return reader.GetOutput()


def main():
    app = QtGui.QApplication(sys.argv)
    window = SimpleView()
    window.show()
    window.iren.Initialize() # Need this line to actually show the render
inside Qt
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()




--
View this message in context: http://vtk.1045678.n5.nabble.com/VTK-Displaying-images-shows-just-garbled-pixels-tp5738925.html
Sent from the VTK - Users mailing list archive at Nabble.com.


More information about the vtkusers mailing list