GIF speed not as expected! Help!

108 次查看(过去 30 天)
Hello. I am trying to generate a GIF of a growing bar. However, the speed of the generating gif is really slow, and I don't understand how to make it faster. The 'DelayTime' is set to 0, and when the animation is generated in MATLAB is looks really fine. However, when I open the bar.gif, the animation is not fluid at all. This is the code
%draw rectangle and lambda
clear;
figure();
rectangle('Position',[0 2 10 1]);
axis off
set(gcf,'Position',[ -901 789 579 70])
t=0:0.05:3
l=1; a=0.5
p=@(t,l,a) 1-(1-a).*exp(-l.*t);
x=[0 0 10 10];
y=[2 3 3 2];
patch(x,y,'white');
frame=getframe(gca); im=frame2im(frame);
[imind,cm] = rgb2ind(im,256);
filename='bar.gif';
imwrite(imind,cm,filename,'gif','DelayTime',0.5, 'Loopcount',inf);
for tt=1:length(t)
pt=p(t(tt),l,a);
x=[0 0 10.*pt 10.*pt];
patch(x,y,'green');
% pause(.01); drawnow;
frame(tt)=getframe(gca);
end
for tt=1:length(t)
im=frame2im(frame(tt));
[imind,cm] = rgb2ind(im,256);
imwrite(imind,cm,filename, 'gif','DelayTime',0,'WriteMode','append');
end
  1 个评论
John D'Errico
John D'Errico 2017-11-14
编辑:John D'Errico 2017-11-14
@DA Huang: Please don't answer a question with your own question. Use comments to make a comment.
Moved an answer by Da Huang into a comment:
"Have you solve this problem?"

请先登录,再进行评论。

采纳的回答

Utpal Kumar
Utpal Kumar 2017-7-7
Edited and the delay time has been set to 0.1 seconds:
%draw rectangle and lambda
clear;
figure();
rectangle('Position',[0 2 10 1]);
axis off
set(gcf,'Position',[ -901 789 579 70])
t=0:0.05:3
l=1; a=0.5
p=@(t,l,a) 1-(1-a).*exp(-l.*t);
x=[0 0 10 10];
y=[2 3 3 2];
patch(x,y,'white');
frame=getframe(gca); im=frame2im(frame);
[imind,cm] = rgb2ind(im,256);
filename='bar.gif';
imwrite(imind,cm,filename,'gif','DelayTime',0.1, 'Loopcount',inf);
for tt=1:length(t)
pt=p(t(tt),l,a);
x=[0 0 10.*pt 10.*pt];
patch(x,y,'green');
% pause(.01); drawnow;
frame(tt)=getframe(gca);
end
for tt=1:length(t)
im=frame2im(frame(tt));
[imind,cm] = rgb2ind(im,256);
imwrite(imind,cm,filename, 'gif','DelayTime',0.1,'WriteMode','append');
end
  3 个评论
Diptangshu Paul
Diptangshu Paul 2022-9-14
How is delaytime 0.1 faster than delaytime 0?
DGM
DGM 2023-4-23
Short delay times depend entirely on the application to render them at the specified rate. It should be no surprise that at some point, the specified delay time fails to be met. While an exact zero delay time might seem a bit of a stretch, in practice it's worse than that. While most web browsers support delay times below 0.1s, most will treat 0 delay time as 0.1s. In other words, if you try to go too fast, you'll actually get slower results.
A number of people have surveyed this experimentally over the years. Here are two old ones that I found.
From the conclusion on the first link:
The first finding to note is that no modern browser surveyed supports frame delays below 0.02 seconds. Therefore, when creating an animated GIF, one should never use a frame delay below this threshold as it will be entirely ineffectual.
From the documentation for imwrite():
To achieve a high animation rate, set DelayTime to 0.02. Setting DelayTime to a lower value will slow down the actual animation rate in many image viewers and web browsers.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Animation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by