如何在MATLAB中创建一个GUI来播放,暂停,快进和快退视频?

时间:2013-08-20 15:40:33

标签: matlab user-interface video matlab-guide

我是MATLAB的新手。我正在尝试创建一个GUI来逐帧播放,暂停,快进和快退avi视频。目前我可以通过切换按钮播放和暂停视频,但是当我再次按下播放时,视频将从第0帧播放。我意识到我需要存储下次我按下播放时使用的帧号,但我不知道该怎么做。 任何帮助将非常感激。我意识到在MATLAB中有一个implay选项,但是我还有一些其他的代码可以实现,我已经做对了,这就是我想创建自己的GUI的原因。以下是暂停/播放视频的代码。

我最近的代码看起来像,

   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);

1 个答案:

答案 0 :(得分:1)

最终解决方案

所以,我没有看到你更新了你的代码。看到您的代码非常简单,看起来您在显示图像后正在递增,因此如果您按下播放按钮,它将显示之前的图像

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

似乎你对编码感到困惑,我编辑了它并评论了你的错误。 它们前面有% 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

阅读你的问题标题我就像,你还需要什么?你也想要沙漠吗?但是开玩笑说,我错了,至少你有一些工作。

我不是matlab的图像专家,而是在回调函数中执行a = 0(因此将视频重置为开始位置),您需要将视频位置保存在GUI中。有几种方法可以实现,一种方法是使用guidatasetappdata,或者通过参数将其传递给回调。您可以将其称为变量videoPos,并将其添加到使用handles存储的guidata结构中。同时在此结构中保存nFrames变量。

快进与您展示的相同,但是videoPos = videoPos + 1执行videoPos = videoPos + nnvideoPos是快进的速度乘数。要回放,只需递减videoPos,或将其重置为1,具体取决于您的需要。

注意:不要忘记添加检查器,您不希望0小于nFrames或大于N_Play_Pause_OpeningFcn


在功能上: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. 添加以下数据:

togglebutton1_Callback

然后,在您的函数% --- 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 执行:

guidata

请注意,每次退出方法时都需要更新guidata,以便更新并保存在图形句柄上。另外一个细节是,您为H is a handle that identifies the figure - it can be the figure itself, or any object contained in the figure. 传递的对象不需要是图形句柄,而是任何由它构成的对象,就像您创建的播放按钮一样。就像在guidata帮助中一样:

  

GUIDATA(H,DATA)将指定的数据存储在图中       申请数据。

togglebutton1

现在只需添加更多按钮并使用+n方法,快进将是相同的,但使用而不是+1,{{1}}。