Get the figures that video created

6 次查看(过去 30 天)
Ron
Ron 2022-12-18
回答: Image Analyst 2024-11-7,4:07
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 ?

回答(2 个)

Divyam
Divyam 2024-11-7,3:58
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 2024-11-7,4:07
"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.

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by