Build videos using every Nth image from original video

1 visualizzazione (ultimi 30 giorni)
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.

Risposta accettata

Salman Ahmed
Salman Ahmed il 18 Nov 2021
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);

Più risposte (0)

Prodotti


Release

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by