MATLAB getframe captures whatever is on screen

◇◆丶佛笑我妖孽 提交于 2019-12-13 15:00:09

问题


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

回答1:


OK, found the solution; instead of

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

sending the figure handle directly to addframe is enough:

aviobj = addframe(aviobj, fig);



回答2:


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



回答3:


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




回答4:


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.




回答5:


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.




回答6:


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)


来源:https://stackoverflow.com/questions/8565404/matlab-getframe-captures-whatever-is-on-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!