人脸识别课程设计附带代码

更新时间:2024-06-10 10:43:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

人脸识别中图像预处理的研究

1.课程设计目的

随着人工智能技术的兴起,以及人类视觉研究的进展,人们逐渐对人脸图像的机器识别投入很大的热情,并形成了一个人脸图像识别研究领域,这一领域除了它的重大理论价值外,也极具实用价值。

如同人的指纹一样,人脸也具有唯一性,也可用来鉴别一个人的身份。人脸图像的自动识别系统较之指纹识别系统、DNA鉴定等更具方便性,开发研究的实际意义更大。然而人脸图像受很多因素的干扰,给识别带来很大难度。国外对于人脸图像识别的研究较早,现己有实用系统面世,只是对于成像条件要求较苛刻,应用范围也就较窄,国内也有许多科研机构从事这方而的研究,并己取得许多成果。

2. 方法综述

2.1特征脸法(PCA):

把单个图像看成一维向量,众多的一维向量形成了人脸图像特征空间,再将其变换到一个新的相对简单的特征空间,通过计算矩阵的特征值和特征向量,利用图像的代数特征信息,寻找“人脸”、“非人脸”两种模式在该特征空间中的分布规律。

2.2人工神经网络(ANN)法:

通过训练一个网络结构,把模式的统计特性隐含在神经网络的结构和参数之中。基于人工神经网络的方法对于复杂的、难以显式描述的模式,具有独特的优势。

2.3支撑向量机(SVM)法:

在统计学习理论基础上发展出的一种新的模式识别方法,它基于结构风险最小化的原理,较之于基于经验风险最小化的人工神经网络,一些难以逾越的问题,如:模型选择和过学习问题、非线性和维数灾难问题、局部极小点问题等都得到了很大程度上的解决。但是直接使用SVM方法进行人脸识别有两方面的困难:第一,训练时需要求解二次规划问题计算

复杂度高,内存需求量巨大;第二,在非人脸样本不受限制时,需要极大规模的训练集合,得到的支持向量会很多,使得分类器的计算量过高。

2.4基于积分图像特征的人脸检测方法:

是Viola等新近提出的一种算法,它综合使用了积分图像描述方法、Adaboost学习算法及训练方法、级联弱分类器。

3.实验结果与分析

3.1 人脸识别的整体过程

图像获取人脸定位图像预处理人脸识别 图3.1 人脸识别技术处理流程图

3.1.1人脸图像的获取

一般来说,图像的获取都是通过摄像头摄取,但摄取的图像可以是真人,也可以是人脸的图片或者为了相对简单,可以不考虑通过摄像头来摄取头像,而是直接给定要识别的图像。

3.1.2人脸定位

对人脸图像打上网格,对区域块图像做二值分析,通过像素比例来做处理,进而得到人脸区域。

在彩色图像中,颜色是人脸表面最为显著的特征之一,利用颜色检测人脸是很自然的想法。研究人员在考察了不同种族、不同个体的肤色后,认为人类的肤色能在颜色空间中聚成单独的一类,而影响肤色值变化的最主要因素是亮度变化。因此他们采用广泛使用的RGB颜色空间,在滤去亮度值的图像中通过比较像素点的r、g值与肤色范围来推断该像素点及

其邻域是否属于人脸区域。除了RGB颜色空间,还有诸如HIS,LUV,GLHS等其它颜色空间被使用。寻找到肤色区域后,必须进行验证,排除类肤色区域。利用肤色像素的连通性分割出区域,使用椭圆拟合各个区域,根据椭圆长短轴的比率判断是否为人脸。如图3.2所示

原图像网格标记图像。

二值图像标记图像

图3.2人脸定位过程

3.1.3图像预处理

①滤波去噪:空域滤波按照空域滤波器的功能又可分为平滑滤波器和锐化滤波器。平滑滤波器可以用低通滤波实现,目的在于模糊图像或消除噪声;锐化滤波器是用高通滤波来实现,目的在于强调图像被模糊的细节。

②灰度变换:有多种方法可以实现图像的灰度变换,其中最常用的就是直方图变换的方法,即直方图的均衡化。这种方法是一种使输出图像直方图近似服从均匀分布的变换算法。

