九月 10

SimpleITK无法调用ImageJ/Fiji的问题

原因猜测:因为地址中有一个空格,所以导致无法识别。

测试经过:尝试过给路径加引号,但是并不成功

解决方法:

  • 第一步:在环境变量中新建变量,变量名为SITK_SHOW_COMMAND,变量值为D:\Program Files\Fiji.app(修改为自己的目录)

  • 第二步:在path中添加%SITK_SHOW_COMMAND%

  • 第三步:重启Jupyter notebook就生效

解决方案引用自:https://blog.csdn.net/qq_32384313/article/details/80234732

八月 21

当PyQt和Python VTK一起使用时出现wglMakeCurrent failed in MakeCurrent(), error: The handle is invalid错误

我使用Pyqt和Python VTK一起编程时,在程序上面加了两个Qvtkwidget,通过python,一个用来显示左边的CT横断面,另一个用来显示CT三维切片,其实就是显示两幅不同的图。然后读取和显示都正常,但是一关闭界面的时候就提示这个错误。折腾好久之后,并不是什么python和qt之间乱七八糟的机制的问题。

解决方案:在UI界面关闭时,加个响应函数。

def closeEvent(self, event):
    self.qvtkWidget2D.Finalize()
    self.qvtkWidget2D_2.Finalize()

这样便可解决弹窗报错的问题。当然只是针对这个问题

八月 5

python VTK 笔记:几何的旋转和平移

在python vtk中,我们可以使用transform进行转换,也可以直接使用actor.SetUserMatrix方法对原几何进行旋转和平移

import vtk
import numpy as np

# create two cones, rotate one of them.

# create a rendering window and renderer
ren = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)
WIDTH = 640
HEIGHT = 480
renWin.SetSize(WIDTH, HEIGHT)

# create a renderwindowinteractor
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)

# create cone
source = vtk.vtkConeSource()
source.SetResolution(60)
source.SetCenter(-2, 0, 0)

# create a transform that rotates the cone
transform = vtk.vtkTransform()

matrix = np.array([[1, 0, 0, 0],
                   [0, 1, 0, 0],
                   [0, 0, 1, 2],
                   [0, 0, 0, 1]])
obj_mat = vtk.vtkMatrix4x4()
for i in range(4):
    for j in range(4):
        obj_mat.SetElement(i, j, matrix[i, j])

transform.SetMatrix(obj_mat)
transformFilter = vtk.vtkTransformPolyDataFilter()
transformFilter.SetTransform(transform)
transformFilter.SetInputConnection(source.GetOutputPort())
transformFilter.Update()

# mapper for original cone
coneMapper1 = vtk.vtkPolyDataMapper()
coneMapper1.SetInputConnection(source.GetOutputPort())

# another mapper for the rotated cone
coneMapper2 = vtk.vtkPolyDataMapper()
coneMapper2.SetInputConnection(transformFilter.GetOutputPort())

matrix2 = np.array([[0, 1, 0, 0],
                   [1, 0, 0, 0],
                   [0, 0, 1, 0],
                   [0, 0, 0, 1]])
obj_mat2 = vtk.vtkMatrix4x4()
for i in range(4):
    for j in range(4):
        obj_mat2.SetElement(i, j, matrix2[i, j])
# actor for original cone
actor1 = vtk.vtkActor()

# 直接使用变换矩阵进行旋转和平移
actor1.SetUserMatrix(obj_mat2)
actor1.SetMapper(coneMapper1)

# actor for rotated cone
actor2 = vtk.vtkActor()
actor2.SetMapper(coneMapper2)

# color the original cone red
actor1.GetProperty().SetColor(1, 0, 0)  # (R,G,B)
# color rotated cone blue
actor2.GetProperty().SetColor(0, 0, 1)  # (R,G,B)

# assign actor to the renderer
ren.AddActor(actor1)
ren.AddActor(actor2)

# enable user interface interactor
iren.Initialize()
renWin.Render()
iren.Start()
八月 5

python VTK 笔记: vtkOrientationMarkerWidget 不显示的问题

值得注意的是,vtkOrientationMarkerWidget必须实例化给实例变量,换句话说

axes = vtk.vtkOrientationMarkerWidget() # 会导致不显示
self.axes = vtk.vtkOrientationMarkerWidget() # 会正常显示

一个完整的代码案例:

import sys
import vtk
from PySide2 import QtCore, QtWidgets
from vtkmodules.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        QtWidgets.QMainWindow.__init__(self, parent)
        self.frame = QtWidgets.QFrame()
        self.vl = QtWidgets.QVBoxLayout()
        self.vtkWidget = QVTKRenderWindowInteractor(self.frame)
        self.vl.addWidget(self.vtkWidget)
        self.ren = vtk.vtkRenderer()
        self.vtkWidget.GetRenderWindow().AddRenderer(self.ren)
        self.iren = self.vtkWidget.GetRenderWindow().GetInteractor()

        # Create source
        source = vtk.vtkSphereSource()
        source.SetCenter(0, 0, 0)
        source.SetRadius(5.0)

        # Create a mapper
        mapper = vtk.vtkPolyDataMapper()
        mapper.SetInputConnection(source.GetOutputPort())

        # Create an actor
        actor = vtk.vtkActor()
        actor.SetMapper(mapper)
        self.ren.AddActor(actor)

        # Add axes
        axesActor = vtk.vtkAxesActor()
        self.axes = vtk.vtkOrientationMarkerWidget()
        self.axes.SetOrientationMarker(axesActor)
        self.axes.SetInteractor(self.iren)
        self.axes.EnabledOn()
        self.axes.InteractiveOn()
        self.ren.ResetCamera()
        self.frame.setLayout(self.vl)
        self.setCentralWidget(self.frame)
        self.show()
        self.iren.Initialize()

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    window = MainWindow()
    sys.exit(app.exec_())