File: ef230-2023-08/www/examples/movieread.m Download
%! Example of reading a movie (AVI) file
%! Displays a montage of individual frames in a figure window
%! and creates a file with a montage of frames and displays the file
%  University of Tennessee : EF 230 Spring, 2010 : Will Schleter
 
function main
clear all; close all; clc;
warning off
 %  Alternate videos
% options={'ski-down.avi','shotput.avi','throwing.avi','highjump.avi','golfback.avi','batting.avi','discus1.avi'};

movie_options={'mooncar.avi','moonflag.avi','robonaut.avi','earth.avi'};
server = 'https://ef.engr.utk.edu/ef230/examples/movies';
ch=menu('Select a video',movie_options);
if ch==0
    error('No selection made');
    return;
end;

url=[server '/' movie_options{ch}];
options = weboptions('CertificateFilename','');  % required for https
movfile = websave(movie_options{ch},url, options);
 
% Read in all video frames.
obj=VideoReader(movfile); % create the object
vidFrames = read(obj); % read in the data
nf=size(vidFrames,4); % number of frames
h=get(obj,'height'); % height in pixels
w=get(obj,'width'); % width in pixels
sc = max(h,w); % scale
nc=6; % number of columns for the montage
nr=ceil(nf/nc);
% create a large image to store all frames in
big=zeros([ h*nr w*nc 3],'uint8');
 
figure(1);
set(gca,'ydir','reverse');
axis([0 w*nc 0 h*nr]);
hold on;
title('Movie frames placed in a collage');
 
% Preallocate space for the movie data
mov(1:nf) = ...
    struct('cdata',zeros(h,w,3,'uint8'),...
           'colormap',[]);
for i=1:nf
    img = vidFrames(:,:,:,i);
    mov(i).cdata = img;
    c=mod(i-1,nc)+1; % column number
    r=floor((i-1)/nc)+1; % row number
    ix=(c-1)*w;
    iy=(r-1)*h;
    image([ix+1 ix+w],[iy+1 iy+h],img); % display in figure window
    big(iy+1:iy+h,ix+1:ix+w,:)=img; %place this frame in the big image
    pause(.1);
end
 
% play the movie twice
figure(2);
axis off
title('Simple movie player - playing twice');
movie(gcf,mov,2,obj.FrameRate);
close(2)
 
% our own basic movie player
global  mov cf nf movtitle; % the lazy way to share data between functions
figure(2)
image(mov(1).cdata);
axis equal
movtitle = {'Custom movie player - use right/left arrow keys to change frames'
            'Frame 1'};
title(movtitle);
cf=1;
set(gcf,'WindowKeyPressFcn',@playmovie);
 
% other ways to save/show the output
% imwrite(big,'tmp.jpg'); % output to a file
% web('tmp.jpg'); % display in matlab's web browser
% system('tmp.jpg &'); % display in system's image viewer, & runs in background
 
 
end % main
 
% this is the callback function that gets called every time a key is
% pressed. We use it to change the frame of the movie being displayed
function playmovie(hObject,eventdata)
global mov cf nf movtitle; % the lazy way to share data between functions
movedir = 0;
switch(eventdata.Key)
    case 'leftarrow'
        if cf>1
            movedir = -1;
        end
    case 'rightarrow'
        if cf<nf
            movedir = +1;
        end
end
 
if movedir
    cf = cf + movedir;
    movtitle{2} = sprintf('Frame: %u',cf);
    image(mov(cf).cdata);
    title(movtitle);
    axis equal
end
 
end % playmovie