-
Notifications
You must be signed in to change notification settings - Fork 1
/
transformstack.m
47 lines (41 loc) · 1.16 KB
/
transformstack.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
45
function transformed = transformstack(sourcestack, targetstack, tform)
% transformed = transformstack(sourcestack, targetstack, tform)
%
% reslice sourcestack in the planes of targetstack using transformation
% matrix tform
% load zstack into RAM
[ nx, ny, ~, nz ] = size(targetstack);
[ maxx, maxy, nc, maxz ] = size(sourcestack);
% preallocate ram
transformed = zeros(nx,ny,nc,nz);
% coordinate matrix in regavg space
if nz > 1
v = ones(nx*ny,4);
else
v = ones(nx*ny,3);
end
for indY=1:ny
v((indY-1)*nx+[1:nx],1) = 1:nx;
v((indY-1)*nx+[1:nx],2) = indY;
end
% loop through planes
for indZ = 1:nz
fprintf('Make plane %d...\n',indZ);
v(:,3) = indZ;
% transform in zstack coords
m = v * tform;
im = zeros(nx,ny,nc);
% fetch nearest neighbour values
for indX=1:nx
for indY=1:ny
x = round(m((indY-1)*nx+indX,1));
y = round(m((indY-1)*nx+indX,2));
z = round(m((indY-1)*nx+indX,3));
if x>=1 && x<=maxx && y>=1 && y<=maxy && z>=1 && z<=maxz
im(indX,indY,:) = sourcestack(x,y,:,z);
end
end
end
% save result
transformed(:,:,:,indZ) = im;
end