-
Notifications
You must be signed in to change notification settings - Fork 1
/
Local_MFS_3D.py
194 lines (137 loc) · 5.5 KB
/
Local_MFS_3D.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
from Algorithm import *
import numpy as np
from math import log10
import scipy.signal
import scipy.io as sio
from MFS_3D import *
class Local_MFS_3D (Algorithm):
"""
:3D implementation of Local MFS through holder exponents f(alpha)
:Several MFS are computed on a single domain, from which then
:a set of operations produces features
:version: 1.0
:author: Rodrigo Baravalle
"""
def __init__(self):
pass
def setDef(self, ind, f, ite, filename, file_mask, params):
# parameters: ind -> determines how many levels are used when computing the density
# choose 1 for using directly the image measurement im or
# >= 6 for computing the density of im (quite stable for >=5)
# f ----> determines the dimension of MFS vector
# ite ---> determines how many levels are used when computing MFS for each
self.ind_num = ind # number of pixels for averaging
self.f_num = f # window
self.ite_num = ite
self.filename = filename
self.file_mask = file_mask
self.params = params
def gauss_kern(self,size_x, size_y, size_z):
""" Returns a normalized 3D gauss kernel array for convolutions """
m = np.float32(size_x)
n = np.float32(size_y)
o = np.float32(size_z)
sigma = 2; # ???
if(size_x <= 3): sigma = 1.5;
if(size_x == 5): sigma = 2.5;
z, y, x = np.mgrid[-(m-1)/2:(m-1)/2+1, -(n-1)/2:(n-1)/2+1, -(o-1)/2:(o-1)/2+1]
b = 2*(sigma**2)
square = lambda i : i**2
fm = lambda i: map(square, i)
x2 = map(fm, x)
y2 = map(fm, y)
z2 = map(fm, z)
g = np.sum([x2, y2, z2], axis=0).astype(np.float32)
g = np.exp(g).astype(np.float32)
return g / g.sum()
def determine_threshold(self, arr):
# compute histogram of values
bins = range(np.min(arr), np.max(arr) + 1)
h = np.histogram(arr, bins=bins)
threshold = np.min(arr)
# get x% of mass -> threshold
assert (len(arr.shape) == 3)
total_pixels = arr.shape[0] * arr.shape[1] * arr.shape[2]
for i in range(len(bins) + 1):
# compute sum of h(x) from x = 0 to x = i
partial_sum_vector = np.cumsum(h[0][: (i + 1)])
partial_sum = partial_sum_vector[len(partial_sum_vector) - 1]
percentage = (float)(partial_sum) / (float)(total_pixels)
if percentage > 0.75:
threshold = np.min(arr) + i
break
return threshold
def openMatlab(self, name, filename, greyscale):
import scipy.io as sio
arr = np.array(sio.loadmat(filename)[name]).astype(np.int32)
if greyscale:
return arr
if name == "S":
threshold = self.determine_threshold(arr)
arr = arr > threshold
a_v = arr.cumsum()
print "Amount of white pixels: ", a_v[len(a_v) - 1]
# debug - to see the spongious structure
# plt.imshow((arr[:,:,50]), cmap=plt.gray())
# plt.show()
return arr
def getFDs(self):
"""
@param string filename : volume location
@param string file_mask : mask volume location
@return [float] : 3D (local) multi fractal dimentions
@author: Rodrigo Baravalle.
"""
# data is a 3D grayscale volume
data = self.openMatlab('S', self.filename, True)
data_mask = self.openMatlab('M', self.file_mask, True)
# use the base 3D MFS with the same parameters
# fix me - parameter handling
base_MFS = MFS_3D()
base_MFS.setDef(self.ind_num, self.f_num, self.ite_num,
self.filename, self.file_mask, self.params)
# Masking
data = data * (data_mask > 0)
# Other multifractal measures
if self.params['gradient'] == True:
data = base_MFS.gradient(data)
else:
if self.params['laplacian'] == True:
print "laplacian!"
data = base_MFS.laplacian(data)
xs, ys, zs = data.shape
print xs, ys, zs
num_divisions = 8
#xs_d = xs / num_divisions
#ys_d = ys / num_divisions
#zs_d = zs / num_divisions
dims = 20
local_mfs = np.zeros((xs, ys, zs, dims))
#min_diff = 10000.0
#max_diff = -10000.0
# window size
w = 6
for i in range(xs):
for j in range(ys):
for k in range(zs):
print i, j, k
print max(0, k - w)
print min(k + w, zs - 1)
mfs = base_MFS.getFDs(
data[max(0, i - w) : min(i + w, xs - 1),
max(0, j - w): min(j + w, ys - 1),
max(0, k - w): min(k + w, zs - 1)])
local_mfs[i, j, k] = mfs
#d = np.max(mfs) - np.min(mfs)
#if d < min_diff:
# min_diff = d
#if d > max_diff:
#max_diff = d
return np.mean(local_mfs, axis=(0,1,2))
#max_fa = np.max(local_mfs)
#min_fa = np.min(local_mfs)
#std_fa = np.std(local_mfs)
#mean_fa = np.mean(local_mfs)
#return np.array([max_fa, min_fa,
# mean_fa, std_fa,
# max_diff, min_diff])