从图像中制作gif [英] Making a gif from images

查看:209
本文介绍了从图像中制作gif的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有100个.sdf文件(标记为0000.sdf到0099.sdf)的大量数据,每个文件都包含一个静止图像,我正在尝试从这些图像中生成.gif文件。

I have a load of data in 100 .sdf files (labelled 0000.sdf to 0099.sdf), each of which contain a still image, and I'm trying to produce a .gif from these images.

我用来绘制图形的代码(与sdf文件位于同一目录中):

The code I use to plot the figure are (in the same directory as the sdf files):

q = GetDataSDF('0000.sdf');
imagesc(q.data');

我试图编写一个for循环来绘制图形,然后用它保存文件名作为sdf文件,但无济于事,使用:

I've attempted to write a for loop that would plot the figure and then save it with the same filename as the sdf file but to no avail, using:

for a = 1:100
    q=GetDataSDF('0000.sdf');
    fh = imagesc(q.dist_fn.x_px.Left.data');
    frm = getframe( fh );
    % save as png image
    saveas(fh, 'current_frame_%02d.jpg');
end






编辑:我收到了尝试运行此代码时出现以下错误:


I received the following errors when trying to run this code:

Error using hg.image/get
The name 'Units' is not an accessible property for an instance of class 'image'.

Error in getframe>Local_getRectanglesOfInterest (line 138)
  if ~strcmpi(get(h, 'Units'), 'Pixels')

Error in getframe (line 56)
  [offsetRect, absoluteRect, figPos, figOuterPos] = ...

Error in loop_code (line 4)
    frm = getframe( fh );

如何使用for循环保存这些文件,然后如何使用这些文件生成电影?

How do I save these files using a for loop, and how do I then use those files to produce a movie?

推荐答案

错误的原因是您将图像句柄传递给 getframe ,但此函数会检测数字句柄。
另一个问题是你总是加载相同的文件,而你的保存将不适用于GIF。 (为了将数字保存为静态图像,可能打印是更好的选择吗?)

The reason for the error is that you pass an image handle to getframe, but this function excpects a figure handle. Another problem is that you always load the same file, and that you saveas will not work for gifs. (For saving figures as static images, maybe print is the better option?)

我试图修改自己的gif-writing循环,以便它可以处理你的数据。我会尽量在评论中明确表达,因为你似乎已经开始了。请记住,您始终可以使用 help name_of_command 来显示简短的Matlab帮助。

I tried to modify my own gif-writing loop so that it works with your data. I'll try to be extra explicit in the comments, since you seem to be starting out. Remember, you can always use help name_of_command to display a short Matlab help.

% Define a variable that holds the frames per second your "movie" should have
gif_fps = 24; 
% Define string variable that holds the filename of your movie
video_filename = 'video.gif';

% Create figure 1, store the handle in a variable, you'll need it later
fh = figure(1);
for a = 0:99
    % Prepare file name so that you loop over the data
    q = GetDataSDF(['00' num2str(a,'%02d') 'sdf']);
    % Plot image
    imagesc(q.dist_fn.x_px.Left.data');
    % Force Matlab to actually do the plot (it sometimes gets lazy in loops)  
    drawnow;
    % Take a "screenshot" of the figure fh
    frame = getframe(fh);
    % 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 a == 0;
        imwrite(imind,cm,video_filename,'gif', 'Loopcount',inf);
    else
        imwrite(imind,cm,video_filename,'gif','WriteMode','append','DelayTime',1/gif_fps);
    end
end

还有一个注意事项:当数据大小为对于每个绘图都是一样的,只使用 plot (或者在这种情况下, imagesc )命令一次是有意义的,并在以后的循环迭代中用集替换它(啊,'Ydata',new_y_data)(或者在这种情况下 set(啊,'CData) ',q.dist_fn.x_px.Left.data'),其中是情节的句柄(不是绘图 figure !)。这比在每次循环迭代中创建一个全新的绘图快几个数量级。缺点是缩放(这里,颜色缩放)将是相同的对于每个情节。但在我迄今为止所做的每一个案例中,这实际上是可取的。

One more note: When the size of the data is the same for each plot, it makes sense to only use the plot(or in this case, imagesc) command once, and in later loop iterations replace it with a set(ah,'Ydata',new_y_data) (or in this case set(ah,'CData',q.dist_fn.x_px.Left.data'), where ah is a handle of the plot axes (not the plot figure!). This is orders of magnitude faster than creating a whole new plot in each loop iteration. The downside is that the scaling (here, the color-scaling) will be the same for each plot. But in every case that I have worked on so far, that was actually desirable.

这篇关于从图像中制作gif的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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