MATLAB. Overlay plotted lines onto an existing .avi movie

351 views Asked by At

I would like to plot series of known coordinates as lines onto a figure of known width and height. Then import a .avi movie of the same height and width, and then combine these two so that the lines overlay the video.

I've been looking around google and stackoverflow, but not sure where to start. Any pointers are greatfully recieved. I am using Matlab R2015a.

Thanks, Chris

1

There are 1 answers

2
Benoit_11 On BEST ANSWER

Here is a way to do it. The code is commented. I created a dummy movie but if you have one you can skip the first few steps, as indicated. The trick is to use hold on and drawnow to refresh the display correctly. You can add more lines or modify pretty much anything you want. If something is unclear please ask.

clear
clc
close all

%//================
%// Make video. If you already have one skip this step.
xyloObj = VideoReader('xylophone.mp4');

vidWidth = xyloObj.Width;
vidHeight = xyloObj.Height;

mov = struct('cdata',zeros(vidHeight,vidWidth,3,'uint8'),...
    'colormap',[]);
%//================
hf = figure;
set(hf,'position',[150 150 vidWidth vidHeight]);

%// Read frames and put into structure to display in an axes.
k = 1;
while hasFrame(xyloObj)
    mov(k).cdata = readFrame(xyloObj);
    k = k+1;
end

%// For animated gif
filename = 'OverlayGif.gif';

NumFrames = k;
hold on
for n = 1:15

    imshow(mov(n).cdata);

    %// Add line. The drawnow is crucial.
    line([round(vidWidth/4) round(3*vidWidth/4)],[round(vidHeight/2) round(vidHeight/2)],'Color',rand(1,3),'LineWidth',4)
    drawnow

    %// Make animated gif (for demo only. You don't need that).
    frame = getframe(1);
    im = frame2im(frame);
    [imind,cm] = rgb2ind(im,256);
    if n == 1;
        imwrite(imind,cm,filename,'gif', 'Loopcount',inf);
    else
        imwrite(imind,cm,filename,'gif','WriteMode','append');
    end

end

Sample output in an animated gif. You can trash the end of the forloop used to create it.

Hope that helps!