保存动画经线作为MATLAB GIF文件

问题描述:

我终于设法得到连锁反应。我的动画,并希望保存动画到 GIF 文件。

I could finally manage to get ripple effect. I animated it and want to save the animation to a GIF file.

不过,我得到了 GIF 文件中的固定形象。

But I get a fixed image in the gif file.

动画的伟大工程,在MATLAB,但我不知道为什么它不会保存。

The animation works great in MATLAB but I don't know why it won't get saved.

im = imread('peppers.png'); 
[m,n,~] = size(im);
n = linspace(-4 * pi,4 * pi,n);
m = linspace(-4 * pi,4 * pi,m);
[X,Y] = meshgrid(m,n);
d = (X .^ 2 + Y .^ 2) .^ .5;
d = d / max(d(:));
d = (d - .5) * 2 * pi;
j = 1;
figure(1);
for i = 0 : .2 : 2 * pi
    Z = cos(2 * d + i) .* exp(-.01 .* d);
    h = warp(X,Y,Z,im);
    axis equal; axis off;
    f = getframe;
    [I,~] = frame2im(f);
    [I,cm] = rgb2ind(I,256);
    if j == 1
        imwrite(I,cm,'B.gif','gif', 'Loopcount',inf);
    else
        imwrite(I,'B.gif','gif','WriteMode','append','DelayTime',1/24);
    end
    j = 0;
end

问题1 如何保存(或什么是目前的code中的问题)?

Question 1 How can I save it (or what is the problem with current code)?

问2 我怎么能保存它的方式,有没有白色背景?

Question 2 How can I save it in a way that there is no white background ?

(例如,使用视图([0 45])和一点点变焦)

(for example with view([0 45]) and a little zoom)

谢谢

修改感谢@ Ayb4btu,我做了一些改进,

Edit Thanks to @Ayb4btu, I made some improvements,

不过使用关闭所有放缓的事了,甚至有时候的getFrame 抓住我的桌面上!

However using close all slows thing down, even sometimes getframe captures my desktop!

由于某种原因,imwrite不喜欢怎样的数字正在更新。下面的不雅code ++工程,通过关闭图和绘图一个新的:

For some reason the imwrite doesn't like how the figure is being updated. The following inelegant code works by closing the figure and drawing a new one:

clear all, close all, clc

I = imread('peppers.png'); 
[m,n] = size(I);
n = linspace(-4 * pi,4 * pi,n);
m = linspace(-4 * pi,4 * pi,m);
[X,Y] = meshgrid(m,n);
d = (X .^ 2 + Y .^ 2) .^ .5;
d = d / max(d(:));
d = (d - .5) * 2 * pi;
j = 1;

for p = 0 : .2 : 4 * pi
    figure(1)
    Z = cos(2 * d + p) .* exp(-.01 .* d);
    h = warp(X,Y,Z,I);
    axis equal; axis off;   

    frame = getframe(1);
    im = frame2im(frame);
    [A,map] = rgb2ind(im,256);

    if j == 1
        imwrite(A,map,'B.gif','gif', 'Loopcount',Inf,'DelayTime',1/24);
    else
        imwrite(A,map,'B.gif','gif','WriteMode','append','DelayTime',1/24);
    end

    j = 0;
    close all
end

以此为基础,你也许能找出问题所在。

Use this as a basis and you might be able to figure out where the problem lies.

至于你的问题2,本次code使用图中的背景颜色,但我相信imwrite了,你可以玩一个颜色属性。

As for your question 2, this code uses the background color of the figure, though I believe imwrite has a color property that you can play with.