我是matlab的新手,我面临着一些问题。如果有人能帮我,我真的很感激。我目前正在做一个关于人脸检测的项目,我想允许用户在网络摄像头上进行人脸检测。一旦网络摄像头关闭,该功能就会关闭。但问题是,我无法关闭该程序,只能在ctrl+c的帮助下关闭,此外,它没有在axes1中查看,而是在视频播放器中打开。我的代码如下所示:
function varargout = face_tracking(varargin)
% FACE_TRACKING MATLAB code for face_tracking.fig
% FACE_TRACKING, by itself, creates a new FACE_TRACKING or raises the existing
% singleton*.
%
% H = FACE_TRACKING returns the handle to a new FACE_TRACKING or the handle to
% the existing singleton*.
%
% FACE_TRACKING('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in FACE_TRACKING.M with the given input arguments.
%
% FACE_TRACKING('Property','Value',...) creates a new FACE_TRACKING or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before face_tracking_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to face_tracking_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 face_tracking
% Last Modified by GUIDE v2.5 28-Jan-2015 00:39:01
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @face_tracking_OpeningFcn, ...
'gui_OutputFcn', @face_tracking_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 face_tracking is made visible.
function face_tracking_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 face_tracking (see VARARGIN)
% Choose default command line output for face_tracking
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes face_tracking wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = face_tracking_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 when figure1 is resized.
function figure1_ResizeFcn(hObject, eventdata, handles)
% hObject handle to figure1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in cameraon.
function cameraon_Callback(hObject, eventdata, handles)
% hObject handle to cameraon (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
faceDetector = vision.CascadeObjectDetector();
obj = imaq.VideoDevice('winvideo', 1, 'MJPG_320x240', ...
'ROI', [1 1 320 240]);
handles.vid=obj;
videoFrame = step(obj);
%Get a bounding box around the face
bbox = step(faceDetector, videoFrame);
%Check if something was detected, otherwise exit
if numel(bbox) == 0
errordlg('Face not detected. Please try again.');
end
[hueChannel,~,~] = rgb2hsv(videoFrame);
noseDetector = vision.CascadeObjectDetector('Nose');
faceImage = imcrop(videoFrame,bbox);
noseBBox = step(noseDetector,faceImage);
% The nose bounding box is defined relative to the cropped face image.
% Adjust the nose bounding box so that it is relative to the original video
% frame.
noseBBox(1:2) = noseBBox(1:2) + bbox(1:2);
% Create a tracker object.
tracker = vision.HistogramBasedTracker;
initializeObject(tracker, hueChannel, noseBBox);
ROI = get(obj,'ROI');
videoSize = [ROI(3) ROI(4)];
VideoPlayer = vision.VideoPlayer('Position',[300 300 videoSize(1:2)+30]);
% Track the face over successive video frames until the video is finished.
%You could set here a finite number of frames to capture
nFrames=0;
while (nFrames<100)
% Extract the next video frame
videoFrame = step(obj);
% RGB -> HSV
[hueChannel,~,~] = rgb2hsv(videoFrame);
% Track using the Hue channel data
bbox = step(tracker, hueChannel(:,:,1));
% Insert a bounding box around the object being tracked
videoOut = insertObjectAnnotation(videoFrame,'rectangle',bbox,'Face');
% Display the annotated video frame using the video player object
step(VideoPlayer, videoOut);
nFrames=nFrames+1;
end
guidata(handles,hObject);
% Release resources
release(obj);
release(VideoPlayer);
% --- Executes on button press in cameraoff.
function cameraoff_Callback(hObject, eventdata, handles)
% hObject handle to cameraoff (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
clear handles.vid
delete (hObject);
错误消息如下所示:
Error using guidata (line 89)
H must be the handle to a figure or figure descendent.
Error in face_tracking>cameraon_Callback (line 153)
guidata(handles);
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in face_tracking (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
@(hObject,eventdata)face_tracking('cameraon_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
非常感谢您的帮助
由于以下命令,错误发生在while循环结束时的cameraon_Callback
中:
guidata(handles,hObject);
更新存储在guidata
中的数据时,第一个输入参数必须是与数据关联的图形。正如错误消息所说,它必须是一个人物或后代。
尝试将上面的行更改为以前在face_tracking_OpeningFcn
:中使用的行
% Update handles structure
guidata(hObject, handles);
这应该行得通。