Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 34 additions & 5 deletions extrafunctions/volumes2movie.m
Original file line number Diff line number Diff line change
@@ -1,27 +1,44 @@
function volumes2movie(V,outfile,fps)
% Converts a set of spm volumes (V struct) to a movie, saved as outfile and
% played back at fps.
% Converts a set of spm volumes (V struct) to a movie, saved as
% outfile and played back at fps.
%
% volumes2movie(V,outfile,fps)
%
% CHANGE HISTORY
%
% 5/2017 [MSJ] replaced movie2avi with writeVideo, general cleanup
%

xyz = spm_read_vols(V);

% Take 3 axial sections

steps = round(size(xyz,3)/4);
secs = [steps steps*2 steps*3];

% Flip around to appropriate orientation

for s = 1:3
axials{s} = permute(squeeze(xyz(:,:,secs(s),:)),[2 1 3]);
axials{s} = axials{s}(end:-1:1,:,:);
end

% And concatenate

axials = [axials{1} axials{2} axials{3}];

% And get rid of ridiculously big variable

clear xyz

% set video in standard display format to avoid weird distortions in most
% video players
% set video in standard display format to avoid weird distortions
% in most video players

axsize = size(axials);
goodsizes = [240 320; 480 640; 600 800; 768 1024];
ind = find(axsize(2)<goodsizes(:,2),1,'first');
targsize = goodsizes(ind,:);

if axsize(1)<targsize(1)
% add 0s at bottom
axials((axsize(1)+1):targsize(1),:,:) = 0;
Expand All @@ -31,15 +48,27 @@ function volumes2movie(V,outfile,fps)
end

% Rescale and make uint8

intmin = min(axials(:));
axials = axials - intmin;
intmax = max(axials(:));
axials = (axials/intmax);
axials = gray2ind(axials,255);

% Make movie frames

for f = 1:size(axials,3)
mframes(f,:) = im2frame(cat(3,axials(:,:,f),axials(:,:,f),axials(:,:,f)));
end

% Write out avi
movie2avi(mframes,outfile,'FPS',fps,'COMPRESSION','none');

% movie2avi(mframes,outfile,'FPS',fps,'COMPRESSION','none');
%
% movie2avi is no longer available. Use writeVideo instead:

v = VideoWriter(outfile, 'Uncompressed AVI');
v.FrameRate = fps;
open(v)
writeVideo(v, mframes);
close(v);