Commit 95f1e985cd9b87af55912a303d878ae39b2d8f92
1 parent
7ce7cd7b
updated structure tensor functions
Showing
2 changed files
with
60 additions
and
7 deletions
Show diff stats
python/structen.py
... | ... | @@ -10,8 +10,7 @@ import scipy.ndimage |
10 | 10 | import progressbar |
11 | 11 | import glob |
12 | 12 | |
13 | -def st2(I, s=1, dtype=numpy.float32): | |
14 | - | |
13 | +def st2(I, s=1, dtype=numpy.float32): | |
15 | 14 | |
16 | 15 | #calculates the 2D structure tensor for an image using a gaussian window with standard deviation s |
17 | 16 | |
... | ... | @@ -222,11 +221,63 @@ def st2amira(filename, T): |
222 | 221 | # 5 dz dz ----> 5 |
223 | 222 | |
224 | 223 | #swap the 2nd and 3rd tensor components |
225 | - temp = T[..., 3] | |
226 | - T[..., 3] = T[..., 2] | |
227 | - T[..., 2] = temp; | |
224 | + A = numpy.copy(T) | |
225 | + A[..., 3] = T[..., 2] | |
226 | + A[..., 2] = T[..., 3] | |
228 | 227 | |
229 | 228 | #roll the tensor axis so that it is the leading component |
230 | - A = numpy.rollaxis(T, T.ndim - 1) | |
229 | + A = numpy.rollaxis(A, A.ndim - 1) | |
231 | 230 | A.tofile(filename) |
232 | - print(A.shape) | |
233 | 231 | \ No newline at end of file |
232 | + print("\n", A.shape) | |
233 | + | |
234 | +def resample3(T, s=2): | |
235 | + #resample a tensor field by an integer factor s | |
236 | + #This function first convolves the field with a box filter and then | |
237 | + # re-samples to create a smaller field | |
238 | + | |
239 | + #check the format for the window size | |
240 | + if type(s) is not list: | |
241 | + s = [s] * 3 | |
242 | + elif len(s) == 1: | |
243 | + s = s * 3 | |
244 | + elif len(s) == 2: | |
245 | + s.insert(1, s[0]) | |
246 | + s = numpy.array(s) | |
247 | + | |
248 | + bar = progressbar.ProgressBar() | |
249 | + bar.max_value = T.shape[3] | |
250 | + | |
251 | + #blur with a uniform box filter of size r | |
252 | + for t in range(T.shape[3]): | |
253 | + T[..., t] = scipy.ndimage.filters.uniform_filter(T[..., t], 2 * s) | |
254 | + bar.update(t+1) | |
255 | + | |
256 | + #resample at a rate of r | |
257 | + R = T[::s[0], ::s[1], ::s[2], :] | |
258 | + return R | |
259 | + | |
260 | +def color3(prefix, T, vector='largest', aniso=True): | |
261 | + #Saves a stack of color images corresponding to the eigenvector and optionally scaled by anisotropy | |
262 | + | |
263 | + bar = progressbar.ProgressBar() | |
264 | + bar.max_value = T.shape[2] | |
265 | + | |
266 | + #for each z-axis slice | |
267 | + for z in range(T.shape[2]): | |
268 | + S = T[:, :, z, :] #get the slice | |
269 | + V = st2vec(S, vector='smallest') #calculate the vector | |
270 | + C = numpy.absolute(V) #calculate the absolute value | |
271 | + | |
272 | + if aniso == True: #if the image is scaled by anisotropy | |
273 | + FA, Cl, Cp, Cs = anisotropy(S) #calculate the anisotropy of the slice | |
274 | + if vector == 'largest': | |
275 | + A = Cl | |
276 | + elif vector == 'smallest': | |
277 | + A = Cp | |
278 | + else: #otherwise just scale by 1 | |
279 | + A = numpy.ones(T.shape[0], T.shape[1]) | |
280 | + image = C * numpy.expand_dims(A, 3) | |
281 | + | |
282 | + filename = prefix + str(z).zfill(4) + ".bmp" | |
283 | + scipy.misc.imsave(filename, image) | |
284 | + bar.update(z + 1) | |
234 | 285 | \ No newline at end of file | ... | ... |
stim/parser/filename.h