③边缘检测:数字图像的边缘检测是图像分割、目标区域识别、区域形状提取等图像分析领域十分重要的基础,也是图像识别中提取图像特征的一个重要属性。边缘检测算子可以检查每个像素的邻域并对灰度变化率进行量化,也包括对方向的确定,其中大多数是基于方向导数掩模求卷积的方法。常用的有Sobel算子,Prewitt算子,Roberts算子,Log算子等。

3.4.4人脸识别

把单个图像看成一维向量,众多的一维向量形成了人脸图像特征空间,再将其变换到一

个新的相对简单的特征空间,通过计算矩阵的特征值和特征向量,利用图像的代数特征信息,寻找“人脸”、“非人脸”两种模式在该特征空间中的分布规律。

传统主成成份分析方法的基本原理是:利用K-L变换抽取人脸的主要成分,构成特征脸空间,识别时将测试图像投影到此空间,得到一组投影系数,通过与各个人脸图像比较进行识别。

对于一幅M*N的人脸图像,将其每列相连构成一个大小为D=M*N维的列向量。D就是人脸图像的维数,即是图像空间的维数。

设n是训练样本的数目;Xj表示第j幅人脸图像形成的人脸向量,则所需样本的协方差矩阵为:

其中U为训练样本的平均图像向量:

令A=[x1-u,x2-u,...xn-u],则有Sr=AA,其维数为D×D。根据K-L变换原理,需要求得的新坐标系由矩阵AA的非零特征值所对应的特征向量组成。直接计算的计算量比较大,所以采用奇异值分解(SVD)定理,通过求解AA的特征值和特征向量来获得AA的特征值和特征向量。

依据SVD定理,令li(i=1,2,?,r)为矩阵AA的r个非零特征值,vi为AA对应于li的特征向量,则AA的正交归一特征向量Ui为:

则“特征脸”空间为:w=(U1,U2,...,Un)

将训练样本投影到“特征脸”空间,得到一组投影向量Ω=wu,构成人脸识别的数据库。在识别时,先将每一幅待识别的人脸图像投影到“特征脸”空间,再利用最邻近分类器比较其与库中人脸的位置,从而识别出该图像是否是库中的人脸,如果是,是哪一幅人脸。

T

T

T

T

T

T

T

T

测试图像与对比结果 3.2实验过程具体分析 3.2.1人脸检测

人脸检测的任务是判断静态图像中是否存在人脸。若存在人脸,给出其在图像中的坐标位置、人脸区域大小等信息。而人脸跟踪则需要进一步输出所检测到的人脸位置、大小等状态随时间的连续变化情况。

3.2.2特征提取

通过人脸特征点的检测与标定可以确定人脸图像中显著特征点的位置(如眼睛、眉毛、鼻子、嘴巴等器官),同时还可以得到这些器官及其面部轮廓的形状信息的描述。根据人脸特征点检测与标定的结果,通过某些运算得到人脸特征的描述。

3.2.3基于人脸图像比对的身份识别

通过将输入人脸图像与人脸数据库中的所有已知原型人脸图像计算相似度并对其排序来给出输入人脸的身份信息。这包括两类识别问题:一类是闭集人脸识别问题,即假定输入的人脸一定是人脸库中的某个个体;另一类是开集识别,即首先要对输入人脸是否在已知人脸库中做出判断,如果是,则给出其身份。

3.2.4基于人脸图像比对的身份验证

即人脸确认问题。系统在输入人脸图像的同时输入一个用户宣称的该人脸的身份信息,系统要对该输入人脸图像的身份与宣称的身份是否相符作出判断。

图3.1.4

4.实验结果分析

人脸识别的优势在于其自然性和不被被测个体察觉的特点。人脸识别的困难主要是人脸作为生物特征的特点所带来的。

相似性:不同个体之间的区别不大,所有的人脸的结构都相似,甚至人脸器官的结构外形都很相似。这样的特点对于利用人脸进行定位是有利的,但是对于利用人脸区分人类个体是不利的。

易变性:人脸的外形很不稳定,人可以通过脸部的变化产生很多表情,而在不同观察角度,人脸的视觉图像也相差很大,另外人脸识别还受光照条件、人脸的很多遮盖物、年龄等多方面因素的影响。

