-
Notifications
You must be signed in to change notification settings - Fork 1
/
slicer_for_fading.py
172 lines (135 loc) · 5.51 KB
/
slicer_for_fading.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import numpy as np
from fos import Actor, Scene, Window
#from fos.actor.tex3d import Texture3D
from tex3d_for_fading import Texture3D
from pyglet.gl import *
from fos.coords import (img_to_ras_coords,
img_to_ras_coords_container,
ras_to_las_coords,
img_to_tex_coords)
class Slicer(Actor):
def __init__(self, name, data, affine, convention='RAS', post_mat=None):
""" Volume Slicer
Parameters
----------
name : str
data : array, (X, Y, Z) or (X, Y, Z, 3) or (X, Y, Z, 4)
affine : array, shape (4, 4)
convention : str,
'LAS' or 'RAS'
post_mat : array, shape (4, 4)
"""
self.name = name
self.data = data
self.affine = affine
self.convention = convention
self.post_mat = post_mat
self.tex = Texture3D('Buzz', self.data, self.affine, interp='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.centershift = img_to_ras_coords(
np.array([[self.I/2., self.J/2., self.K/2.]]),
affine)
if self.convention == 'LAS':
self.centershift = ras_to_las_coords(self.centershift)
self.texcoords_i, self.vertcoords_i = self.slicecoords_i()
self.texcoords_j, self.vertcoords_j = self.slicecoords_j()
self.texcoords_k, self.vertcoords_k = self.slicecoords_k()
self.show_i = True
self.show_j = True
self.show_k = True
def img_to_tex_vert_coords(self, imgcoords):
vertcoords = img_to_ras_coords(imgcoords, self.affine)
if self.convention == 'LAS':
vertcoords = ras_to_las_coords(vertcoords)
texcoords = img_to_tex_coords(imgcoords, self.data.shape)
vertcoords = vertcoords - self.centershift
if self.post_mat is not None:
vertcoords = vertcoords.T
vertcoords1 = np.vstack((vertcoords,
np.ones(vertcoords.shape[1])))
vertcoords1 = np.dot(self.post_mat, vertcoords1)
vertcoords = vertcoords1[:-1, :].T
return texcoords, vertcoords
def slicecoords_i(self):
imgcoords = np.array([[self.i, 0, 0],
[self.i, 0, self.K],
[self.i, self.J, self.K],
[self.i, self.J, 0]], dtype='f8')
texcoords, vertcoords = self.img_to_tex_vert_coords(imgcoords)
return texcoords, vertcoords
def slicecoords_j(self):
imgcoords = np.array([[0, self.j, 0],
[0, self.j, self.K],
[self.I, self.j, self.K],
[self.I, self.j, 0]], dtype='f8')
texcoords, vertcoords = self.img_to_tex_vert_coords(imgcoords)
return texcoords, vertcoords
def slicecoords_k(self):
imgcoords = np.array([[0, 0, self.k],
[0, self.J, self.k],
[self.I, self.J, self.k],
[self.I, 0, self.k]], dtype='f8')
texcoords, vertcoords = self.img_to_tex_vert_coords(imgcoords)
return texcoords, vertcoords
def slice_i(self, i):
self.i = i
self.texcoords_i, self.vertcoords_i = self.slicecoords_i()
self.draw()
def slice_j(self, j):
self.j = j
self.texcoords_j, self.vertcoords_j = self.slicecoords_j()
self.draw()
def slice_k(self, k):
self.k = k
self.texcoords_k, self.vertcoords_k = self.slicecoords_k()
self.draw()
def draw(self):
if self.show_i:
glPushMatrix()
self.tex.update_quad(self.texcoords_i, self.vertcoords_i)
self.tex.set_state()
self.tex.draw()
self.tex.unset_state()
glPopMatrix()
if self.show_j:
glPushMatrix()
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:
glPushMatrix()
self.tex.update_quad(self.texcoords_k, self.vertcoords_k)
self.tex.set_state()
self.tex.draw()
self.tex.unset_state()
glPopMatrix()
if __name__ == '__main__':
import nibabel as nib
from fos import Window, Scene, Init, Run
#dname='/home/eg309/Data/trento_processed/subj_03/MPRAGE_32/'
#fname = dname + 'T1_flirt_out.nii.gz'
#dname = '/home/eg309/Data/111104/subj_05/'
#fname = dname + '101_32/DTI/fa.nii.gz'
dname = '/usr/share/fsl/data/standard/'
fname = dname + 'FMRIB58_FA_1mm.nii.gz'
#fname = '/home/eleftherios/Data/trento_processed/subj_01/MPRAGE_32/raw.nii'
img=nib.load(fname)
data = img.get_data()
data[np.isnan(data)] = 0
data = np.interp(data, [data.min(), data.max()], [0, 255])
data = data.astype(np.ubyte)
affine = img.get_affine()
print data.shape
Init()
window = Window(caption='[F]OS',bgcolor = (0.1, 0.2, 0.6))
scene = Scene(activate_aabb = False)
sli = Slicer('Volume Slicer', data, affine, convention='RAS')
scene.add_actor(sli)
window.add_scene(scene)
window.refocus_camera()
Run()