【问题标题】:Creating Movie for each Generation of Data [duplicate]为每一代数据创建电影[重复]
【发布时间】:2013-06-01 18:13:32
【问题描述】:

我有以下代码:

figure;
contour(X1,X2,f);

hold on

plot(top(1:size(top,1)), 'rx');

编辑

    figure;
for i = 1: G
    contour(X1,X2,f);

    hold on

    plot(top(1:size(top,1)), 'rx');
end

注意:G 是最大代。 这应该绘制与选定个体叠加的球体轮廓。在个体的每次迭代中,选择最好的个体,并且这些个体一直持续到达到全局最优。我需要以电影形式展示它,如下所示:

当您运行迭代的每个阶段时,随附的幻灯片中都会显示。这就是我想要做的。请问有什么办法吗?

【问题讨论】:

  • 这是一个类似的问题How to create movies on each generation of a for loop in Matlab plot。或者只看VideoWriter 中的最后一个示例。另请参阅getframe。顺便说一句,图片不错!
  • 您使用的是 MATLAB 还是 python?此外,您不应多次重新发布相同的问题。
  • @tcaswell。我正在使用 Matlab。很抱歉多次发帖。
  • @pm89,我看过 videowriter 和 getframe,但他们没有做什么我做什么?也许你编辑上面的代码并在上面实现让我看看它是如何完成的,也许我做得不对。
  • 很好,@pm89 这个问题不仅相似,而且是同一个用户提出的同一个问题。

标签: algorithm matlab evolutionary-algorithm


【解决方案1】:

好的,我现在只是从here 复制和粘贴。 但是我添加了FrameRate(每秒),因为您以后可能想使用(或询问)它。

writerObj = VideoWriter('Your_video.avi');
writerObj .FrameRate = 1; % 1 frames per second animation.
open(writerObj);

fig_h = figure;
for i = 1: G
    contour(X1,X2,f);
    hold on
    plot(top(1:size(top,1)), 'rx');

    frame = getframe(fig_h); % or frame = getframe; since getframe gets gcf.
    writeVideo(writerObj, frame);
end

close(writerObj);

现在您的工作目录中将有一个Your_video.avi 文件。


如果您的 matlab 不支持 VideoWriter,您可以使用 avifilethis 答案(或数学文档示例 here)中提到的相同,如下所示:

aviobj = avifile('Your_video.avi','compression','None', 'fps', 1);

fig_h = figure;
for i = 1:G
    contour(X1,X2,f);
    hold on
    plot(top(1:size(top,1)), 'rx');

    frame = getframe(fig_h); % or frame = getframe; since getframe gets gcf.
    aviobj = addframe(aviobj, frame);
end

aviobj = close(aviobj);

编辑

this question 也指出,可能会出现问题,即捕获的帧是恒定图像。如果你在windows上运行Matlab,这个问题可能是windows in与某些显卡驱动结合造成的,可以按照this answer中的方法解决。

【讨论】:

  • 我已经尝试过您的代码,但它有错误显示“未定义的函数或方法 'VideoWriter' 用于类型 'char' 的输入参数”错误出现在此代码 writerObj = VideoWriter('Your_video.avi ');
  • 那可能你的matlab版本不支持这个命令,我更新一下答案。
  • 我的matlab版本是R2008b。
  • 糟糕,我的错误对不起。 fig_h 应该分配给 figure 而不是 plot 的输出(现在它是正确的)。或者,您也可以对 getframe 命令设置无输入,例如:frame = getframe;
  • 如果你有空,也许它会更快更容易解释。我们可以聊天吗?
猜你喜欢
  • 2016-07-30
  • 1970-01-01
  • 1970-01-01
  • 2020-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多