特征脸算法(PCA)使得压缩前后的均方误差最小,且变换后的低维空间有很好的分辨能力,但是在种方法在处理人脸图像时,要将二维图像矩阵转换成一维的列向量,使图像的维数达到上万维,计算工作量非常大,特征提取速度慢。

为了克服传统PCA的不足,研究者们相继提出了二维PCA(2DPCA)方法、PCA+2DPCA等一些方法。

这些方法的提出不仅有效地解决了图像处理的高维问题,而且大大提高了人脸的识别率。

5.心得体会

通过本学期对数字图象处理课程的学习,是我对数字图像处理领域有了一定的了解与体会,熟悉了图像处理的流程与方式方法,加深了我对数字领域的理解。通过matlab实验,是我对matlab应用有了更进一步的掌握与学习,为下一步学习打下了良好的基础。

参考文献

[1]阮秋琦.数字图像处理学[M]. 北京: 电子工业出版社, 2000.4 [2]贺兴华. MATLAB7.x图像处理[M]. 北京: 人民邮电出版社, 2006.11 [3]王耀南. 计算机图像处理与识别技术[M]. 北京: 高等教育出版社, 2001.6 [4]章毓晋. 图像工程[M]. 北京: 清华大学出版社, 2001.9 [5]胡学龙. 数字图像处理[M]. 北京: 电子工业出版社, 2006.9

[6]罗军辉. MATLAB7.0在图像处理中的应用[M]. 北京: 机械工业出版社, 2005.6 [7]刘文耀. 数字图像采集与处理[M]. 北京: 电子工业出版社, 2007.8

[8]缪绍纲. 数字图像处理——活用MATLAB [M].成都: 西南交通大学出版社, 2001.7 [9]罗良正.数字图像处理[M]. 南京: 东南大学出版社, 1999.8

附录代码:(运行exampae.m文件) %Ch1

Img = imread('1.jpg'); if ndims(Img) == 3 I=rgb2gray(Img); else I = Img; end

BW = im2bw(I, graythresh(I)); % 二值化 figure; imshow(Img); title('原图像'); hold on;

[xt, yt] = meshgrid(round(linspace(1, size(I, 1), 10)), ... round(linspace(1, size(I, 2), 10)));

mesh(yt, xt, zeros(size(xt)), 'FaceColor', ... 'None', 'LineWidth', 3, ... 'EdgeColor', 'r');

imshow(BW); title('二值图像'); [n1, n2] = size(BW);

r = floor(n1/10); % 分成10块,行 c = floor(n2/10); % 分成10块,列 x1 = 1; x2 = r; % 对应行初始化 s = r*c; % 块面积 for i = 1:10

y1 = 1; y2 = c; % 对应列初始化 for j = 1:10

if (y2<=c || y2>=9*c) || (x1==1 || x2==r*10) % 如果是在四周区域

loc = find(BW(x1:x2, y1:y2)==0); [p, q] = size(loc);

pr = p/s*100; % 黑色像素所占的比例数 if pr <= 100

BW(x1:x2, y1:y2) = 0; end end

y1 = y1+c; % 列跳跃 y2 = y2+c; % 列跳跃 end

x1 = x1+r; % 行跳跃 x2 = x2+r; % 行跳跃 end

[L, num] = bwlabel(BW, 8); % 区域标记

stats = regionprops(L, 'BoundingBox'); % 得到包围矩形框 Bd = cat(1, stats.BoundingBox); [s1, s2] = size(Bd); mx = 0;

for k = 1:s1

p = Bd(k, 3)*Bd(k, 4); % 宽*高 if p>mx && (Bd(k, 3)/Bd(k, 4))<1.8 % 如果满足面积块大,而且宽/高<1.8 mx = p; j = k; end end

imshow(I); hold on;

rectangle('Position', Bd(j, :), ... 'EdgeColor', 'r', 'LineWidth', 3); title('标记图像');

%CreateDatabase

function T = CreateDatabase(TrainDatabasePath)

% Align a set of face images (the training set T1, T2, ... , TM ) %

% Description: This function reshapes all 2D images of the training database

