打印

MATLAB人脸检测

[复制链接]
620|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
CZX123123|  楼主 | 2018-7-13 19:07 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
function varargout = untitled1(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @untitled1_OpeningFcn, ...
                   'gui_OutputFcn',  @untitled1_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 untitled1 is made visible.
function untitled1_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 untitled1 (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);
axis off





% --- Outputs from this function are returned to the command line.
function varargout = untitled1_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 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)



%%的人脸检测和跟踪使用实时视频采集

%视频流,使用KLT算法。

%显示视频帧。

%创建人脸检测对象。
faceDetector = vision.CascadeObjectDetector();

%创建点跟踪器对象。
pointTracker = vision.PointTracker('MaxBidirectionalError', 2);

%创建webcam对象。
vid = webcam();

% %创建webcam对象。
videoFrame = snapshot(vid);
frameSize = size(videoFrame);

% 创建视频播放器对象。
videoPlayer = vision.VideoPlayer('Position', [100 100 [frameSize(2), frameSize(1)]+30]);


runLoop = true;
numPts = 0;
frameCount = 0;

while runLoop && frameCount < 400
   
    %获取下一帧。
    videoFrame = snapshot(vid);
    videoFrameGray = rgb2gray(videoFrame);
    frameCount = frameCount + 1;
   
    if numPts < 10
        %检测模式。
        bbox = faceDetector.step(videoFrameGray);
        
        if ~isempty(bbox)
            %找到检测区域内的角点。
            points = detectMinEigenFeatures(videoFrameGray, 'ROI', bbox(1, :));
            
            %重新初始化点跟踪器。
            xyPoints = points.Location;
            numPts = size(xyPoints,1);
            release(pointTracker);
            initialize(pointTracker, xyPoints, videoFrameGray);
            
            % %保存点的副本。
            oldPoints = xyPoints;
            
            % 将表示为[x, y, w, h]的矩形转换为an
            % m×2矩阵[x,y]四个角的坐标。这需要能够转换边框以显示脸部的朝向。
            bboxPoints = bbox2points(bbox(1, :));  
            
            % %将方框角转换为[x1 y1 x2 y2 x3 y3 x4 y4]
            % 按insertShape要求的格式。
            bboxPolygon = reshape(bboxPoints', 1, []);
            
            % %在检测到的面周围显示边框。
            videoFrame = insertShape(videoFrame, 'Polygon', bboxPolygon, 'LineWidth', 3);
            
      
        end
        
    else
        %%跟踪模式。
        [xyPoints, isFound] = step(pointTracker, videoFrameGray);
        visiblePoints = xyPoints(isFound, :);
        oldInliers = oldPoints(isFound, :);
               
        numPts = size(visiblePoints, 1);      
        
        if numPts >= 10
            % 估计旧点之间的几何变换和新点。
            [xform, oldInliers, visiblePoints] = estimateGeometricTransform(...
                oldInliers, visiblePoints, 'similarity', 'MaxDistance', 4);            
            
            %将转换应用到边框。
            bboxPoints = transformPointsForward(xform, bboxPoints);
            
            % %将方框角转换为[x1 y1 x2 y2 x3 y3 x4 y4]按insertShape要求的格式。
            bboxPolygon = reshape(bboxPoints', 1, []);            
            
            %在被跟踪的脸部周围显示一个包围盒。
            videoFrame = insertShape(videoFrame, 'Polygon', bboxPolygon, 'LineWidth', 3);
            
         
            
           %重置点。
            oldPoints = visiblePoints;
            setPoints(pointTracker, oldPoints);
        end

    end
        
    %使用视频播放器对象显示带注释的视频帧。
   step(videoPlayer, videoFrame);

    %%检查视频播放窗口是否已关闭。
    runLoop = isOpen(videoPlayer);
end

displayEndOfDemoMessage(mfilename);











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


求解:打开会运行会跳出一个视频界面,怎么把视频界面加到GUI界面的axes1中,急用!求大神解

相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

2

帖子

0

粉丝