Create .gif without displaying each frame

6 Ansichten (letzte 30 Tage)
Tom
Tom am 22 Nov. 2014
Kommentiert: Geoff Hayes am 23 Nov. 2014
Trying to create a .gif with hundreds of frames. Matlab pops up each window which takes a while so the whole process is mentally long.
Can you plot and save the gif in the background? code below, good luck if you read all of it, thanks.
h = figure('position', [100 300 600 600]);
plot3(recon_CuRvEkiller(:,1),recon_CuRvEkiller(:,2),recon_CuRvEkiller(:,3),'b');
hold on
plot3(recon_CuRvEdroso(:,1), recon_CuRvEdroso(:,2),recon_CuRvEdroso(:,3),'r');%plot3([recon_CuRvEkiller(i,1) recon_CuRvEdroso(i,1)], [recon_CuRvEkiller(i,2) recon_CuRvEdroso(i,2)], [recon_CuRvEkiller(i,3) recon_CuRvEdroso(i,3)],'-k');
hold on
axis equal
grid on
set(gca, 'XColor', [.4 .4 .4], 'YColor', [.4 .4 .4], 'ZColor', [.4 .4 .4],...
'XTickLabel',[],'YTickLabel',[],'ZTickLabel',[]); % hides the ticks on x-axis
%now pause and ask the user to choose viewing angle
h = msgbox('chose correct viewing angle by rotating figure');
timeout = 600;
uiwait(h,timeout)
%%4. Now change the viewing angle until we are happy wiht it and run this cell
[az,el] = view;
mainfolder = pwd;
mkdir('Plotting_recon+vectors'); %this makes a new folder
cd([pwd '\Plotting_recon+vectors']); %this moves us to the new folder
save 'az' 'az'
save 'el' 'el'
cd(mainfolder)
save 'view' 'az' 'el'
close all
%%5. THIS IS preparing the 6. PLOTING OF THE HEADING with LOS and 7. with LOS_neuraldelay_fixed_frames.
%UNCOMMENT parts of this section if you want to save each snap as a TIFF
%5.
%skipframe = 1; % set this to 1 if we want to render each frame
%-----------------------------------------------------------------------
%uncomment this
%mkdir('traj_snaps'); %this makes a new folder
%------------------------------------------------------------------------
%Before we plot, we must find teh min and min xyz values.
%Calculate max values for xyz of the extended vectors and locations
vectorkill = VeLoCiTy_norm_killer_ext'+recon_CuRvEkiller;
vectordroso = VeLoCiTy_norm_droso_ext'+recon_CuRvEdroso;
%find max.
maxPOINT(1,1:3) = max(vectorkill,[],1);
maxPOINT(2,1:3) = max(vectordroso,[],1);
maxPOINT(3,1:3) = max(recon_CuRvEkiller,[],1);
maxPOINT(4,1:3) = max(recon_CuRvEdroso,[],1);
maxPOINTFINAL(1,1:3) = max(maxPOINT,[],1);
%finmin
minPOINT(1,1:3) = min(vectorkill,[],1);
minPOINT(2,1:3) = min(vectordroso,[],1);
minPOINT(3,1:3) = min(recon_CuRvEkiller,[],1);
minPOINT(4,1:3) = min(recon_CuRvEdroso,[],1);
minPOINTFINAL(1,1:3) = min(minPOINT,[],1);
bit= 10;
xmin = minPOINTFINAL(1,1)-bit ;
ymin = minPOINTFINAL(1,2)-bit;
zmin = minPOINTFINAL(1,3)-bit;
xmax = maxPOINTFINAL(1,1) +bit ;
ymax = maxPOINTFINAL(1,2)+bit;
zmax = maxPOINTFINAL(1,3)+bit;
load distance_analysis rendertype_volume
%6. Set up the movie for normal LOS
gif_fps = 15;
% Define string variable that holds the filename of your movie
snap_traj = 'snap_traj.gif';
for i = 1:killer_catchframe-Killer_1stmovingframe;
% Define a variable that holds the frames per second your "movie" should have
h = figure('Position', [20 40 1600 900],'color','w');
set(gca, 'XColor', [.4 .4 .4], 'YColor', [.4 .4 .4], 'ZColor', [.4 .4 .4],...
'XTickLabel',[],'YTickLabel',[],'ZTickLabel',[]); % hides the ticks on x-axis
cc=hsv(size(recon_CuRvEkiller,1));
xlim([xmin xmax])
ylim([ymin ymax])
zlim([zmin zmax])
%------------------------------------------------------------------------------------
%%paste in here the view axes results!
view([az el]); %this needs to be changed depending on the trajectory
hold on
axis equal
%-------------------------------------------------------------------------------------
% get(gca);
quiver3(recon_CuRvEdroso(i,1),recon_CuRvEdroso(i,2),recon_CuRvEdroso(i,3)...
,VeLoCiTy_norm_droso_ext(1,i),VeLoCiTy_norm_droso_ext(2,i),...
VeLoCiTy_norm_droso_ext(3,i),0,'m','LineWidth',1.5,'MaxHeadSize',5) ; % the '0' turns off auto-scaling
hold on;
quiver3(recon_CuRvEkiller(i,1),recon_CuRvEkiller(i,2),recon_CuRvEkiller(i,3)...
,VeLoCiTy_norm_killer_ext(1,i),VeLoCiTy_norm_killer_ext(2,i),...
VeLoCiTy_norm_killer_ext(3,i),0,'c','LineWidth',1.5,'MaxHeadSize',5) ; % the '0' turns off auto-scaling
hold on;
%this plots the LOS
quiver3(recon_CuRvEkiller(i,1),recon_CuRvEkiller(i,2),recon_CuRvEkiller...
(i,3), LOS_vec(i,1), LOS_vec(i,2),LOS_vec(i,3),0,'--k','LineWidth',1,'ShowArrowHead','off');%'LineStyle','--','ShowArrowHead','off)
grid on
hold on;
if strcmp(rendertype_volume, 'yes')==1
% where (a,b,c) is center of the sphere,
%where xc,yc,zc is the center of the ellipsoid.
adroso = recon_CuRvEdroso(:,1);
bdroso = recon_CuRvEdroso(:,2);
cdroso = recon_CuRvEdroso(:,3);
akiller = recon_CuRvEkiller(:,1);
bkiller = recon_CuRvEkiller(:,2);
ckiller = recon_CuRvEkiller(:,3);
%[x,y,z] = ellipsoid(xc,yc,zc,xr,yr,zr,n)
% xr,yr,zr
%and r is the rahdius (i.e bead size/2
rdroso = preysize_number_droso/2;
rkiller = preysize_number_killer/2;%create a unit vector sphere.
[x,y,z] = sphere(100);
%get(gca);
if i== 1
surf((rdroso*x)+adroso(i,1), (rdroso*y)+bdroso(i,1), (rdroso*z)+cdroso(i,1), 'FaceColor', 'r', 'EdgeColor', 'r', 'clipping', 'off');
axis manual
hold on
surf ((rkiller*x)+ akiller(i,1), (rkiller*y)+bkiller(i,1), (rkiller*z)+ckiller(i,1), 'FaceColor', 'b', 'EdgeColor', 'b','clipping', 'off' );
axis manual
hold on
else
%you can turn this into different colors by uncommented
%the text next to lines
surf((rdroso*x)+adroso(i,1), (rdroso*y)+bdroso(i,1), (rdroso*z)+cdroso(i,1),'clipping', 'off','FaceColor','r','EdgeColor','r'); %cc(i,:)
axis manual
hold on
surf ((rkiller*x)+ akiller(i,1), (rkiller*y)+bkiller(i,1), (rkiller*z)+ckiller(i,1),'clipping', 'off','FaceColor','b','EdgeColor', 'b');
axis manual
hold on
end
%light('Position',[1 0 0],'Style','infinite');
else
%this plots the trajectories (smooth ones)
plot3(recon_CuRvEkiller(:,1),recon_CuRvEkiller(:,2),recon_CuRvEkiller(:,3),'b');
hold on
plot3(recon_CuRvEdroso(:,1), recon_CuRvEdroso(:,2),recon_CuRvEdroso(:,3),'r');%plot3([recon_CuRvEkiller(i,1) recon_CuRvEdroso(i,1)], [recon_CuRvEkiller(i,2) recon_CuRvEdroso(i,2)], [recon_CuRvEkiller(i,3) recon_CuRvEdroso(i,3)],'-k');
hold on
end
%-----------------------------------------------------------------------
%uncoment this
%this saves it as tiff.
%saveas(h,[pwd '\Traj_snaps\' 'Frame_' int2str(i) '.tiff' ]);
%------------------------------------------------------------------------
drawnow;
pause(0.3)
% % Take a "screenshot" of the figure fh
frame = getframe(h);
% Turn screenshot into image
im = frame2im(frame);
% Turn image into indexed image (the gif format needs this)
[imind,cm] = rgb2ind(im,256);
% If first loop iteration: Create the file, else append to it
if i == 1;
imwrite(imind,cm,snap_traj,'gif', 'Loopcount',inf);
else
imwrite(imind,cm,snap_traj,'gif','WriteMode','append','DelayTime',1/gif_fps);
end
close all
end
  2 Kommentare
Geoff Hayes
Geoff Hayes am 23 Nov. 2014
Tom - what do you mean by Matlab pops up each window which takes a while? What window are you referring to - that for the frame which you are writing to the animated gif?
Or are you asking for an alternative to the above so that each frame is not displayed?
Tom
Tom am 23 Nov. 2014
Hi - to clarify: Matlab creates each frame of the gif in turn, and closes and loads each new frame which takes time. can't it plot each frame within the same figure window?
i'd like to be able to create each frame and append it to the gif in the background without it being visible
I'm sorry i can't ask a more direct question as I don't really know that much about matlab image creation.
here is a video to show what the windows are doing: Video

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Geoff Hayes
Geoff Hayes am 23 Nov. 2014
Tom - your for loop looks something like
...
snap_traj = 'snap_traj.gif';
for i = 1:killer_catchframe-Killer_1stmovingframe;
% Define a variable that holds the frames per second your "movie" should have
h = figure('Position', [20 40 1600 900],'color','w');
set(gca, 'XColor', [.4 .4 .4], 'YColor', [.4 .4 .4], 'ZColor', [.4 .4 .4],...
'XTickLabel',[],'YTickLabel',[],'ZTickLabel',[]); % hides the ticks on x-axis
cc=hsv(size(recon_CuRvEkiller,1));
xlim([xmin xmax])
ylim([ymin ymax])
zlim([zmin zmax])
% other stuff
if i == 1;
imwrite(imind,cm,snap_traj,'gif', 'Loopcount',inf);
else
imwrite(imind,cm,snap_traj,'gif','WriteMode','append','DelayTime',1/gif_fps);
end
close all
end
The call to figure creates a new figure, and the close all, closes it. Try removing the close all and put the creation of the figure outside of the for loop as
...
snap_traj = 'snap_traj.gif';
% create the figure
h = figure('Position', [20 40 1600 900],'color','w');
for i = 1:killer_catchframe-Killer_1stmovingframe;
% clear the axes
cla(gca,'reset');
set(gca, 'XColor', [.4 .4 .4], 'YColor', [.4 .4 .4], 'ZColor', [.4 .4 .4],...
'XTickLabel',[],'YTickLabel',[],'ZTickLabel',[]); % hides the ticks on x-axis
% other stuff
if i == 1;
imwrite(imind,cm,snap_traj,'gif', 'Loopcount',inf);
else
imwrite(imind,cm,snap_traj,'gif','WriteMode','append','DelayTime',1/gif_fps);
end
end
The above is very similar to what you had, with just a couple of lines changed. Note the addition of cla(gca,'reset') as the first line in the for loop which clears the current axes (see cla for details). That should remove all graphics handles from the previous iteration. You may notice some flickering but at least a new figure won't be opened and closed each time.
  2 Kommentare
Tom
Tom am 23 Nov. 2014
Hi - thanks for the help; on the right track, now each frame is rewritten in the window rather than each window being reloaded.
unfortunately, it no longer appends the gif; ie the output is now a single frame gif. i can't see why that would be?
snap_traj = 'snap_traj.gif';
h = figure('Position', [20 40 1600 900],'color','w');
for i = 1:killer_catchframe-Killer_1stmovingframe;
% Define a variable that holds the frames per second your "movie" should have
% clear the axes
cla(gca,'reset');
% h = figure('Position', [20 40 1600 900],'color','w');
set(gca, 'XColor', [.4 .4 .4], 'YColor', [.4 .4 .4], 'ZColor', [.4 .4 .4],...
'XTickLabel',[],'YTickLabel',[],'ZTickLabel',[]); % hides the ticks on x-axis
cc=hsv(size(recon_CuRvEkiller,1));
xlim([xmin xmax])
ylim([ymin ymax])
zlim([zmin zmax])
%------------------------------------------------------------------------------------
%%paste in here the view axes results!
view([az el]); %this needs to be changed depending on the trajectory
hold on
axis equal
%-------------------------------------------------------------------------------------
% get(gca);
quiver3(recon_CuRvEdroso(i,1),recon_CuRvEdroso(i,2),recon_CuRvEdroso(i,3)...
,VeLoCiTy_norm_droso_ext(1,i),VeLoCiTy_norm_droso_ext(2,i),...
VeLoCiTy_norm_droso_ext(3,i),0,'m','LineWidth',1.5,'MaxHeadSize',5) ; % the '0' turns off auto-scaling
hold on;
quiver3(recon_CuRvEkiller(i,1),recon_CuRvEkiller(i,2),recon_CuRvEkiller(i,3)...
,VeLoCiTy_norm_killer_ext(1,i),VeLoCiTy_norm_killer_ext(2,i),...
VeLoCiTy_norm_killer_ext(3,i),0,'c','LineWidth',1.5,'MaxHeadSize',5) ; % the '0' turns off auto-scaling
hold on;
%this plots the LOS
quiver3(recon_CuRvEkiller(i,1),recon_CuRvEkiller(i,2),recon_CuRvEkiller...
(i,3), LOS_vec(i,1), LOS_vec(i,2),LOS_vec(i,3),0,'--k','LineWidth',1,'ShowArrowHead','off');%'LineStyle','--','ShowArrowHead','off)
grid on
hold on;
if strcmp(rendertype_volume, 'yes')==1
% where (a,b,c) is center of the sphere,
%where xc,yc,zc is the center of the ellipsoid.
adroso = recon_CuRvEdroso(:,1);
bdroso = recon_CuRvEdroso(:,2);
cdroso = recon_CuRvEdroso(:,3);
akiller = recon_CuRvEkiller(:,1);
bkiller = recon_CuRvEkiller(:,2);
ckiller = recon_CuRvEkiller(:,3);
%[x,y,z] = ellipsoid(xc,yc,zc,xr,yr,zr,n)
% xr,yr,zr
%and r is the rahdius (i.e bead size/2
rdroso = preysize_number_droso/2;
rkiller = preysize_number_killer/2;%create a unit vector sphere.
[x,y,z] = sphere(100);
%get(gca);
if i== 1
surf((rdroso*x)+adroso(i,1), (rdroso*y)+bdroso(i,1), (rdroso*z)+cdroso(i,1), 'FaceColor', 'r', 'EdgeColor', 'r', 'clipping', 'off');
axis manual
hold on
surf ((rkiller*x)+ akiller(i,1), (rkiller*y)+bkiller(i,1), (rkiller*z)+ckiller(i,1), 'FaceColor', 'b', 'EdgeColor', 'b','clipping', 'off' );
axis manual
hold on
else
%you can turn this into different colors by uncommented
%the text next to lines
surf((rdroso*x)+adroso(i,1), (rdroso*y)+bdroso(i,1), (rdroso*z)+cdroso(i,1),'clipping', 'off','FaceColor','r','EdgeColor','r'); %cc(i,:)
axis manual
hold on
surf ((rkiller*x)+ akiller(i,1), (rkiller*y)+bkiller(i,1), (rkiller*z)+ckiller(i,1),'clipping', 'off','FaceColor','b','EdgeColor', 'b');
axis manual
hold on
end
%light('Position',[1 0 0],'Style','infinite');
else
%this plots the trajectories (smooth ones)
plot3(recon_CuRvEkiller(:,1),recon_CuRvEkiller(:,2),recon_CuRvEkiller(:,3),'b');
hold on
plot3(recon_CuRvEdroso(:,1), recon_CuRvEdroso(:,2),recon_CuRvEdroso(:,3),'r');%plot3([recon_CuRvEkiller(i,1) recon_CuRvEdroso(i,1)], [recon_CuRvEkiller(i,2) recon_CuRvEdroso(i,2)], [recon_CuRvEkiller(i,3) recon_CuRvEdroso(i,3)],'-k');
hold on
end
%-----------------------------------------------------------------------
%uncoment this
%this saves it as tiff.
%saveas(h,[pwd '\Traj_snaps\' 'Frame_' int2str(i) '.tiff' ]);
%------------------------------------------------------------------------
drawnow;
% % Take a "screenshot" of the figure fh
frame = getframe;
% Turn screenshot into image
im = frame2im(frame);
% Turn image into indexed image (the gif format needs this)
[imind,cm] = rgb2ind(im,256);
% If first loop iteration: Create the file, else append to it
if i == 1;
imwrite(imind,cm,snap_traj,'gif', 'Loopcount',inf);
else
imwrite(imind,cm,snap_traj,'gif','WriteMode','append','DelayTime',0);
end
% % close all
end
Geoff Hayes
Geoff Hayes am 23 Nov. 2014
Tom - are you sure that the output image is just the single frame (is it first or last) or is it multiple copies of the same frame? If you put a breakpoint in the else block of your final if statement, does the code reach that breakpoint and try to append the image? I see from the above that you have removed set the DelayTime to be zero instead of 1/15. Why the change and could that be the reason your are only seeing one image (since there are zero seconds between each frame)?

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Images finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by