在循环MATLAB的getFrame冲突创建动画GIF [英] MATLAB getframe conflict in loop to create animated gifs

查看:225
本文介绍了在循环MATLAB的getFrame冲突创建动画GIF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图动画图像并将其保存为.gif文件。起草一个公式后,我用imshow一个循环内捕捉的getFrame连续帧,然后我转换为图像,并再次成为一个索引文件。我尝试过其他方法这一点,但总是想出了同样的问题。即使Matlab的创建GIF动画文件中的样本峰/冲浪功能不输出正确的.gif文件(这意​​味着它滞后因为重复帧)。

我遇到的问题是,它似乎,在看似随机时间,的getFrame抓住了previous框架,而不是当前的。该问题不会发生在我尝试过在其他计算机上,所以我有一种感觉它与我的CPU和图形处理器(的Mac OSX 10.7.5,MacBook Pro的W¯¯Retina显示屏的17)之间的沟通有问题。这几乎是因为如果CPU处理图像速度比GPU可以将它们发送到我的显示器。这一理论的任何见解AP preciated。我已经尝试更新的Java和Matlab(v.2012a)无济于事。我也装最近更新的OSX,其中一个目标帧刷新率的视频游戏。

当我给电脑通过取消注释关闭命令imshow和操作的getFrame之间的某个时间(见code以下),这样的getFrame不能捕捉可能的错误的图像,将.gif文件出来完美。否则,我得到重复输出帧(见imgur专辑图像)。我用关闭的问题是,它确实延长执行时间(约4倍)。

请参阅: http://imgur.com/a/pWCE5 确切地理解了我的意思。

我的code是低于,这是可能产生预期的结果,而不在取消关闭在大多数人的电脑。我知道这是一个棘手的问题来解决,因为这似乎与我的GPU,但我在为这里做什么损失...谢谢您的时间。

 清除所有
抽搐
ENDLOOP = 26;
对于i = 1:ENDLOOP%定义下一个循环的高度值
    如果我≤(ENDLOOP / 2 + 2)
        F(ⅰ)= 3 + I;
    其他
        F(ⅰ)= F(I-1)-1;
    结束
结束[X,Y] = meshgrid(-256:255,-256:255); %创建网格
(X ^ 2 + Y ^ 2)Z =开方; %定义圈对于k = 1:ENDLOOP
    C =(z,其中F(K));
    CF = fftshift(FFT​​2(c)项); %应用变换
        FL =日志(1 + ABS(CF)); FM = MAX(FL(:));
        imshow(im2uint8(FL / FM));
    帧=的getFrame; % 关;
    IM = frame2im(架);
    [imind,图] = rgb2ind(IM,256);
    如果k == 1
        imwrite(imind,地图,'circle3.gif','DelayTime',0.1,LoopCount',INF);
    其他
        imwrite(imind,地图,'circle3.gif','DelayTime',0.1,WriteMode','追加');
    结束
结束
关闭所有
TOC


解决方案

的DrawNow'preceding'的getFrame的伎俩。

I'm trying to animate an image and save it as a .gif file. After crafting an equation, I use imshow within a loop and capture successive frames with getframe, which I then convert to an image and again into an indexed file. I've tried other approaches to this but always came up with the same problem. Even the sample 'peaks/surf' function in Matlab's documentation on creating animated gifs doesn't output the correct .gif file (meaning it lags because of repeated frames).

The problem I'm having is that it appears that at seemingly random times, getframe captures the previous frame rather than the current one. The issue does not occur on other computers I've tried it on, so I have a feeling it's a problem with the communication between my cpu and graphics processor (Mac OSX 10.7.5, Macbook Pro w Retina display 17"). It's almost as if the CPU is processing images faster than the GPU can send them to my monitor. Any insights on this theory are appreciated. I've tried updating Java and Matlab (v.2012a) to no avail. I also installed the recent updates for OSX, one of which addressed frame refresh rates for video games.

When I give the computer some time between the imshow and getframe operations by uncommenting the 'close' command (see code below) so that getframe cannot possible capture the wrong image, the .gif comes out perfectly. Otherwise, I get repeated frames in the output (see images in imgur album). The issue I have with using 'close' is that it really lengthens the execution time (about 4x).

See: http://imgur.com/a/pWCE5 to understand exactly what I mean.

My code is below, which is likely to produce the desired result without uncommenting 'close' on most people's computers. I realize it's a tough issue to tackle because it seems to be related to my GPU but I'm at a loss for what to do here... Thank you for your time.

clear all
tic
endloop=26;
for i=1:endloop % defines height values for next loop
    if i<(endloop/2 + 2)
        f(i)=3+i;
    else
        f(i)=f(i-1)-1;
    end 
end

[x,y]=meshgrid(-256:255,-256:255); % create grid
z=sqrt(x.^2+y.^2); % define circle

for k=1:endloop
    c=(z<f(k));
    cf=fftshift(fft2(c)); % apply transform
        fl = log(1+abs(cf)); fm = max(fl(:));
        imshow(im2uint8(fl/fm)); 
    frame=getframe; % close;
    im=frame2im(frame);
    [imind,map]=rgb2ind(im,256);
    if k==1
        imwrite(imind,map,'circle3.gif','DelayTime',0.1,'LoopCount',inf);
    else
        imwrite(imind,map,'circle3.gif', 'DelayTime',0.1, 'WriteMode', 'append');
    end
end
close all
toc

解决方案

'drawnow' preceding 'getframe' does the trick.

这篇关于在循环MATLAB的getFrame冲突创建动画GIF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