gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 21:43:09 25 4
gpt4 key购买 nike

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

最佳答案

最终解决方案

所以,我还没有看到您更新了代码。查看您的代码非常容易,您似乎在显示图像后递增,因此如果您按下播放按钮,它将显示之前的图像

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(因此将视频重置为开始位置),您需要将视频位置保存在图形用户界面。有几种方法可以做到这一点,一种方法是使用 guidatasetappdata,或者通过参数将其传递给您的回调。您可以将其称为变量 videoPos 并将其添加到您使用 guidata 存储的 handles 结构中。同时将您的 nFrames 变量保存在此结构中。

快进与您展示的相同,但是您将执行 videoPos = videoPos + 1 而不是执行 videoPos = videoPos + n,其中 n 是你想要的快进速度倍增器。要倒带,只需减少 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) 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.

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

关于matlab - 如何在 MATLAB 中创建一个 GUI 来播放、暂停、快进和快退视频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18339335/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com