user192936
user192936

Reputation:

MATLAB getframe captures whatever is on screen

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

Upvotes: 3

Views: 15750

Answers (6)

A Blue Shoe
A Blue Shoe

Reputation: 157

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

Upvotes: 3

shalti
shalti

Reputation: 1

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)

Upvotes: 0

erichlf
erichlf

Reputation: 129

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.

Upvotes: 0

user192936
user192936

Reputation:

OK, found the solution; instead of

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

sending the figure handle directly to addframe is enough:

aviobj = addframe(aviobj, fig);

Upvotes: 4

Nzbuu
Nzbuu

Reputation: 5251

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.

Upvotes: 1

gnovice
gnovice

Reputation: 125854

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

Upvotes: 1

Related Questions