% into 1D column vectors. Then, it puts these 1D column vectors in a row to

% construct 2D matrix 'T'. % %

% Argument: TrainDatabasePath - Path of the training database

%

% Returns: T - A 2D matrix, containing all 1D image vectors.

% Suppose all P images in the training database

% have the same size of MxN. So the length of 1D

% column vectors is MN and 'T' will be a MNxP 2D matrix.

%

% See also: STRCMP, STRCAT, RESHAPE

% Original version by Amir Hossein Omidvarnia, October 2007

% Email: aomidvar@ece.ut.ac.ir

%%%%%%%%%%%%%%%%%%%%%%%% File management TrainFiles = dir(TrainDatabasePath); Train_Number = 0;

for i = 1:size(TrainFiles,1) if

not(strcmp(TrainFiles(i).name,'.')|strcmp(TrainFiles(i).name,'..')|strcmp(TrainFiles(i).name,'Thumbs.db'))

Train_Number = Train_Number + 1; % Number of all images in the training database

end end

%%%%%%%%%%%%%%%%%%%%%%%% Construction of 2D matrix from 1D image vectors

T = [];

for i = 1 : Train_Number

% I have chosen the name of each image in databases as a corresponding

% number. However, it is not mandatory! str = int2str(i);

str = strcat('\\',str,'.jpg');

str = strcat(TrainDatabasePath,str);

img = imread(str); img = rgb2gray(img);

[irow icol] = size(img);

