Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python VTKwindoiImageFilter的速度太慢,如何加速?_Python_Pyqt_Pyqt5_Vtk - Fatal编程技术网

Python VTKwindoiImageFilter的速度太慢,如何加速?

Python VTKwindoiImageFilter的速度太慢,如何加速?,python,pyqt,pyqt5,vtk,Python,Pyqt,Pyqt5,Vtk,目前,我们需要在我的项目中使用vtk。我们希望获得off-sreen渲染图片并显示它。我们使用vtkWindowToImageFilter获得渲染图片。然而,我们发现它太慢了。我的代码是: from PyQt5.QtWidgets import * from PyQt5.QtGui import * from PyQt5.QtCore import * import sys, os import numpy as np import vtk, qimage2ndarray class vtkL

目前,我们需要在我的项目中使用vtk。我们希望获得off-sreen渲染图片并显示它。我们使用vtkWindowToImageFilter获得渲染图片。然而,我们发现它太慢了。我的代码是:

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_()
在这段代码中,我们发现当我们改变窗口大小时速度很慢。奇怪的是,如果我删除/注释代码“renWin.SetOffScreenRendering(1)”,速度会很快


如何加速此代码?任何建议都将不胜感激。

您只是想将VTK嵌入QMain窗口吗?有一个合适的VTK类。我会贴一个例子,如果这是你想要的…非常感谢你的回复。是的,我想在QT中嵌入VTK。但是,我不想使用QVTK类,例如QVTKRenderWindowInteractor,因为QT比VTK更方便地添加更复杂的操作。您在考虑哪种更复杂的操作?您是指渲染中的功能,例如单击点和拖动位吗?或者你的意思是在渲染之外,比如添加可能改变图像的按钮和滑块?QVTKRenderWindowInteractor相当简单。你可以把它放进一个QWidget中,然后它就是正常的QT了……是的,拖动点或者其他东西。因此,我想用QT实现GUI操作,而VTK只用于渲染。我真的认为,在这种情况下,你是在试图重新发明轮子。你可以改变VTK鼠标交互的工作方式,给它一个不同的交互风格-默认的是非常可怕的。尝试将
iren.Start()
之前的
iren.setStylevtkInteractorStyleTrackballCamera())
添加到通用VTK示例中,看看这是否更适合您。