Build videos using every Nth image from original video

조회 수: 2(최근 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 + ')' + ...
vidObjOut = VideoWriter(OutputFileName, 'MPEG-4');
vidObjOut.FrameRate = 1;
% Select all frames at this location
for Frame = Offset : numImagesPerTrigger : numFrames - 1
vidObjIn.CurrentTime = (Frame) / FrameRate;
vidFrame = readFrame(vidObjIn);
writeVideo(vidObjOut, vidFrame);
% Clean up output file
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);

추가 답변(0개)




Community Treasure Hunt

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

Start Hunting!

Translated by