循环中的 MATLAB getframe 冲突以创建动画 gif [英] MATLAB getframe conflict in loop to create animated gifs

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

问题描述

我正在尝试为图像制作动画并将其另存为 .gif 文件.在创建一个方程后,我在循环中使用 imshow 并使用 getframe 捕获连续的帧,然后将其转换为图像并再次转换为索引文件.我尝试了其他方法来解决这个问题,但总是遇到同样的问题.即使是 Matlab 文档中关于创建动画 gif 的示例peaks/surf"函数也没有输出正确的 .gif 文件(这意味着它由于重复帧而滞后).

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).

我遇到的问题是,似乎在看似随机的时间,getframe 捕获前一帧而不是当前帧.这个问题在我试过的其他电脑上没有发生,所以我觉得这是我的 cpu 和图形处理器(Mac OSX 10.7.5,Macbook Pro w Retina display 17")之间的通信问题.几乎是好像 CPU 处理图像的速度比 GPU 可以将它们发送到我的显示器的速度快.对此理论的任何见解表示赞赏.我尝试更新 Java 和 Matlab (v.2012a) 无济于事.我还安装了最近的更新OSX,其中之一解决了视频游戏的帧刷新率.

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.

当我通过取消对关闭"命令的注释(参见下面的代码)在 imshow 和 getframe 操作之间给计算机一些时间以便 getframe 无法捕获错误的图像时,.gif 会完美地显示出来.否则,我会在输出中得到重复的帧(请参阅 imgur 相册中的图像).我使用close"的问题是它确实延长了执行时间(大约 4 倍).

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).

请参阅:http://imgur.com/a/pWCE5 以准确理解我的意思.

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

我的代码如下,它很可能会在大多数人的计算机上不取消注释关闭"的情况下产生所需的结果.我意识到这是一个很难解决的问题,因为它似乎与我的 GPU 有关,但我不知道在这里做什么...谢谢您的时间.

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

推荐答案

在 'getframe' 之前的 'drawnow' 可以解决问题.

'drawnow' preceding 'getframe' does the trick.

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

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