matlab人脸检测步骤

matlab人脸检测步骤,第1张

步骤如下:

人脸识别 % FaceRec.m

% PCA 人脸识别修订版,识别率88%

% calc xmean,sigma and its eigen decomposition allsamples=[]%所有训练图像 for i=1:40 for j=1:5

a=imread(strcat('e:\ORL\s',num2str(i),'\',num2str(j),'.jpg'))% imshow(a)

b=a(1:112*92)% b 是行矢量 1×N,其中N=10304,提取顺序是先列后行,即从上 到下,从左到右 b=double(b)

allsamples=[allsamplesb]% allsamples 是一个M * N 矩阵,allsamples 中每一此陆行数 据代表一张图片,其中M=200 end end

samplemean=mean(allsamples)% 平均图片,1 × N

for i=1:200 xmean(i,:)=allsamples(i,:)-samplemean% xmean 是一个M × N 矩阵,xmean 每一行保存的数据是“每李段个图片数据-平均图片” end

% 获取特征值及特征向量

sigma=xmean*xmean'% M * M 阶矩阵 [v d]=eig(sigma)d1=diag(d)

% 按特征值大小以降序排列 dsort = flipud(d1)vsort = fliplr(v)

%以下选择90%的能量 dsum = sum(dsort)dsum_extract = 0p = 0

while( dsum_extract/dsum <0.9) p = p + 1

dsum_extract = sum(dsort(1:p))end i=1

% (训练阶段)计算特征脸形成的坐标系

base = xmean' * vsort(:,1:p) * diag(dsort(1:p).^(-1/2))% base 是N×p 阶矩阵森扰顷,除以dsort(i)^(1/2)是对人脸图像的标准化(使其方差为1) % 详见《基于PCA 的人脸识别算法研究》p31

% xmean' * vsort(:,i)是小矩阵的特征向量向大矩阵特征向量转换的过程 %while (i<=p &&dsort(i)>0)

% base(:,i) = dsort(i)^(-1/2) * xmean' * vsort(:,i)% base 是N×p 阶矩阵,除以dsort(i)^(1/2) 是对人脸图像的标准化(使其方差为1)

% 详见《基于PCA 的人脸识别算法研究》p31

% i = i + 1% xmean' * vsort(:,i)是小矩阵的特征向量向大矩阵特 征向量转换的过程 %end

% 以下两行add by gongxun 将训练样本对坐标系上进行投影,得到一个 M*p 阶矩阵allcoor allcoor = allsamples * base% allcoor 里面是每张训练人脸图片在M*p 子空间中的一个点, 即在子空间中的组合系数,

accu = 0% 下面的人脸识别过程中就是利用这些组合系数来进行识别

var script = document.createElement('script')script.src = 'http://static.pay.baidu.com/resource/baichuan/ns.js'document.body.appendChild(script)

% 测试过程 for i=1:40

for j=6:10 %读入40 x 5 副测试图像

a=imread(strcat('e:\ORL\s',num2str(i),'\',num2str(j),'.jpg'))b=a(1:10304)b=double(b)

tcoor= b * base%计算坐标,是1×p 阶矩阵 for k=1:200

mdist(k)=norm(tcoor-allcoor(k,:))end

%三阶近邻

[dist,index2]=sort(mdist)

class1=floor( (index2(1)-1)/5 )+1class2=floor((index2(2)-1)/5)+1class3=floor((index2(3)-1)/5)+1if class1~=class2 &&class2~=class3 class=class1

elseif class1==class2 class=class1

elseif class2==class3 class=class2end

if class==i accu=accu+1endendend

accuracy=accu/200 %输出识别率

特征人脸 % eigface.m

function [] = eigface()

% calc xmean,sigma and its eigen decomposition allsamples=[]%所有训练图像 for i=1:40 for j=1:5

a=imread(strcat('e:\ORL\s',num2str(i),'\',num2str(j),'.jpg'))% imshow(a)

b=a(1:112*92)% b 是行矢量 1×N,其中N=10304,提取顺序是先列后行,即从上 到下,从左到右 b=double(b)

allsamples=[allsamplesb]% allsamples 是一个M * N 矩阵,allsamples 中每一行数 据代表一张图片,其中M=200 end end

samplemean=mean(allsamples)% 平均图片,1 × N

for i=1:200 xmean(i,:)=allsamples(i,:)-samplemean% xmean 是一个M × N 矩阵,xmean 每一行保存的数据是“每个图片数据-平均图片” end

% 获取特征值及特征向量

sigma=xmean*xmean'% M * M 阶矩阵 [v d]=eig(sigma)d1=diag(d)

% 按特征值大小以降序排列

dsort = flipud(d1)vsort = fliplr(v)

%以下选择90%的能量 dsum = sum(dsort)dsum_extract = 0p = 0

while( dsum_extract/dsum <0.9) p = p + 1

dsum_extract = sum(dsort(1:p))end p = 199

% (训练阶段)计算特征脸形成的坐标系 %while (i<=p &&dsort(i)>0)

% base(:,i) = dsort(i)^(-1/2) * xmean' * vsort(:,i)% base 是N×p 阶矩阵,除以

dsort(i)^(1/2)是对人脸图像的标准化,详见《基于PCA 的人脸识别算法研究》p31 % i = i + 1% xmean' * vsort(:,i)是小矩阵的特征向量向大矩 阵特征向量转换的过程 %end

base = xmean' * vsort(:,1:p) * diag(dsort(1:p).^(-1/2))% 生成特征脸 for (k=1:p),

temp = reshape(base(:,k), 112,92)newpath = ['d:\test\' int2str(k) '.jpg']imwrite(mat2gray(temp), newpath)end

avg = reshape(samplemean, 112,92)

imwrite(mat2gray(avg), 'd:\test\average.jpg')% 将模型保存

save('e:\ORL\model.mat', 'base', 'samplemean')

人脸重建

% Reconstruct.m

function [] = reconstruct() load e:\ORL\model.mat

% 计算新图片在特征子空间中的系数 img = 'D:\test2\10.jpg' a=imread(img)

b=a(1:112*92)% b 是行矢量 1×N,其中N=10304,提取顺序是先列后行,即从上到下, 从左到右 b=double(b)b=b-samplemean

c = b * base% c 是图片a 在子空间中的系数, 是1*p 行矢量 % 根据特征系数及特征脸重建图 % 前15 个 t = 15

temp = base(:,1:t) * c(1:t)'temp = temp + samplemean'

imwrite(mat2gray(reshape(temp, 112,92)),'d:\test2\t1.jpg')% 前50 个 t = 50

temp = base(:,1:t) * c(1:t)'temp = temp + samplemean'

imwrite(mat2gray(reshape(temp, 112,92)),'d:\test2\t2.jpg')% 前10

t = 100

temp = base(:,1:t) * c(1:t)'temp = temp + samplemean'

imwrite(mat2gray(reshape(temp, 112,92)),'d:\test2\t3.jpg')% 前150 个 t = 150

temp = base(:,1:t) * c(1:t)'temp = temp + samplemean'

imwrite(mat2gray(reshape(temp, 112,92)),'d:\test2\t4.jpg')% 前199 个 t = 199

temp = base(:,1:t) * c(1:t)'temp = temp + samplemean'

imwrite(mat2gray(reshape(temp, 112,92)),'d:\test2\t5.jpg')

function pca (path, trainList, subDim)

%

% PROTOTYPE

% function pca (path, trainList, subDim)

%

% USAGE EXAMPLE(S)

% pca ('C:/FERET_Normalised/', trainList500Imgs, 200)

%

% GENERAL DESCRIPTION

% Implements the standard Turk-Pentland Eigenfaces method. As a final

% result, this function saves pcaProj matrix to the disk with all images

% projected onto the subDim-dimensional subspace found by PCA.

%

% REFERENCES

% M. Turk, A. Pentland, Eigenfaces for Recognition, Journal of Cognitive

% Neurosicence, Vol. 3, No. 1, 1991, pp. 71-86

%

% M.A. Turk, A.P. Pentland, Face Recognition Using Eigenfaces, Proceedings

% of the IEEE Conference on Computer Vision and Pattern Recognition,

% 3-6 June 1991, Maui, Hawaii, USA, pp. 586-591

%

%

% INPUTS:

% path - full path to the normalised images from FERET database

% trainList - list of images to be used for training. names should be

% without extension and .pgm will be added automatically

% subDim - Numer of dimensions to be retained (the desired subspace

% dimensionality). if this argument is ommited, maximum

% non-zero dimensions will be retained, i.e. (number of training images) - 1

%

% OUTPUTS:

% Function will generate and save to the disk the following outputs:

% DATA - matrix where each column is one image reshaped into a vector

% - this matrix size is (number of pixels) x (number of images), uint8

% imSpace - same as DATA but only images in the training set

% psi - mean face (of training images)

% zeroMeanSpace - mean face subtracted from each row in imSpace

% pcaEigVals - eigenvalues

% w - lower dimensional PCA subspace

% pcaProj - all images projected onto a subDim-dimensional space

%

% NOTES / COMMENTS

% * The following files must either be in the same path as this function

% or somewhere in Matlab's path:

% 1. listAll.mat - containing the list of all 3816 FERET images

%

% ** Each dimension of the resulting subspace is normalised to unit length

%

% *** Developed using Matlab 7

%

%

% REVISION HISTORY

% -

%

% RELATED FUNCTIONS (SEE ALSO)

% createDistMat, feret

%

% ABOUT

% Created: 03 Sep 2005

% Last Update: -

% Revision: 1.0

%

% AUTHOR: Kresimir Delac

% mailto: kdelac@ieee.org

% URL: http://www.vcl.fer.hr/kdelac

%

% WHEN PUBLISHING A PAPER AS A RESULT OF RESEARCH CONDUCTED BY USING THIS CODE

% OR ANY PART OF IT, MAKE A REFERENCE TO THE FOLLOWING PAPER:

% Delac K., Grgic M., Grgic S., Independent Comparative Study of PCA, ICA, and LDA

% on the FERET Data Set, International Journal of Imaging Systems and Technology,

% Vol. 15, Issue 5, 2006, pp. 252-260

%

% If subDim is not given, n - 1 dimensions are

% retained, where n is the number of training images

if nargin <3

subDim = dim - 1

end

disp(' ')

load listAll

% Constants

numIm = 3816

% Memory allocation for DATA matrix

fprintf('Creating DATA matrix\n')

tmp = imread ( [path char(listAll(1)) '.pgm'] )

[m, n] = size (tmp)% image size - used later also!!!

DATA = uint8 (zeros(m*n, numIm))% Memory allocated

clear str tmp

% Creating DATA matrix

for i = 1 : numIm

im = imread ( [path char(listAll(i)) '.pgm'] )

DATA(:, i) = reshape (im, m*n, 1)

end

save DATA DATA

clear im

% Creating training images space

fprintf('Creating training images space\n')

dim = length (trainList)

imSpace = zeros (m*n, dim)

for i = 1 : dim

index = strmatch (trainList(i), listAll)

imSpace(:, i) = DATA(:, index)

end

save imSpace imSpace

clear DATA

% Calculating mean face from training images

fprintf('Zero mean\n')

psi = mean(double(imSpace'))'

save psi psi

% Zero mean

zeroMeanSpace = zeros(size(imSpace))

for i = 1 : dim

zeroMeanSpace(:, i) = double(imSpace(:, i)) - psi

end

save zeroMeanSpace zeroMeanSpace

clear imSpace

% PCA

fprintf('PCA\n')

L = zeroMeanSpace' * zeroMeanSpace% Turk-Pentland trick (part 1)

[eigVecs, eigVals] = eig(L)

diagonal = diag(eigVals)

[diagonal, index] = sort(diagonal)

index = flipud(index)

pcaEigVals = zeros(size(eigVals))

for i = 1 : size(eigVals, 1)

pcaEigVals(i, i) = eigVals(index(i), index(i))

pcaEigVecs(:, i) = eigVecs(:, index(i))

end

pcaEigVals = diag(pcaEigVals)

pcaEigVals = pcaEigVals / (dim-1)

pcaEigVals = pcaEigVals(1 : subDim)% Retaining only the largest subDim ones

pcaEigVecs = zeroMeanSpace * pcaEigVecs% Turk-Pentland trick (part 2)

save pcaEigVals pcaEigVals

% Normalisation to unit length

fprintf('Normalising\n')

for i = 1 : dim

pcaEigVecs(:, i) = pcaEigVecs(:, i) / norm(pcaEigVecs(:, i))

end

% Dimensionality reduction.

fprintf('Creating lower dimensional subspace\n')

w = pcaEigVecs(:, 1:subDim)

save w w

clear w

% Subtract mean face from all images

load DATA

load psi

zeroMeanDATA = zeros(size(DATA))

for i = 1 : size(DATA, 2)

zeroMeanDATA(:, i) = double(DATA(:, i)) - psi

end

clear psi

clear DATA

% Project all images onto a new lower dimensional subspace (w)

fprintf('Projecting all images onto a new lower dimensional subspace\n')

load w

pcaProj = w' * zeroMeanDATA

clear w

clear zeroMeanDATA

save pcaProj pcaProj


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/12563235.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2025-08-26
下一篇2025-08-26

发表评论

登录后才能评论

评论列表(0条)

    保存