How to save frames to a video?

Dear Matlab Team,
I have a function that creates frames, using getframe, now I need to save all this frames and convert them to the video. Would you please let me know how can I do this?
Thanks :)

 Respuesta aceptada

Image Analyst
Image Analyst el 5 de Nov. de 2024

0 votos

Please see my attached demo on how to make a movie from figures/axes/frames.
Probably the simplest demos is movie_made_from_surf.m. The others are fancier versions that do additional operations.

3 comentarios

Here is a snippet from the full demo that does most of the work. The rest of the demo allows you to replay it and to save it to disk.
%==============================================================================================
% Set up the movie structure.
% Preallocate movie, which will be an array of structures.
% First get a cell array with all the frames.
allTheFrames = cell(numberOfFrames,1);
vidHeight = 344;
vidWidth = 446;
allTheFrames(:) = {zeros(vidHeight, vidWidth, 3, 'uint8')};
% Next get a cell array with all the colormaps.
allTheColorMaps = cell(numberOfFrames,1);
allTheColorMaps(:) = {zeros(256, 3)};
% Now combine these to make the array of structures.
myMovie = struct('cdata', allTheFrames, 'colormap', allTheColorMaps);
% Create a VideoWriter object to write the video out to a new, different file.
% writerObj = VideoWriter('problem_3.avi');
% open(writerObj);
% Need to change from the default renderer to zbuffer to get it to work right.
% openGL doesn't work and Painters is way too slow.
set(gcf, 'renderer', 'zbuffer');
%==============================================================================================
% Create the movie.
% Get a list of x and y coordinates for every pixel in the x-y plane.
[x, y] = meshgrid(x1d, y1d);
% After this loop starts, BE SURE NOT TO RESIZE THE WINDOW AS IT'S SHOWING THE FRAMES, or else you won't be able to save it.
for frameIndex = 1 : numberOfFrames
z = exp(-(x-t(frameIndex)).^2-(y-t(frameIndex)).^2);
cla reset;
% Enlarge figure to full screen.
% set(gcf, 'Units', 'Normalized', 'Outerposition', [0, 0, 1, 1]);
surf(x,y,z);
axis('tight')
zlim([0, 1]);
caption = sprintf('Frame #%d of %d, t = %.1f', frameIndex, numberOfFrames, t(frameIndex));
title(caption, 'FontSize', 15);
drawnow;
thisFrame = getframe(gca);
% Write this frame out to a new video file.
% writeVideo(writerObj, thisFrame);
myMovie(frameIndex) = thisFrame;
end
Neda
Neda el 5 de Nov. de 2024
Thank you !!! I will try this
Neda
Neda el 6 de Nov. de 2024
I have applied your code, in each frame it asked me to save as a video or not. I need to run my code for t = 5000, and it save all the frame in one video. How can I do this?

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Convert Image Type en Centro de ayuda y File Exchange.

Preguntada:

el 5 de Nov. de 2024

Comentada:

el 6 de Nov. de 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by