-
Notifications
You must be signed in to change notification settings - Fork 1
/
slicer1.py
124 lines (102 loc) · 3.67 KB
/
slicer1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import numpy as np
from fos import Actor, Scene, Window
from fos.actor.tex3d import Texture3D
from fos.actor.axes import Axes
from pyglet.gl import *
class Slicer(Actor):
def __init__(self, name, data, affine=None):
""" Volume Slicer
Parameters
----------
name : str
data : array, (X, Y, Z) or (X, Y, Z, 3) or (X, Y, Z, 4)
affine : not implemented (default None)
Notes
------
Coordinate Systems
http://www.grahamwideman.com/gw/brain/orientation/orientterms.htm
http://www.slicer.org/slicerWiki/index.php/Coordinate_systems
"""
self.name = name
self.data = data
self.affine = affine
self.tex = Texture3D('Buzz', self.data, self.affine,
type=GL_RGBA, interp=GL_LINEAR)
self.vertices = self.tex.vertices
self.visible = True
self.I, self.J, self.K = self.data.shape[:3]
self.i, self.j, self.k = self.I/2, self.J/2, self.K/2
self.texcoords_i, self.vertcoords_i = self.tex.slice_i(self.i)
self.texcoords_j, self.vertcoords_j = self.tex.slice_j(self.j)
self.texcoords_k, self.vertcoords_k = self.tex.slice_k(self.k)
self.show_i = True
self.show_j = True
self.show_k = True
def slice_i(self, i):
self.i = i
self.texcoords_i, self.vertcoords_i = self.tex.slice_i(i)
self.draw()
def slice_j(self, j):
self.j = j
self.texcoords_j, self.vertcoords_j = self.tex.slice_j(j)
self.draw()
def slice_k(self, k):
self.k = k
self.texcoords_k, self.vertcoords_k = self.tex.slice_k(k)
self.draw()
def draw(self):
if self.show_i:
#i Slice
glPushMatrix()
glRotatef(90, 0., 1., 0)
glRotatef(90, 0., 0., 1.)
self.tex.update_quad(self.texcoords_i, self.vertcoords_i)
self.tex.set_state()
self.tex.draw()
self.tex.unset_state()
glPopMatrix()
#j Slice
if self.show_j:
glPushMatrix()
glRotatef(180, 0., 1., 0) # added for fsl convention
glRotatef(90, 0., 0., 1.)
self.tex.update_quad(self.texcoords_j, self.vertcoords_j)
self.tex.set_state()
self.tex.draw()
self.tex.unset_state()
glPopMatrix()
if self.show_k:
#k Slice
glPushMatrix()
glRotatef(90, 1., 0, 0.)
glRotatef(90, 0., 0., 1)
glRotatef(180, 1., 0., 0.) # added for fsl
self.tex.update_quad(self.texcoords_k, self.vertcoords_k)
self.tex.set_state()
self.tex.draw()
self.tex.unset_state()
glPopMatrix()
# def process_keys(self,symbol,modifiers):
#
# #NEEDS to change
# if modifiers & key.MOD_SHIFT:
# print 'Shift'
if __name__ == '__main__':
import nibabel as nib
dname = '/usr/share/fsl/data/standard/'
fname = dname + 'FMRIB58_FA_1mm.nii.gz'
img=nib.load(fname)
data = img.get_data()
data = np.interp(data, [data.min(), data.max()], [0, 255])
#data = np.ones((156, 122, 96), dtype=np.ubyte)
#data = 255*data
#data[70:100, 70:90, 70:80 ] = 140
window = Window(caption="Interactive Slicer",
bgcolor=(0.4, 0.4, 0.9))
scene = Scene(activate_aabb=False)
slicer = Slicer('VolumeSlicer', data)
scene.add_actor(slicer)
window.add_scene(scene)
window.refocus_camera()
#ax = Axes(name="3 axes", scale=200, linewidth=10.0)
#scene.add_actor(ax)