-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDirectVolRendering.py
87 lines (73 loc) · 2.72 KB
/
DirectVolRendering.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
import numpy as np
import time
# Ray casting
# Dakai Zhou
def TransferFunc1(vol, l1, l2, l3, l4, alpha1, alpha2, alpha3, alpha4, alpha5):
dim = np.shape(vol)
fres = np.zeros([dim[0], 4, dim[1], dim[2]])
res1 = np.zeros([dim[1], dim[2]])
res2 = np.zeros([dim[1], dim[2]])
res3 = np.zeros([dim[1], dim[2]])
res4 = np.zeros([dim[1], dim[2]])
idxall = np.ones([dim[1], dim[2]])
for i in range(dim[0]):
idx = vol[i, :, :] < l1
res1[idx] = 0
res2[idx] = 0
res3[idx] = 0
res4[idx] = alpha1
pidx = idx
idx = vol[i, :, :] < l2
res1[np.logical_xor(idx, pidx)] = 0
res2[np.logical_xor(idx, pidx)] = 0
res3[np.logical_xor(idx, pidx)] = 255
res4[np.logical_xor(idx, pidx)] = alpha2
pidx = idx
idx = vol[i, :, :] < l3
res1[np.logical_xor(idx, pidx)] = 0
res2[np.logical_xor(idx, pidx)] = 255
res3[np.logical_xor(idx, pidx)] = 0
res4[np.logical_xor(idx, pidx)] = alpha3
pidx = idx
idx = vol[i, :, :] < l4
res1[np.logical_xor(idx, pidx)] = 255
res2[np.logical_xor(idx, pidx)] = 0
res3[np.logical_xor(idx, pidx)] = 0
res4[np.logical_xor(idx, pidx)] = alpha4
pidx = idx
res1[np.logical_xor(idxall, pidx)] = 127
res2[np.logical_xor(idxall, pidx)] = 0
res3[np.logical_xor(idxall, pidx)] = 127
res4[np.logical_xor(idxall, pidx)] = alpha5
fres[i, 0, :, :] = res1
fres[i, 1, :, :] = res2
fres[i, 2, :, :] = res3
fres[i, 3, :, :] = res4
return fres
def Compositing1(trans_vol):
dim = np.shape(trans_vol)
res = np.zeros([dim[2], dim[3], dim[1]])
tmp_res = np.zeros([4, dim[2], dim[3]])
ps = np.zeros([dim[1], dim[2], dim[3]])
# stop condition
#stc = np.ones([1, dim[2], dim[3]]) * 0.95
for i in range(dim[0]):
s = trans_vol[i, :, :, :]
tmp_res[0:3, :, :] = ps[0:3, :, :] + (np.ones([dim[2], dim[3]]) - ps[3, :, :]) * s[3, :, :] * s[0:3, :, :]
tmp_res[3, :, :] = ps[3, :, :] + (np.ones([dim[2], dim[3]]) - ps[3, :, :]) * s[3, :, :]
ps = tmp_res
# acceleration
#stid = tmp_res[4, :, :] >= stc
#conid = tmp_res[4, :, :] < stc
for i in range(dim[1]):
res[:, :, i] = tmp_res[i, :, :]
return res
def DirectVolRendering1(vol, l1, l2, l3, l4, alpha1, alpha2, alpha3, alpha4, alpha5):
tic = time.clock()
trans_vol = TransferFunc1(vol, l1, l2, l3, l4, alpha1, alpha2, alpha3, alpha4, alpha5)
res = Compositing1(trans_vol)
dim = np.shape(res)
res[:, :, 3] = np.ones([dim[0], dim[1]]) - res[:, :, 3]
toc = time.clock()
print 'Time elspsed ', toc - tic
return res