为什么这个vtk-PyQt代码显示分段错误?

时间:2017-02-20 10:52:44

标签: python pyqt4 visualization vtk

#!/usr/bin/env python

import sys
import vtk
from PyQt4 import QtCore, QtGui
from vtk.qt4.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
import pickle
import numpy as np
from time import time
import matplotlib.pyplot as plt
import scipy as sp



class MainWindow(QtGui.QMainWindow):

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

    self.frame = QtGui.QFrame()

    self.vl = QtGui.QVBoxLayout()
    self.vtkWidget = QVTKRenderWindowInteractor(self.frame)
    self.vl.addWidget(self.vtkWidget)

    self.ren = vtk.vtkRenderer()
    self.renWin= self.vtkWidget.GetRenderWindow()
    self.renWin.AddRenderer(self.ren)
    self.iren =   self.renWin.GetInteractor()
    t0 = time()
    try:
        with open('/home/nxpd/VTK/Image35.pickle','rb') as readData:
            I = pickle.load(readData)

    except pickle.PickleError as pErr:
        print('Pickling error: '+ str(pErr))

    t1 = time()
    print "Time elapsed in pickle reading:  " , t1 - t0

    data_matrix = I
    #print data_matrix
    # For VTK to be able to use the data, it must be stored as a VTK-image. This can be done by the vtkImageImport-class which
    # imports raw data and stores it.
    dataImporter = vtk.vtkImageData()
    dataImporter.SetDimensions(728,728,35)
    data=vtk.vtkUnsignedCharArray()
    data.SetName('scalar')
    #data.SetNumberOfTuples(728*728*35)
    for k in range(35):
        for j in range(728):
            for i in range(728):
                data.InsertNextTuple1(data_matrix[i][j][k]/256)


    t2 = time()
    print "Time elapsed in writing to array: " , t2 - t1


    dataImporter.SetExtent(0, 727, 0,727,0,34)

    # The following class is used to store transparencyv-values for later retrieval. In our case, we want the value 0 to be
    # completly opaque whereas the three different cubes are given different transperancy-values to show how it works.
    alphaChannelFunc = vtk.vtkPiecewiseFunction()
    alphaChannelFunc.AddPoint(0, 0)
    alphaChannelFunc.AddPoint(53000/256, 0)
    alphaChannelFunc.AddPoint(53000/256+1, 1)
    alphaChannelFunc.AddPoint(65535/256, 1)

    # This class stores color data and can create color tables from a few color points. For this demo, we want the three cubes
    # to be of the colors red green and blue.
    colorFunc = vtk.vtkColorTransferFunction()
    colorFunc.AddRGBPoint(0, 0, 0, 0)
    colorFunc.AddRGBPoint(53000/256, 0,0,0)
    colorFunc.AddRGBPoint(53000/256+1, 1,1,1)
    colorFunc.AddRGBPoint(65535/256, 1, 1, 1)

    # The preavius two classes stored properties. Because we want to apply these properties to the volume we want to render,
    # we have to store them in a class that stores volume prpoperties.
    volumeProperty = vtk.vtkVolumeProperty()
    volumeProperty.SetColor(colorFunc)
    volumeProperty.SetScalarOpacity(alphaChannelFunc)

    # This class describes how the volume is rendered (through ray tracing).
    compositeFunction = vtk.vtkVolumeRayCastCompositeFunction()
    # We can finally create our volume. We also have to specify the data for it, as well as how the data will be rendered.
    volumeMapper = vtk.vtkVolumeRayCastMapper()
    volumeMapper.SetVolumeRayCastFunction(compositeFunction)
    volumeMapper.SetInputData(dataImporter)

    # The class vtkVolume is used to pair the preaviusly declared volume as well as the properties to be used when rendering that volume.
    volume = vtk.vtkVolume()
    volume.SetMapper(volumeMapper)
    volume.SetProperty(volumeProperty)
    t3 = time()
    print "Time elapsed in Setting parameters: " , t3 - t2

    # With almost everything else ready, its time to initialize the renderer and window, as well as creating a method for exiting the application
    #renderer = vtk.vtkRenderer()
    #renderWin = vtk.vtkRenderWindow()
    #renderWin.AddRenderer(renderer)
    #renderInteractor = vtk.vtkRenderWindowInteractor()
    #renderInteractor.SetRenderWindow(renderWin)

    # We add the volume to the renderer ...
    self.ren.AddVolume(volume)

    # ... set background color to white ...
    self.ren.SetBackground(0.5,0.5,0.7)
    # ... and set window size.
    self.renWin.SetSize(800,800)


    t4 = time()
    print "Time elapsed in rendering: " , t4 - t3



    # Tell the application to use the function as an exit check.
    self.renWin.AddObserver("AbortCheckEvent", exitCheck)

    self.iren.Initialize()
    # Because nothing will be rendered without any input, we order the first render manually before control is handed over to the main-loop.
    self.ren.Render()   
    self.ren.ResetCamera()

    self.iren.Start()


    t5 = time()
    print "Time elapsed in setting up: " , t5 - t4


if __name__ == "__main__":

     app = QtGui.QApplication(sys.argv)

     window = MainWindow()

     sys.exit(app.exec_())

我无法弄清楚为什么这段代码会向我显示分段错误。我使用了http://www.vtk.org/Wiki/VTK/Examples/Python/Widgets/EmbedPyQt中的示例来详细了解它的工作原理。

独立代码可以在不使用pyqt的情况下正常工作。

0 个答案:

没有答案