How do I add a marker to only specific frames in a video

I would like to add a visual marker with a fixed position (e.g. top right corner) to my video that is only visible at specific frames. The frames during which I want the marker to be shown are saved in a vector.

2 Comments

Matthias - do you have an array/vector of all the frames? Do you know the indices or positions of the frames (to mark) relative to the larget set?
Yes, I have an array with all the frames and I know the indices of the frames to mark.

Sign in to comment.

 Accepted Answer

Hi,
As you know the indices of frames to mark, you can use readFrame function to read those frames from video and then use insertMarker function to add marker to the frame at specific position. If you want to add markers to frames with indices 2, 4 and 6, your code will look something like this:
% Create object to read video
v = VideoReader('xylophone.mp4');
% Frames to which marker must be inserted
markFrames = [2 4 6];
frameidx = 0;
videoPlayer = vision.VideoPlayer;
while hasFrame(v)
% Read next video frame
frame = readFrame(v);
frameidx = frameidx + 1;
% Check if index of frame is to be marked or not
if any(ismember(markFrames, frameidx))
markedFrame = insertMarker(frame, [50 50]);
videoPlayer(markedFrame);
else
videoPlayer(frame);
end
end
Hope this helps.

5 Comments

This would show only those frames, and would not create a video with all of the frames (most of which would not be modified.)
To create a video file:
% Create object to read video
v = VideoReader('xylophone.mp4');
% Frames to which marker must be inserted
markFrames = [2 4 6];
frameidx = 0;
videoPlayer = vision.VideoPlayer;
% Create object to write video
writerObj = VideoWriter('myVideo.avi');
open(writerObj);
while hasFrame(v)
% Read next video frame
frame = readFrame(v);
frameidx = frameidx + 1;
% Check if index of frame is to be marked or not
if any(ismember(markFrames, frameidx))
markedFrame = insertMarker(frame, [50 50]);
videoPlayer(markedFrame);
% Write marked frame to video
writeVideo(writerObj, markedFrame);
else
videoPlayer(frame);
% Write original frame to video
writeVideo(writerObj, frame);
end
end
This method is extremely slow, how could it be faster? I want to add a marker to selected frames and then export the video in the fastest way possible, I don't want to see it in real time. Thanks in advance!
It is more efficient to writeVideo several frames at a time.
Most efficient if you have the memory would be to read the entire video, replace selected frames with the marked versions, and write out everything in one call.
My understanding is that the video writing library that MATLAB uses is not the most efficient library, so you might be able to improve performance by using a C++ interface or using loadlibrary() to talk to an external library.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!