MatLab,如何预先分配帧来制作电影?

发布于 2024-12-13 21:18:33 字数 516 浏览 1 评论 0原文

Matlab 有以下制作 avi 格式电影的指南。我的目标是能够通过 powerpoint 播放演示文稿中的视频。

nFrames = 20;
% Preallocate movie structure.
mov(1:nFrames) = struct('cdata', [],...
                    'colormap', []);

% Create movie.
Z = peaks; surf(Z); 
axis tight
set(gca,'nextplot','replacechildren');
for k = 1:nFrames 
surf(sin(2*pi*k/20)*Z,Z)
mov(k) = getframe(gcf);
end

% Create AVI file.
movie2avi(mov, 'myPeaks.avi', 'compression', 'None');

我理解这个示例,并且我不应该将压缩加载到 PowerPoint 中。但是我不明白如何使用 struct 正确地预分配内存。

Matlab has the following guide to making a movie in avi format. My goal is to be able to play the video in my presentation through powerpoint.

nFrames = 20;
% Preallocate movie structure.
mov(1:nFrames) = struct('cdata', [],...
                    'colormap', []);

% Create movie.
Z = peaks; surf(Z); 
axis tight
set(gca,'nextplot','replacechildren');
for k = 1:nFrames 
surf(sin(2*pi*k/20)*Z,Z)
mov(k) = getframe(gcf);
end

% Create AVI file.
movie2avi(mov, 'myPeaks.avi', 'compression', 'None');

I understand this example and that I should have no compression to load into PowerPoint. However I dont understand how to properly preallocate my memory using struct.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风向决定发型 2024-12-20 21:18:33

您可以使用avifile来创建电影,甚至可以使用更新的VideoWriter

avifile

Z = peaks; surf(Z); 
axis tight
set(gca,'nextplot','replacechildren');

vid = avifile('myPeaks.avi', 'fps',15, 'quality',100);
for k = 1:20 
    surf(sin(2*pi*k/20)*Z,Z)
    vid = addframe(vid, getframe(gcf));
end
vid = close(vid);

winopen('myPeaks.avi')

VideoWriter

Z = peaks; surf(Z); 
axis tight
set(gca,'nextplot','replacechildren');

vid = VideoWriter('myPeaks2.avi');
vid.Quality = 100;
vid.FrameRate = 15;
open(vid);
for k = 1:20 
    surf(sin(2*pi*k/20)*Z,Z)
    writeVideo(vid, getframe(gcf));
end
close(vid);

winopen('myPeaks2.avi')

You could use avifile to create the movie or even the newer VideoWriter:

avifile

Z = peaks; surf(Z); 
axis tight
set(gca,'nextplot','replacechildren');

vid = avifile('myPeaks.avi', 'fps',15, 'quality',100);
for k = 1:20 
    surf(sin(2*pi*k/20)*Z,Z)
    vid = addframe(vid, getframe(gcf));
end
vid = close(vid);

winopen('myPeaks.avi')

VideoWriter

Z = peaks; surf(Z); 
axis tight
set(gca,'nextplot','replacechildren');

vid = VideoWriter('myPeaks2.avi');
vid.Quality = 100;
vid.FrameRate = 15;
open(vid);
for k = 1:20 
    surf(sin(2*pi*k/20)*Z,Z)
    writeVideo(vid, getframe(gcf));
end
close(vid);

winopen('myPeaks2.avi')
怎会甘心 2024-12-20 21:18:33

您不需要预先分配。只需初始化mov = []即可。另外,getframe 假定 gcf,因此您可以只使用 mov(k) = getframe()。我同意你想要一个未压缩的视频。 Matlab 附带的编解码器非常有限。如果空间很重要,您可以使用开源工具来压缩视频。

You don't need to pre-allocate. Just initialize mov = []. Also getframe assumes gcf, so you can just use mov(k) = getframe(). I agree that you want an uncompressed video. The codecs that come with Matlab are pretty limited. You could use an open source tool to compress the video if space is important.

只是一片海 2024-12-20 21:18:33

您不必预先分配。它曾经帮助使用 moviein 命令进行预分配,但它不再提供任何性能改进。以下是 MATLAB 的引用:

>> help moviein
moviein Initialize movie frame memory.
moviein is no longer needed as of MATLAB Release 11 (5.3).  
In previous revisions, pre-allocating a movie increased 
performance, but there is no longer a need to pre-allocate 
movies. To create a movie, use something like the 
following example:

  for j=1:n
     plot_command
     M(j) = getframe;
  end
  movie(M)

For code that is compatible with all versions of MATLAB, 
including versions before Release 11 (5.3), use:

  M = moviein(n);
  for j=1:n
     plot_command
     M(:,j) = getframe;
  end
  movie(M)

See also movie, getframe.

You don't have to pre-allocate. It used to help to pre-allocate using moviein command, but it no longer provides any performance improvements. Here is the quote from MATLAB:

>> help moviein
moviein Initialize movie frame memory.
moviein is no longer needed as of MATLAB Release 11 (5.3).  
In previous revisions, pre-allocating a movie increased 
performance, but there is no longer a need to pre-allocate 
movies. To create a movie, use something like the 
following example:

  for j=1:n
     plot_command
     M(j) = getframe;
  end
  movie(M)

For code that is compatible with all versions of MATLAB, 
including versions before Release 11 (5.3), use:

  M = moviein(n);
  for j=1:n
     plot_command
     M(:,j) = getframe;
  end
  movie(M)

See also movie, getframe.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文