Build videos using every Nth image from original video

9 ビュー (過去 30 日間)
Jim McIntyre
Jim McIntyre 2021 年 10 月 6 日
コメント済み: Jim McIntyre 2021 年 11 月 18 日
I have a high speed video of a spinning object. The camera is triggered by the object position and it takes 20 pictures for each trigger. So I want to extract every 20th frame to a separate video as that will give me separate videos of the different positions around the object. I expect the input video to have around 4,000 frames.
What I want is to create one video with frames 1, 21, 41... another with frames 2, 22, 42... etc., up to frames 8, 28, 48...
Here is my current code. Is there a better way to do this?
% variables for extracting fixed position videos
numImagesPerTrigger = 20;
numLocsToOutput = 8;
%% Code to create a movie with every Nth image
% Get properties of the video
vidObjIn = VideoReader(InputFileName);
numFrames = vidObjIn.NumberOfFrames;
FrameRate = vidObjIn.FrameRate;
% Must reopen the video after getting its properties.
vidObjIn = VideoReader(InputFileName);
% Create separate videos for each part
for Offset = 0 : numLocsToOutput - 1
% Create the output file
OutputFileName = char(string(SelectedFilePath) + ...
string(InputFileBaseName)+ '-(' + Offset + ')' + ...
string(InputFileext));
vidObjOut = VideoWriter(OutputFileName, 'MPEG-4');
vidObjOut.FrameRate = 1;
open(vidObjOut);
% Select all frames at this location
for Frame = Offset : numImagesPerTrigger : numFrames - 1
vidObjIn.CurrentTime = (Frame) / FrameRate;
vidFrame = readFrame(vidObjIn);
writeVideo(vidObjOut, vidFrame);
end
% Clean up output file
close(vidObjOut)
end
It seems to me that the line:
vidObjIn.CurrentTime = (Frame) / FrameRate;
is cumbersome. I am concerned that it is possibly prone to error if there are a large number of frames. I would prefer to select the actual frame number directly.

採用された回答

Salman Ahmed
Salman Ahmed 2021 年 11 月 18 日
Hi Jim,
Since you would like to select a Frame from the VideoReader object (vidObjIn) directly, you could use the read function as shown below. Hope this resolves your concern.
vidFrame = read(vidObjIn, Frame);
  1 件のコメント
Jim McIntyre
Jim McIntyre 2021 年 11 月 18 日
Thank you. This is better.

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

その他の回答 (0 件)

カテゴリ

Find more on Video Formats and Interfaces in Help Center and File Exchange

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by