MATLAB getframe captures whatever is on screen

2019-07-06 00:19发布

I am trying to create a movie from my MATLAB plot. When I call getframe, it "usually" captures the plot image, but sometimes if there is something else active on the screen (which is normal if I continue to use the computer) it captures whatever window is active. Is there another way to grab the image of the active figure?

e.g.

fig = figure;
aviobj = avifile('sample.avi','compression','None');
for i=1:t
    clf(fig);
    plot(...); % some arbitrary plotting
    hold on;
    plot(...); % some other arbitrary plotting
    axis([0 50 0 50]);
    aviobj = addframe(aviobj, getframe(fig));
end
aviobj = close(aviobj);

6条回答
Ridiculous、
2楼-- · 2019-07-06 00:43

OK, found the solution; instead of

aviobj = addframe(aviobj, getframe(fig));

sending the figure handle directly to addframe is enough:

aviobj = addframe(aviobj, fig);
查看更多
聊天终结者
3楼-- · 2019-07-06 00:46

As someone has already stated you don't have to use getframe, however if you insist on using it you can use

set(fig,'Renderer','zbuffer')

and this should fix your issue.

查看更多
该账号已被封号
4楼-- · 2019-07-06 00:49

The Matlab people are apparently phasing out the avifile and addframe functions in future releases, replacing them with VideoWriter and writeVideo respectively. Unfortunately, this means that the accepted answer will no longer work, since writeVideo doesn't accept the figure handle as the argument.

I've played around with it a bit, and for future reference, the same thing can be accomplished using the undocumented hardcopy function. The following code works perfectly for me, with the added benefit of not even having a plot window pop up, so it does everything completely in the background:

fig = figure('Visible','off');
set(fig,'PaperPositionMode','auto');
writerobj = VideoWriter('sample.avi','Uncompressed AVI');
open(writerobj);

for i=1:t
    clf(fig);
    plot(...); % some arbitrary plotting
    hold on;
    plot(...); % some other arbitrary plotting
    axis([0 50 0 50]);
    figinfo = hardcopy(fig,'-dzbuffer','-r0');
    writeVideo(writerobj, im2frame(figinfo));
end

close(writerobj);
查看更多
smile是对你的礼貌
5楼-- · 2019-07-06 00:49

I may depend on the renderer you're using. If it's 'painters', then you should be OK, but if it's anything else, such as 'OpenGL', then I think it has to get the frame data from the graphics card, which means that if you have something overlapping the window, then that may end up in the output of getframe.

查看更多
ら.Afraid
6楼-- · 2019-07-06 00:52

You can pass the handle of the desired figure or axis to GETFRAME to ensure that it doesn't capture another window.

查看更多
Fickle 薄情
7楼-- · 2019-07-06 00:57

If you use getframe in many subplots, try to add at the end: I think the get frame works fine it just the rendering a bit was unpositioned.

 clf(fig)
% use 1st frame to get dimensions
[h, w, p] = size(frames(1).cdata);
hf = figure; 
% resize figure based on frame's w x h, and place at (150, 150)
set(hf,'Position', [150 150 w h]);
axis off
% Place frames at bottom left
movie(hf,frames,4,30,[0 0 0 0]);
 close(gcf)
查看更多
登录 后发表回答