【图像识别】基于AdaBoost算法的人脸检测matlab代码

267 阅读5分钟

1 简介

人脸检测(face detection)问题最初来源于人脸识别(face recognition),是自动人脸识别系统中的一个关键环节。近几年随着电子商务等应用的发展,使得人脸检测开始作为一个独立的课题,受到研究者的重视。今天,人脸检测的应用背景己经远远超出了人脸识别系统的范畴,在基于内容的检索、数字视频处理、视觉监测等方面有着重要的应用价值。但是到目前为止,人脸检测问题由于自身的复杂性,仍旧未能得到彻底解决。最新的进展是Viola等人的基于积分图像的Adaboost方法,其层叠分类器在人脸检测方面速度快且性能与Rowley的ANN方法基本相同,在此基础上所构建的检测系统是第一个真正实时的人脸检测系统。本文作者结合研究生阶段所参加的科研项目,首先对人脸检测问题作了一些探讨,重点对基于积分图像的Adaboost人脸检测方法进行了研究,在基于Matlab平台仿真实现了基于Adaboost的整个人脸检测系统算法,在综合多个标准人脸库的大型人脸图像库中作了大量实验,最终训练得到一个分类效果较为满意的层叠分类器。 本文除了对基于Adaboost人脸检测算法研究外,还重点研究了人脸中双眼定位的问题。眼睛定位算法是在人脸位置已经确定的基础上进行的,因为眼睛的位置是实现人脸图像几何归一化的必要条件,同时眼睛包含比人脸其他器官更多地信息细节,因此眼睛的准确定位成为人脸识别中非常关键的一环。本文针对眼睛定位主要做了以下两方面的贡献: 第一,提出了一种基于人脸结构特征的眼睛区域搜索的方法。该方法利用人脸“三庭五眼”的结构特征确定人眼在图像中的大致区域。 第二,基于眼睛的几何特征和灰度信息,给出了一种快速、准确的眼睛定位算法,对人脸识别中的眼睛特征提取的研究有一定的帮助。 实验结果表明,最终训练得到的人脸检测层叠分类器在一些标准人脸检测集上取得较好的检测效果,同时经过大量图片的试验,新的眼睛定位算法在人脸图像上取得了快速,准确的定位效果。

​2 部分代码

% CSCi 5561 Spring 2015 - Semester Project
% Authors: Stephen Peyton, Chee Yi Ong
% Team: Who Is This (WIT)
% detectFaces.m - detects face using trained classifiers 
function [faces,faceBound] = detectFaces(img)
% preprocessing by Gaussian filtering
img2 = img; % keep a copy of the original color 3D image
img = imread(img);
img = rgb2gray(img);%灰度化
img = conv2(img,fspecial('gaussian',3,3),'same');%高斯低通滤波

% get image parameters
[m,n] = size(img);

% other variables
scanItr = 8; % can be modified depending on how big face is relative to image可以根据脸相对于图像的大小来修改
% scanItr of 8 works well for images with size about 300x400 scanItr 8适用于大小约300x400的图像
faces = []; % empty by default

% compute integral image
intImg = integralImg(img);

% load finalClassifiers
load '../trainHaar/trainedClassifiers.mat' % 286 classifiers

%%%%% Cascaded Detector Structure: 7 levels, 200 classifiers %%%%%
class1 = selectedClassifiers(1:2,:);
class2 = selectedClassifiers(3:12,:);
class3 = selectedClassifiers(13:20,:);
class4 = selectedClassifiers(21:40,:);
class5 = selectedClassifiers(41:70,:);
class6 = selectedClassifiers(71:150,:);
class7 = selectedClassifiers(151:200,:);

% iterate through each window size/pyramid level
for itr = 1:scanItr
   printout = strcat('Iteration #',int2str(itr),'\n');%打印 第几次放缩
   fprintf(printout);
   for i = 1:2:m-19%垂直或者向下遍历
       if i + 19 > m 
           break; % boundary case check
       end
       for j = 1:2:n-19%向右遍历
           if j + 19 > n
               break; % boundary case check
           end
           window = intImg(i:i+18,j:j+18); % 19x19 window as per training当前坐标的滑窗
           check1 = cascade(class1,window,1);
           if check1 == 1
               check2 = cascade(class2,window,.5);
               if check2 == 1
                   check3 = cascade(class3,window,.5);
                   if check3 == 1
                       check4 = cascade(class4,window,.5);
                       if check4 == 1
                           check5 = cascade(class5,window,.6);
                           if check5 == 1
                               check6 = cascade(class6,window,.6)
                               if check6 == 1
                                   fprintf('Passed level 6 cascade.\n');
                                   check7 = cascade(class7,window,.5);
                                   if check7 == 1
                                       % save rectangular corner coordinates
                                       bounds = [j,i,j+18,i+18,itr];
                                       fprintf('Face detected!\n');
                                       faces = [faces;bounds];%人脸所在的区域 框 一行是一个人脸框
                                   end
                               end
                           end
                       end
                   end
               end
           end
       end
   end
   % create next image pyramid level
   tempImg = imresize(img,.8);%imresize(A,m)表示把图像A放大m倍
   img = tempImg;
  [m,n] = size(img);
   intImg = integralImg(img);%积分图矩阵
end

if size(faces,1) == 0 % no faces detected
  error('No face detected! Try again with a larger value of scanItr.')
end

%%%%% Get Best Bounding Box %%%%%
% upscale rectangular bound coordinates back to base level of pyramid
faceBound = zeros(size(faces,1),4);%size(faces,1)取列,有多少个人脸区域 对应的矩阵5x个行,4列
maxItr = max(faces(:,5)); % higher iterations have larger bounding boxes
                                %更高的迭代具有更大的边界框
for i = 1:size(faces,1) %遍历每一个人脸
   if faces(i,5) ~= maxItr   %不等于
       continue; % only interested in large bounding boxes
   end
   faceBound(i,:) = floor(faces(i,1:4)*1.25^(faces(i,5)-1));%floor向下取整%把检测到的框恢复正常大小(即放大1.25^(itr-1))
end

3 仿真结果

4 参考文献

[1]龙伶敏. 基于Adaboost的人脸检测方法及眼睛定位算法研究[D]. 电子科技大学, 2008.

部分理论引用网络文献,若有侵权联系博主删除。