栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Slicer学习笔记(十七)编写一个python扩展模块

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Slicer学习笔记(十七)编写一个python扩展模块

Slicer学习笔记 十七 编写一个python扩展模块 1、安装并打开slicer2、选择扩展向导3、选择创建扩展4、增加python扩展模块5、编写自己的python扩展6、修改python脚本

1、安装并打开slicer

2、选择扩展向导

3、选择创建扩展


先起个名字 practice1 并选定一个保存路径。

此时在指定路径下生成两个文件。

并弹出一个对话窗口 选择 OK 就可以。

之后slicer 左侧界面如下

4、增加python扩展模块

选择Add Module to Extension 弹出右侧窗口 名字应该起一个有含义的 表示扩展的作用 Type 类型 scripted 对应python 脚本 loadable 代表c 扩展 其他类型我暂时还没有了解。此处为写示例 名称python_test1 ,type 为scripted。命名好后选择OK。

弹出如下界面

选择Yes.
此时文件夹下多了一个文件夹 slicer界面左侧显示也多了一个文件夹


此时可以看到在Modules选项下 Examples类别下已经有自己新添加了python_test1 模块了。

选择该模块后界面如下 这个是模板给我们自动生成的

5、编写自己的python扩展

可以分别点选Edit编辑python脚本 点选Edit UI 编辑 UI。

选择Edit时可能并不能自动打开关联的代码 此时去 D:documentsslicerpractice1python_test1文件夹下 找到python_test1.py 右键– 属性– 打开方式 选择你认为适合自己的打开软件 此处我选择的是pycharm 也可以选择notepad 、vscode等适合编辑python脚本的编辑器。


Edit UI 打开的是文件夹
D:documentsslicerpractice1python_test1ResourcesUI下的python_test1.ui 文件 关联的应用是Qt Designer。


从上图可以看到Apply按钮是灰色的 即不能使用 正常情况应该是要么需要自己手动编辑属性 使案件可以使用 要么需要触发条件 满足条件才会显示黑色文字的 使能状态。

执行默认测试程序 发现默认测试程序是一个配准的程序。

6、修改python脚本

找到

def onApplyButton(self):

注释掉该函数原来的内容。

将函数内容替换成https://www.slicer.org/wiki/documentation/Nightly/Developers/Charts 中的脚本

import slicer
import math
# Switch to a layout (24) that contains a Chart View to initiate the construction of the widget and Chart View Node
lns slicer.mrmlScene.GetNodesByClass( vtkMRMLLayoutNode )
lns.InitTraversal()
ln lns.GetNextItemAsObject()
ln.SetViewArrangement(24)
# Get the Chart View Node
cvns slicer.mrmlScene.GetNodesByClass( vtkMRMLChartViewNode )
cvns.InitTraversal()
cvn cvns.GetNextItemAsObject()
# Create an Array Node and add some data
dn slicer.mrmlScene.AddNode(slicer.vtkMRMLDoubleArrayNode())
a dn.GetArray()
a.SetNumberOfTuples(600)
x range(0, 600)
for i in range(len(x)):
 a.SetComponent(i, 0, x[i]/50.0)
 a.SetComponent(i, 1, math.sin(x[i]/50.0))
 a.SetComponent(i, 2, 0)
# Create a second Array node
dn2 slicer.mrmlScene.AddNode(slicer.vtkMRMLDoubleArrayNode())
a dn2.GetArray()
a.SetNumberOfTuples(600)
x range(0, 600)
for i in range(len(x)):
 a.SetComponent(i, 0, x[i]/50.0)
 a.SetComponent(i, 1, math.cos(x[i]/50.0))
 a.SetComponent(i, 2, 0)
# Create a Chart Node.
cn slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode())
# Add the Array Nodes to the Chart. The first argument is a string used for the legend and to refer to the Array when setting properties.
cn.AddArray( A double array , dn.GetID())
cn.AddArray( Another double array , dn2.GetID())
# Set a few properties on the Chart. The first argument is a string identifying which Array to assign the property. 
# default is used to assign a property to the Chart itself (as opposed to an Array Node).
cn.SetProperty( default , title , A simple chart with 2 curves )
cn.SetProperty( default , xAxisLabel , Something in x )
cn.SetProperty( default , yAxisLabel , Something in y )
# Tell the Chart View which Chart to display
cvn.SetChartNodeID(cn.GetID())

选择 Thresholded volume 创建volume后下面的Apply 按钮才切换为 使能状态 然后单击 Apply, 生成右侧的正弦和余弦波形图。
需要注意的是 查看一下代码看一下哪里控制了Apply的使能状态 然后根据自己的需要控制使能状态。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/267919.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号