Get the figures that video created

2 visualizzazioni (ultimi 30 giorni)
Ron
Ron il 18 Dic 2022
Risposto: Image Analyst il 7 Nov 2024
Hello ,
I have some script that creating video of alot of figures with videoReader (about 1000+) , i'm trying to upload the video with VideoReader (.avi)
and get back all the figures or access to those figures from another matlab code , is that possible ?

Risposte (2)

Divyam
Divyam il 7 Nov 2024
Hi @Ron,
Assuming that you have the script that was used to create the video, you can modify the script and use the "savefig" function after the figure plotting code to save your created figures as a ".fig" or an image file.
% Replace with your figure generation code!
for i = 1:numFigures
% Generate figure
fig = figure;
plot(data{i}); % Replace with your plotting code!
% Save the figure
savefig(fig, sprintf('figure_%d.fig', i));
% Optionally save as an image
saveas(fig, sprintf('figure_%d.png', i));
% Close the figure
close(fig);
end
Alternatively, you can use the "VideoReader" function to read your ".avi" file and then loop through each frame of the video. Assuming that each frame represents a figure, you can then save the frame as an image using the "imwrite" function after reading the frame using the "readFrame" function.
videoFile = 'your_video_file.avi'; % Replace with your video file name!
v = VideoReader(videoFile);
% Frame counter
frameNumber = 1;
% Loop through each frame
while hasFrame(v)
% Read the next frame
frame = readFrame(v);
% Create a filename for the image
imageFileName = sprintf('frame_%04d.png', frameNumber);
% Save the frame as an image
imwrite(frame, imageFileName);
% Increment the frame counter
frameNumber = frameNumber + 1;
end
For more information regarding the "readFrame" function, refer to the following documentation: https://www.mathworks.com/help/matlab/ref/videoreader.readframe.html

Image Analyst
Image Analyst il 7 Nov 2024
"i'm trying to upload the video" <== where (what site) are you trying to upload your video file to? Is the destination folder a local/mapped folder on your computer or network?
"get back all the figures or access to those figures from another matlab code" <== To extract all the frames from the movie (which was created from all your figure windows), you can use read. Here is a snippet from my attached full demo that does exactly that:
for frame = 1 : numberOfFrames
% Extract the frame from the movie structure.
thisFrame = read(videoReaderObject, frame);
% Display it
hImage = subplot(2, 2, 1);
image(thisFrame);
caption = sprintf('Frame %4d of %d.', frame, numberOfFrames);
title(caption, 'FontSize', fontSize);
axis('on', 'image'); % Show tick marks and get aspect ratio correct.
drawnow; % Force it to refresh the window.
Once you have the frame, you can create a filename and save it to disk.

Prodotti


Release

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by