使用OpenCV进行人脸识别的三种方法

1 简介

OpenCV从版本2.4开始,加入了一个类FaceRecognizer,使用它可以方便的地进行人脸识别(源代码,在OpenCV的opencv\modules\contrib\doc\facerec\src下)。

目前支持三种算法(BSD license):

Eigenfaces特征脸: createEigenFaceRecognizer()

Fisherfaces: createFisherFaceRecognizer()

LocalBinary Patterns Histograms(局部二值直方图):createLBPHFaceRecognizer()


2 人脸数据库

1) http://face-rec.org/databases/

2) http://face-rec.org

  

  • AT&T Facedatabase The AT&T Facedatabase, sometimes also referred to asORL Database of Faces, contains ten different images of each of 40 distinct subjects. For some subjects, the images were taken at different times, varying the lighting, facial expressions (open / closed eyes, smiling / not smiling) and facial details (glasses / no glasses). All the images were taken against a dark homogeneous background with the subjects in an upright, frontal position (with tolerance for some side movement).

  • Yale Facedatabase A, also known as Yalefaces. The AT&T Facedatabase is good for initial tests, but it’s a fairly easy database. The Eigenfaces method already has a 97% recognition rate on it, so you won’t see any great improvements with other algorithms. The Yale Facedatabase A (also known as Yalefaces) is a more appropriate dataset for initial experiments, because the recognition problem is harder. The database consists of 15 people (14 male, 1 female) each with 11 grayscale images sized 320X243 pixel. There are changes in the light conditions (center light, left light, right light), facial expressions (happy, normal, sad, sleepy, surprised, wink) and glasses (glasses, no-glasses).

    The original images are not cropped and aligned. Please look into the Appendix for a Python script, that does the job for you.

  • Extended Yale Facedatabase B The Extended Yale Facedatabase B contains 2414 images of 38 different people in its cropped version. The focus of this database is set on extracting features that are robust to illumination, the images have almost no variation in emotion/occlusion/... . I personally think, that this dataset is too large for the experiments I perform in this document. You better use the AT&T Facedatabase for intial testing. A first version of the Yale Facedatabase B was used in[BHK97] to see how the Eigenfaces and Fisherfaces method perform under heavy illumination changes.[Lee05] used the same setup to take 16128 images of 28 people. The Extended Yale Facedatabase B is the merge of the two databases, which is now known as Extended Yalefacedatabase B.

3 数据准备的格式和方法

用CSV文件存储下载的人脸数据的路径和标签,路径和标签用分号(;)隔开,格式如下:

  

/path/to/image.ext;0

其中/path/to/image.ext为图片的路径,标签0为人的序号标签。

下载AT&T的数据库和对应的CSV文件,格式是这样的:

./at/s1/1.pgm;0
./at/s1/2.pgm;0
...
./at/s2/1.pgm;1
./at/s2/2.pgm;1
...
./at/s40/1.pgm;39
./at/s40/2.pgm;39

CSV文件的创建可以用OpenCV提供的脚本 create_csv.py完成。

例如;数据的存储路径数如下:

philipp@mango:~/facerec/data/at$ tree
.
|-- s1
|   |-- 1.pgm
|   |-- ...
|   |-- 10.pgm
|-- s2
|   |-- 1.pgm
|   |-- ...
|   |-- 10.pgm
...
|-- s40
|   |-- 1.pgm
|   |-- ...
|   |-- 10.pgm
那么创建CSV文件的方式如下:

philipp@mango:~/facerec/data$ python create_csv.py
at/s13/2.pgm;0
at/s13/7.pgm;0
at/s13/6.pgm;0
at/s13/9.pgm;0
at/s13/5.pgm;0
at/s13/3.pgm;0
at/s13/4.pgm;0
at/s13/10.pgm;0
at/s13/8.pgm;0
at/s13/1.pgm;0
at/s17/2.pgm;1
at/s17/7.pgm;1
at/s17/6.pgm;1
at/s17/9.pgm;1
at/s17/5.pgm;1
at/s17/3.pgm;1
[...]


4 Eigenfaces

4.1 算法描述

2  表示一个随机特征,其中3 .

      1) 计算均值向量 4

                             5
      2) 计算协方差矩阵 S
                                6
       3) 计算S的特征值 7和对应的特征向量   8   
          9
       4)对特征值进行递减排序,特征向量和它顺序一致. K个主成分也就是k个最大的特征值对应的特征向量。

     

      x的K个主成份:

        10

 

       其中11  .

      

       PCA基的重构:

                  12

 

       其中 13 .

    

      然后特征脸通过下面的方式进行人脸识别:

     A.  把所有的训练数据投影到PCA子空间

     B.  把待识别图像投影到PCA子空间

     C.  找到训练数据投影后的向量和待识别图像投影后的向量最近的那个。

 仍然有一个问题有待解决。假设给定400张100*100像素大小的图像,PCA需要解决协方差矩阵 14的求解,而X的大小是10000*400,那么将会得到10000*10000大小的矩阵,大概0.8GB的内存。解决这个问题不容易,所以我们需要另一个计策,就是转置一下再求,特征向量不变化,描述如下:

 线性代数课程中讲到。对于一个 M \times N 的矩阵,如果M > N 你只能得到N - 1 个非零的奇异值. So it’s possible to take the eigenvalue decompositionS = X^{T} X of sizeN \times N instead: X^{T} X v_{i} = \lambda_{i} v{i}and get the original eigenvectors of S = X X^{T} with a left multiplication of the data matrix:X X^{T} (X v_{i}) = \lambda_{i} (X v_{i})

最终的结果奇异值向量是正交的, 要得到单位正交向量需要归一化为单位长度.文献 [Duda01]中有描述。

代码:

/*
 * Copyright (c) 2011. Philipp Wagner <bytefish[at]gmx[dot]de>.
 * Released to public domain under terms of the BSD Simplified license.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
  • 6
    点赞
  • 76
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值