3

我是 MATLAB 的新手。我正在尝试创建一个 GUI 来逐帧播放、暂停、快进和倒带 avi 视频。目前我可以通过切换按钮播放和暂停视频,但是当我再次按下播放时,视频从零帧开始播放。我意识到我需要存储要在下次按播放时使用的帧号,但我不知道该怎么做。任何帮助将非常感激。我意识到 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);
4

1 回答 1

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 中。有几种方法可以做到这一点,一种方法是使用guidata,setappdata或通过参数将其传递给回调。您可以将其称为变量videoPos并将其添加到handles您存储的结构中guidata。还将您的nFrames变量保存在此结构中。

快进将与您显示的相同,但videoPos = videoPos + 1您会这样做videoPos = videoPos + nn您想要的快进速度乘数在哪里。要倒带,只需减少您的videoPos,或将其重置为 1,这取决于您想要什么。

注意:不要忘记添加检查器,您不会想要videoPos小于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不需要是图形句柄,而是它所持有的任何对象,作为您创建的播放按钮。如在 guidata 帮助中:

GUIDATA(H, DATA) 将指定的数据存储在图窗的应用程序数据中。

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

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

于 2013-08-20T17:45:14.010 回答