How do I save multiple frames from a video?

8 ビュー (過去 30 日間)
Kiersten Horton
Kiersten Horton 2021 年 6 月 28 日
コメント済み: Mili Goyal 2021 年 7 月 7 日
I am trying to extract 10 frames from any video (evenly spaced time intervals throughout its duration). It is only saving one photo (the very first frame). How do I get all 10 frames and save them? I was hoping my third to last line would solve the problem, but it did not. It saves directly to my matlab folder, and I am not too worried about that part.
%Pick desired video file
startingFolder = pwd; % or 'C:\wherever';
if ~exist(startingFolder, 'dir')
% If that folder doesn't exist, just start in the current folder.
startingFolder = pwd;
end
defaultFileName = fullfile(startingFolder, '*.*');
[baseFileName, folder] = uigetfile(defaultFileName, 'Select a file');
if baseFileName == 0
% User clicked the Cancel button.
return;
end
fullFileName = fullfile(folder, baseFileName); % holds chosen video file
obj = VideoReader(fullFileName);
frameReader = readFrame(obj);
get(obj)
% read the total number of frames
numFrames = ceil(obj.FrameRate*obj.Duration); %maybe subtract 1, may not be necessary here
% file format of the frames to be saved in
ST ='.jpg';
% reading and writing the frames
vid = readFrame(obj);
for x = 1 : obj.Duration/10 : numFrames
imwrite(vid(:,:,:,x),strcat('ChosenFrame-',num2str(x),'.jpg'));
end

回答 (1 件)

Mili Goyal
Mili Goyal 2021 年 6 月 28 日
I think the readFrame outside the for loop could be the problem. Try placing it inside the for loop. Refer to the following MATLAB Answer for better clarification.
  2 件のコメント
Kiersten Horton
Kiersten Horton 2021 年 6 月 28 日
I tried doing that, and I am still having the same problem.
Mili Goyal
Mili Goyal 2021 年 7 月 7 日
What exactly is the issue that you are facing now?

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

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

タグ

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by