temp = reshape(img',irow*icol,1); % Reshaping 2D images into 1D image vectors

T = [T temp]; % 'T' grows after each turn end

%EigenfaceCore

function [m, A, Eigenfaces] = EigenfaceCore(T)

% Use Principle Component Analysis (PCA) to determine the most % discriminating features between images of faces. %

% Description: This function gets a 2D matrix, containing all training image vectors

% and returns 3 outputs which are extracted from training database. %

% Argument: T - A 2D matrix, containing all 1D image vectors.

% Suppose all P images in the training database

% have the same size of MxN. So the length of 1D

% column vectors is M*N and 'T' will be a MNxP 2D matrix.

%

% Returns: m - (M*Nx1) Mean of the training database

% Eigenfaces - (M*Nx(P-1)) Eigen vectors of the covariance matrix of the training database

% A - (M*NxP) Matrix of centered image vectors

%

% See also: EIG

% Original version by Amir Hossein Omidvarnia, October 2007

% Email: aomidvar@ece.ut.ac.ir

%%%%%%%%%%%%%%%%%%%%%%%% Calculating the mean image

m = mean(T,2); % Computing the average face image m = (1/P)*sum(Tj's) (j = 1 : P)

Train_Number = size(T,2);

%%%%%%%%%%%%%%%%%%%%%%%% Calculating the deviation of each image from mean image

A = [];

for i = 1 : Train_Number

temp = double(T(:,i)) - m; % Computing the difference image for each image in the training set Ai = Ti - m

A = [A temp]; % Merging all centered images end

%%%%%%%%%%%%%%%%%%%%%%%% Snapshot method of Eigenface methos % We know from linear algebra theory that for a PxQ matrix, the maximum

% number of non-zero eigenvalues that the matrix can have is min(P-1,Q-1).

% Since the number of training images (P) is usually less than the number

% of pixels (M*N), the most non-zero eigenvalues that can be found are equal

% to P-1. So we can calculate eigenvalues of A'*A (a PxP matrix) instead of

% A*A' (a M*NxM*N matrix). It is clear that the dimensions of A*A' is much

% larger that A'*A. So the dimensionality will decrease.

L = A'*A; % L is the surrogate of covariance matrix C=A*A'. [V D] = eig(L); % Diagonal elements of D are the eigenvalues for both L=A'*A and C=A*A'.

%%%%%%%%%%%%%%%%%%%%%%%% Sorting and eliminating eigenvalues % All eigenvalues of matrix L are sorted and those who are less than a

% specified threshold, are eliminated. So the number of non-zero % eigenvectors may be less than (P-1).

L_eig_vec = [];

for i = 1 : size(V,2)

if( D(i,i)>1 )

L_eig_vec = [L_eig_vec V(:,i)]; end end

%%%%%%%%%%%%%%%%%%%%%%%% Calculating the eigenvectors of covariance matrix 'C'

% Eigenvectors of covariance matrix C (or so-called \ % can be recovered from L's eiegnvectors.

Eigenfaces = A * L_eig_vec; % A: centered image vectors

%example

% A sample script, which shows the usage of functions, included in

% PCA-based face recognition system (Eigenface method) %

% See also: CREATEDATABASE, EIGENFACECORE, RECOGNITION

% Original version by Amir Hossein Omidvarnia, October 2007

% Email: aomidvar@ece.ut.ac.ir

clear all clc close all

% You can customize and fix initial directory paths TrainDatabasePath

=

uigetdir('D:\\Program

Files\\MATLAB\\R2006a\\work', 'Select training database path' );

TestDatabasePath = uigetdir('D:\\Program

Files\\MATLAB\\R2006a\\work', 'Select test database path');

prompt = {'Enter test image name (a number between 1 to 10):'}; dlg_title = 'Input of PCA-Based Face Recognition System'; num_lines= 1; def = {'1'};

TestImage = inputdlg(prompt,dlg_title,num_lines,def); TestImage

strcat(TestDatabasePath,'\\',char(TestImage),'.jpg');

im = imread(TestImage);

T = CreateDatabase(TrainDatabasePath); [m, A, Eigenfaces] = EigenfaceCore(T);

OutputName = Recognition(TestImage, m, A, Eigenfaces);

SelectedImage = strcat(TrainDatabasePath,'\\',OutputName); SelectedImage = imread(SelectedImage);

imshow(im)

title('Test Image');

figure,imshow(SelectedImage); title('Equivalent Image');

str = strcat('Matched image is : ',OutputName); disp(str)

=

%Recognition

function OutputName = Recognition(TestImage, m, A, Eigenfaces) % Recognizing step.... %

% Description: This function compares two faces by projecting the images into facespace and

% measuring the Euclidean distance between them. %

% Argument: TestImage - Path of the input test image

%

% m - (M*Nx1) Mean of the training

% database, which is output of 'EigenfaceCore' function.

%

% Eigenfaces - (M*Nx(P-1)) Eigen vectors of the

% covariance matrix of the training

% database, which is output of 'EigenfaceCore' function.

%

% A - (M*NxP) Matrix of centered image

% vectors, which is output of 'EigenfaceCore' function.

%

% Returns: OutputName - Name of the recognized image in the training database.

%

% See also: RESHAPE, STRCAT

% Original version by Amir Hossein Omidvarnia, October 2007

% Email: aomidvar@ece.ut.ac.ir

%%%%%%%%%%%%%%%%%%%%%%%% Projecting centered image vectors into facespace

% All centered images are projected into facespace by multiplying in

% Eigenface basis's. Projected vector of each face will be its corresponding

% feature vector.

ProjectedImages = [];

Train_Number = size(Eigenfaces,2); for i = 1 : Train_Number

temp = Eigenfaces'*A(:,i); % Projection of centered images into facespace

ProjectedImages = [ProjectedImages temp]; end

%%%%%%%%%%%%%%%%%%%%%%%% Extracting the PCA features from test image

InputImage = imread(TestImage); temp = InputImage(:,:,1);

[irow icol] = size(temp);

InImage = reshape(temp',irow*icol,1);

Difference = double(InImage)-m; % Centered test image

ProjectedTestImage = Eigenfaces'*Difference; % Test image feature vector

%%%%%%%%%%%%%%%%%%%%%%%% Calculating Euclidean distances % Euclidean distances between the projected test image and the projection

% of all centered training images are calculated. Test image is % supposed to have minimum distance with its corresponding image in the

% training database.

Euc_dist = [];

for i = 1 : Train_Number q = ProjectedImages(:,i);

temp = ( norm( ProjectedTestImage - q ) )^2; Euc_dist = [Euc_dist temp]; end

[Euc_dist_min , Recognized_index] = min(Euc_dist); OutputName = strcat(int2str(Recognized_index),'.jpg');

本文来源:https://www.bwwdw.com/article/8ob6.html

Top