how to extract and save frames in a video ??

조회 수: 132 (최근 30일)
NAVNEET NAYAN
NAVNEET NAYAN 2017년 4월 4일
이동: DGM 2023년 5월 28일
I am using the following code to read and extract entire frames of a video, but problem is the frames are stored in "multi" as shown in the attached image. It is not stored as 1.jpg, 2.jpg, 3.jpg..... .How to resolve this problem. or tell me how can we extract and store entire frames and how to use it in any for loop.
clc;
clear all;
close all;
tic;
vid=VideoReader('I:\testing\video1.avi');
numFrames = vid.NumberOfFrames;
n=numFrames;
for i = 1:1:n
frames = read(vid,i);
imwrite(frames,['I:\testing\abrupt\' int2str(i), '.jpg']);
end
multi = dir('I:\testing\abrupt\*.jpg*');
for i = 1:1:length(multi)
--------------
--------------
end

채택된 답변

Jan
Jan 2017년 4월 4일
편집: Jan 2017년 4월 4일
The shown code creates the file "1.jpg", "2.jpg" and so on. After getting the file names by dir they are ordered alphabetically, which is not the original order. Solve this by:
Folder = 'I:\testing\abrupt\';
for iFrame = 1:n
frames = read(vid, iFrame);
imwrite(frames, fullfile(Folder, sprintf('%06d.jpg', iFrame)));
end
FileList = dir(fullfile(Folder, '*.jpg'));
for iFile = 1:length(FileList)
aFile = fullfile(Folder, FileList(iFile).name);
img = imread(aFile);
end
fullfile cares about file separators. Now changing the work folder requires a single change in the code only.
"iFile" and "iFrame" is more descritpive than "i", which might shadow the imaginary unit also.
  댓글 수: 7
suman jain
suman jain 2019년 1월 10일
It doesn't work if you just want to read
Samanvay Anand
Samanvay Anand 2023년 5월 27일
Thanks..!

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Image Analyst
Image Analyst 2019년 1월 11일
Try my attached demo, which does exactly that, plus a lot more fun things.
  댓글 수: 12
Image Analyst
Image Analyst 2020년 6월 19일
Try this. But instead of surf(), use imread(), and then rgb2gray() if needed to convert to gray scale.
% Demo to create a movie file from a Gaussian and then optionally save it to disk as a gray scale MP4 video file.
%==============================================================================================
% Initialization code
clear all;
clc;
workspace;
numberOfFrames = 61;
x1d = linspace(-3, 3, numberOfFrames);
y1d = x1d;
t = linspace(0, 5, numberOfFrames);
hFigure = figure;
% Set up the movie structure.
vidHeight = 344;
vidWidth = 446;
% 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.
fullFileName = fullfile(pwd, 'Deleteme.mp4');
% Create a video writer object with that file name.
% The VideoWriter object must have a profile input argument, otherwise you get jpg.
% Determine the format the user specified:
[folder, baseFileName, ext] = fileparts(fullFileName);
switch lower(ext)
case '.jp2'
profile = 'Archival';
case '.mp4'
profile = 'MPEG-4';
otherwise
% Either avi or some other invalid extension.
profile = 'Uncompressed AVI';
end
writerObj = VideoWriter(fullFileName, profile);
open(writerObj);
% 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;
% OPTION : 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.
grayImage = rgb2gray(thisFrame.cdata);
writeVideo(writerObj, grayImage);
end
close(writerObj);
% Display the current folder panel so they can see their newly created file.
cd(folder);
filebrowser;
if contains(computer, 'WIN')
% If using Windows, play the movie in the default video player.
winopen(fullFileName);
end
message = sprintf('Finished creating movie file\n %s.\n\nDone with demo!', fullFileName);
uiwait(helpdlg(message));
Alix Dujardin
Alix Dujardin 2023년 1월 23일
Hello,
Thanks for this code. I know this question is a little bit old but do you know how to extract a frame every seconde and then create a new movie with it? Thanks!

댓글을 달려면 로그인하십시오.


Guram Tsirekizde
Guram Tsirekizde 2019년 3월 16일
I am quite new to matlab, so silly question. how to show for example the first image from matrix above with code? thank you in advance.
  댓글 수: 1
Image Analyst
Image Analyst 2019년 3월 17일
videoObject = VideoReader(movieFullFileName)
% Extract the first frame from the movie structure.
thisFrame = read(videoObject, 1);

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by