how to extract and save frames in a video ??

91 ビュー (過去 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);

サインインしてコメントする。

カテゴリ

Help Center および File Exchange3-D Volumetric Image Processing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by