How to create a GUI to play, pause, fast forward and rewind video in MATLAB?

Norman picture Norman · Aug 20, 2013 · Viewed 8k times · Source

I am a newbie to MATLAB. I am trying to create a GUI to play, pause, fast forward and rewind an avi video frame by frame. At the moment I can play and pause the video, via a toggle button, but when I press play again the video plays from frame zero. I realise I need to store the frame number to be used the next time I press play but I don't know how to do this. Any help would be much appreciated. I realise there is an implay option in MATLAB but I have some other code to implement which I have already got right and that is why I want to create my own GUI. Below is the code to pause/play the video.

My most recent code looks like,

   function varargout = N_Play_Pause_2(varargin)
% N_PLAY_PAUSE_2 MATLAB code for N_Play_Pause_2.fig
%      N_PLAY_PAUSE_2, by itself, creates a new N_PLAY_PAUSE_2 or raises the existing
%      singleton*.
%
%      H = N_PLAY_PAUSE_2 returns the handle to a new N_PLAY_PAUSE_2 or the handle to
%      the existing singleton*.
%
%      N_PLAY_PAUSE_2('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in N_PLAY_PAUSE_2.M with the given input arguments.
%
%      N_PLAY_PAUSE_2('Property','Value',...) creates a new N_PLAY_PAUSE_2 or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before N_Play_Pause_2_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to N_Play_Pause_2_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help N_Play_Pause_2

% Last Modified by GUIDE v2.5 29-Aug-2013 08:39:38

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @N_Play_Pause_2_OpeningFcn, ...
                   'gui_OutputFcn',  @N_Play_Pause_2_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before N_Play_Pause_2 is made visible.
function N_Play_Pause_2_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to N_Play_Pause_2 (see VARARGIN)

% Choose default command line output for N_Play_Pause_2
handles.output = hObject;
handles.VidObj = VideoReader('x05.avi');
handles.nFrames = handles.VidObj.NumberOfFrames;
handles.videoPos = 1; %Current video position. Starts at 1.

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes N_Play_Pause_2 wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = N_Play_Pause_2_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in togglebutton1.
function togglebutton1_Callback(hObject, eventdata, handles)
% hObject    handle to togglebutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of togglebutton1
while get(hObject,'Value')
    snapshot = read(handles.VidObj,handles.videoPos); % Here we use the stored video position 
     if handles.videoPos >= handles.nFrames % Protect your code not to go be number of frames available
      break;
    end
    handles.videoPos=handles.videoPos+1;     % Increment the stored position
    imshow(snapshot),title(handles.videoPos);
end
guidata(hObject,handles) % Save the modifications done at the handles structure at the figure handle
set(hObject,'Value',false);


% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if get(hObject,'Value')
    snapshot = read(handles.VidObj,handles.videoPos); % Here we use the stored video position 
    handles.videoPos=handles.videoPos+1;     % Increment the stored position
    imshow(snapshot),title(handles.videoPos);
end
guidata(hObject,handles) % Save the modifications done at the handles structure at the figure handle
set(hObject,'Value',false);


% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if get(hObject,'Value')
    snapshot = read(handles.VidObj,handles.videoPos); % Here we use the stored video position 
    handles.videoPos=handles.videoPos-1;     % Increment the stored position
    imshow(snapshot),title(handles.videoPos);
end
guidata(hObject,handles) % Save the modifications done at the handles structure at the figure handle
set(hObject,'Value',false);


% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if get(hObject,'Value')
    snapshot = read(handles.VidObj,handles.videoPos); % Here we use the stored video position 
    handles.videoPos=handles.videoPos+10;     % Increment the stored position
    imshow(snapshot),title(handles.videoPos);
end
guidata(hObject,handles) % Save the modifications done at the handles structure at the figure handle
set(hObject,'Value',false);


% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if get(hObject,'Value')
    snapshot = read(handles.VidObj,handles.videoPos); % Here we use the stored video position 
    handles.videoPos=handles.videoPos-10;     % Increment the stored position
    imshow(snapshot),title(handles.videoPos);
end
guidata(hObject,handles) % Save the modifications done at the handles structure at the figure handle
set(hObject,'Value',false);

Answer

Werner picture Werner · Aug 20, 2013

Final solution

So, I haven't seen that you updated your code. Seeing your code is quite easier, it seems that you are incrementing after you show your image, so if you push the play button it will show the image before

function varargout = N_Play_Pause2(varargin)
% N_PLAY_PAUSE2 MATLAB code for N_Play_Pause2.fig
%      N_PLAY_PAUSE2, by itself, creates a new N_PLAY_PAUSE2 or raises the existing
%      singleton*.
%
%      H = N_PLAY_PAUSE2 returns the handle to a new N_PLAY_PAUSE2 or the handle to
%      the existing singleton*.
%
%      N_PLAY_PAUSE2('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in N_PLAY_PAUSE2.M with the given input arguments.
%
%      N_PLAY_PAUSE2('Property','Value',...) creates a new N_PLAY_PAUSE2 or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before N_Play_Pause2_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to N_Play_Pause2_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help N_Play_Pause2

% Last Modified by GUIDE v2.5 23-Aug-2013 13:50:30

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @N_Play_Pause2_OpeningFcn, ...
                   'gui_OutputFcn',  @N_Play_Pause2_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before N_Play_Pause2 is made visible.
function N_Play_Pause2_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to N_Play_Pause2 (see VARARGIN)

handles.output = hObject;
handles.VidObj = VideoReader('x05.avi');
handles.nFrames = handles.VidObj.NumberOfFrames;
handles.videoPos = 1; % Current video position, starts at first frame.

snapshot = read(handles.VidObj,handles.videoPos); % Here we use the stored video position 
imshow(snapshot),title(handles.videoPos);

% Choose default command line output for N_Play_Pause2
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes N_Play_Pause2 wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = N_Play_Pause2_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in togglebutton1.
function togglebutton1_Callback(hObject, eventdata, handles)
% hObject    handle to togglebutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of togglebutton1

while get(hObject,'Value')  && handles.videoPos < handles.nFrames 
    handles.videoPos=handles.videoPos+1;     % Increment the stored position
    snapshot = read(handles.VidObj,handles.videoPos); % Here we use the stored video position 
    imshow(snapshot),title(handles.videoPos);
    end
end
guidata(hObject,handles) % Save the modifications done at the handles structure at the figure handle

% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if get(hObject,'Value')
    handles.videoPos=handles.videoPos+1;     % Increment the stored position
    snapshot = read(handles.VidObj,handles.videoPos); % Here we use the stored video position 
    imshow(snapshot),title(handles.videoPos);
end
guidata(hObject,handles) % Save the modifications done at the handles structure at the figure handle


% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if get(hObject,'Value') && handles.videoPos>1
    handles.videoPos=handles.videoPos-1;     % Increment the stored position
    snapshot = read(handles.VidObj,handles.videoPos); % Here we use the stored video position 
    imshow(snapshot),title(handles.videoPos);
end
guidata(hObject,handles) % Save the modifications done at the handles structure at the figure handle


% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if get(hObject,'Value') && handles.videoPos<handles.nFrames-9
    handles.videoPos=handles.videoPos+10;     % Increment the stored position
    snapshot = read(handles.VidObj,handles.videoPos); % Here we use the stored video position 
    imshow(snapshot),title(handles.videoPos);
end
guidata(hObject,handles) % Save the modifications done at the handles structure at the figure handle


% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if get(hObject,'Value') && handles.videoPos>10
    handles.videoPos=handles.videoPos-10;     % Increment the stored position
    snapshot = read(handles.VidObj,handles.videoPos); % Here we use the stored video position 
    imshow(snapshot),title(handles.videoPos);
end
guidata(hObject,handles) % Save the modifications done at the handles structure at the figure handle

Seems you got confused a little bit with the coding, I've edited it and commented your errors. They are preceded by % COMMENT:

function varargout = N_Play_Pause(varargin)
% N_PLAY_PAUSE MATLAB code for N_Play_Pause.fig
%      N_PLAY_PAUSE, by itself, creates a new N_PLAY_PAUSE or raises the existing
%      singleton*.
%
%      H = N_PLAY_PAUSE returns the handle to a new N_PLAY_PAUSE or the handle to
%      the existing singleton*.
%
%      N_PLAY_PAUSE('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in N_PLAY_PAUSE.M with the given input arguments.
%
%      N_PLAY_PAUSE('Property','Value',...) creates a new N_PLAY_PAUSE or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before N_Play_Pause_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to N_Play_Pause_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help N_Play_Pause

% Last Modified by GUIDE v2.5 13-Aug-2013 16:26:32

% Begin initialization code - DO NOT EDIT

gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @N_Play_Pause_OpeningFcn, ...
                   'gui_OutputFcn',  @N_Play_Pause_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before N_Play_Pause is made visible.
function N_Play_Pause_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to N_Play_Pause (see VARARGIN)

% Choose default command line output for N_Play_Pause
handles.output = hObject;
handles.VidObj = VideoReader('x05.avi'); % COMMENT: PAY ATTENTION, 's' was missing!
handles.nFrames = handle.VidObj.NumberOfFrames;
handles.videoPos = 1; % Current video position, starts at first frame.

% Update handles structure
guidata(hObject, handles); % Here you are saving the handles method at the hObject, this is the handle from your GUI figure.

% COMMENT: In the original code here, you would save guidata twice, you didn't need that, just save guidata after you make ALL ALTERATIONS IN IT!


% --- Outputs from this function are returned to the command line.
function varargout = N_Play_Pause_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in togglebutton1.
function togglebutton1_Callback(hObject, eventdata, handles)
% hObject    handle to togglebutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of togglebutton1


while get(hObject,'Value')
    snapshot = read(handles.VidObj,handles.videoPos); % Here we use the stored video position 
    imshow(snapshot),title(double2str(handles.videoPos));            
    if handles.videoPos >= handles.nFrames % Protect your code not to go be number of frames available
      break;
    end
    handles.videoPos=handles.videoPos+1;     % Increment the stored position
end
guidata(hObject,handles) % Save the modifications done at the handles structure at the figure handle

Reading your question title I was like, what else you need? Do you also want a desert? But jokes a part, I was wrong, at least you have something working.

I am not an image specialist at matlab, but instead doing a = 0 in your callback function (and therefore reseting your video to start position), you will need to save your video position at your GUI. There are several ways of doing it, one way would be by using guidata, setappdata, or to pass it via arguments to your callbacks. You could call it a variable videoPos and add it to the handles struct that you store with guidata. Also save your nFrames variable in this struct.

The fast forward would be the same as you showed, but instead doing videoPos = videoPos + 1 you would do videoPos = videoPos + n, where n is the speed multiplier you want on the fast forward. To rewind, just decrement your videoPos, or reset it to 1, depending what you want.

Note: Don't forget to add checkers, you won't want your videoPos lesser than 0 or greater than nFrames.


On the function: N_Play_Pause_OpeningFcn Add the following data:

handle.VidObj = VideoReader('x05.avi');
handle.nFrames = VidObj.NumberOfFrames;
handle.videoPos = 1; % Current video position, starts at first frame.

% Update handles structure
guidata(hObject, handles); % Here you are saving the handles method at the hObject, this is the handle from your GUI figure.

Then, at your function togglebutton1_Callback do:

% --- Executes on button press in togglebutton1.
function togglebutton1_Callback(hObject, eventdata, handles)
% hObject    handle to togglebutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of togglebutton1


% You won't need this line anymore a=0;
% If you would like to retrieve the stored guidata you would do it here, by doing **handles=guidata(hObject);** but this is not needed, the handles data stored at the figure handle is already given to you as the third argument internally by the matlab!    

while get(hObject,'Value')
    snapshot = read(VidObj,handles.videoPos); % Here we use the stored video position 
    imshow(snapshot),title(double2str(handles.videoPos));            
    if handles.videoPos >= handles.nFrames % Protect your code not to go be number of frames available
      break;
    end
    handles.videoPos=handles.videoPos+1;     % Increment the stored position
end
guidata(hObject,handles) % Save the modifications done at the handles structure at the figure handle

Note that you need to update the guidata everytime you quit your methods, so that you keep it updated and saved on the figure handle. One more detail is that the object you pass for the guidata don't need to be the figure handle, but any object holden by it, as the play button you have created. As in the guidata help:

GUIDATA(H, DATA) stores the specified data in the figure's application data.

H is a handle that identifies the figure - it can be the figure
itself, or any object contained in the figure.

Now just add more buttons and work with the togglebutton1 method, the fastforward would be the same, but using instead of +1, +n.