MATLAB: How to add a marker to only specific frames in a video

video processing

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.

Best 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.
Related Question