-
Notifications
You must be signed in to change notification settings - Fork 0
/
align_zstack.m
44 lines (29 loc) · 1006 Bytes
/
align_zstack.m
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
function [m,T] = align_zstack(zstack)
% Aligns images in all frames in a zstack
%
% m - mean image after the alignment (similar to maximum projection)
% T - optimal translation for each frame
if size(zstack,3) == 1
m = squeeze(zstack(:,:,1));
T = [0 0];
elseif size(zstack,3) == 2
A = squeeze(zstack(:,:,1));
B = squeeze(zstack(:,:,2));
Ap = adapthisteq(A(50:end-50,151:end-50));
Bp = adapthisteq(B(50:end-50,151:end-50));
[u, v] = jkfftalign(Ap,Bp);
Ar = circshift(A,[u,v]);
m = (Ar+B)/2;
T = [[u v] ; [0 0]];
else
zstack1 = zstack(:,:,1:floor(end/2));
zstack2 = zstack(:,:,floor(end/2)+1 : end);
[A,T0] = align_zstack(zstack1);
[B,T1] = align_zstack(zstack2);
Ap = adapthisteq(A(50:end-50,151:end-50));
Bp = adapthisteq(B(50:end-50,151:end-50));
[u, v] = jkfftalign(Ap,Bp);
Ar = circshift(A,[u, v]);
m = (Ar+B)/2;
T = [(ones(size(T0,1),1)*[u v] + T0) ; T1];
end