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

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

问题描述

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

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

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

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

for a = 1:100q=GetDataSDF('0000.sdf');fh = imagesc(q.dist_fn.x_px.Left.data');frm = getframe( fh );%另存为png图片saveas(fh, 'current_frame_%02d.jpg');结尾

<小时>

我在尝试运行此代码时收到以下错误:

使用 hg.image/get 时出错名称Units"不是image"类实例的可访问属性.getframe>Local_getRectanglesOfInterest 中的错误(第 138 行)如果 ~strcmpi(get(h, 'Units'), '像素')getframe 中的错误(第 56 行)[offsetRect, absoluteRect, figPos, figOuterPos] = ...loop_code 中的错误(第 4 行)frm = getframe( fh );

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

解决方案

错误的原因是你给 getframe 传递了一个 image 句柄,但是这个函数期望一个句柄.另一个问题是您总是加载相同的文件,并且您的 saveas 不适用于 gif.(要将数字保存为静态图像,也许打印是更好的选择?)>

我尝试修改我自己的 gif 写入循环,以便它可以处理您的数据.我会尽量在评论中更加明确,因为你似乎开始了.请记住,您始终可以使用 help name_of_command 来显示简短的 Matlab 帮助.

% 定义一个变量来保存电影"应该具有的每秒帧数gif_fps = 24;% 定义保存电影文件名的字符串变量video_filename = 'video.gif';% 创建图 1,将句柄存储在一个变量中,稍后您将需要它fh = 图(1);对于 a = 0:99% 准备文件名,以便您循环数据q = GetDataSDF(['00' num2str(a,'%02d') 'sdf']);% 绘图图像imagesc(q.dist_fn.x_px.Left.data');% 强制 Matlab 实际做绘图(它有时在循环中变得懒惰)绘制;% 对图 fh 进行截图"框架 = getframe(fh);% 截图为图片im = frame2im(frame);% 将图像转为索引图像(gif 格式需要这个)[imind,cm] = rgb2ind(im,256);% 如果第一次循环迭代:创建文件,否则追加到它如果 a == 0;imwrite(imind,cm,video_filename,'gif','Loopcount',inf);别的imwrite(imind,cm,video_filename,'gif','WriteMode','append','DelayTime',1/gif_fps);结尾结尾

再注意:当每个图的数据大小相同时,仅使用 plot(或在本例中为 imagesc)是有意义的命令一次,在以后的循环迭代中用 set(ah,'Ydata',new_y_data) 替换它(或者在这种情况下 set(ah,'CData',q.dist_fn.x_px.Left.data'),其中 ah 是绘图 axes 的句柄(不是绘图 figure!).这个比在每次循环迭代中创建一个全新的图快几个数量级.缺点是每个图的缩放(这里是颜色缩放)将相同.但在我迄今为止处理的每种情况下,这实际上是可取的.

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.

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

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

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


EDIT: 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 );

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

解决方案

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

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

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天全站免登陆