当前位置:首页 > 代码 > 正文

matlab人脸检测代码(基于matlab的人脸检测)

admin 发布:2022-12-19 22:08 159


今天给各位分享matlab人脸检测代码的知识,其中也会对基于matlab的人脸检测进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

matlab人脸检测步骤

步骤如下:

人脸识别 % 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=[allsamples; b]; % 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 = 0; p = 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 = ''; 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 )+1; class2=floor((index2(2)-1)/5)+1; class3=floor((index2(3)-1)/5)+1; if class1~=class2 class2~=class3 class=class1;

elseif class1==class2 class=class1;

elseif class2==class3 class=class2; end;

if class==i accu=accu+1; end; end; end;

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=[allsamples; b]; % 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 = 0; p = 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')

LDA人脸识别的matlab程序,谁能帮我看看或者教下我啊??

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:

%

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

matlab中PCA的人脸识别,最后得出的识别率是什么意思啊!

识别率指的是通过人脸识别技术识别正确数占识别总数的百分比。

人脸识别算法分类

基于人脸特征点的识别算法(Feature-based recognition algorithms)。

基于整幅人脸图像的识别算法(Appearance-based recognition algorithms)。

基于模板的识别算法(Template-based recognition algorithms)。

利用神经网络进行识别的算法(Recognition algorithms using neural network)。

神经网络识别

基于光照估计模型理论

提出了基于Gamma灰度矫正的光照预处理方法,并且在光照估计模型的基础上,进行相应的光照补偿和光照平衡策略。

优化的形变统计校正理论

基于统计形变的校正理论,优化人脸姿态;

强化迭代理论

强化迭代理论是对DLFA人脸检测算法的有效扩展;

独创的实时特征识别理论

该理论侧重于人脸实时数据的中间值处理,从而可以在识别速率和识别效能之间,达到最佳的匹配效果

求个MATLAB大神来帮忙解释下,这段人脸检测的代码是基于什么样的算法原理实现的。

这段程序是基于Viola-Jones 算法检测人脸、鼻子、眼睛的,其过程是先用faceDetector = vision.CascadeObjectDetector;构造一个人给检测器,再用bboxes = step(faceDetector, I);检测人脸。

基于肤色人脸检测程序 matlab

你这个问题说的太大了,按我的理解,是只检测有没有人脸,而不检测是谁是吧。那就比较简单。

计算的原理是当图中一片区域出现某个颜色的时候(比如黄种人就是黄色,这个颜色往往是个范围)则认为是有人。

我写的程序如下:

f = imread('123.jpg');%人脸的读取图像,往往是一个三维的,比如320*240*3,后面的3表示有rgb三种基色。

[m,n,k]=size(f);%得到维度值,比如m=320;n=240;k=3

jg=zeros(m,n);%定义一个m*n的0矩阵,当发现f里有颜色和人脸一样使,把当前值赋值为1

for i=1:m

for j=1:n

if f(i,j,1)150 f(i,j,1)200 ... %这个颜色范围你可以自己调整,我用的数据

f(i,j,2)150 f(i,j,2)200 ... %是我用屏幕吸色器随便找了人脸看了下大体范围

f(i,j,3)100 f(i,j,3)150 %其中的三个点表示和下面的一行是一个语句。

jg(i,j)=1; %当发现f里有颜色和人脸一样使,把当前值赋值为1

end

end

end

%下面的程序完成:如果某个25的小方块内有20个点以上满足上面的条件则认为有人脸。

aa=0;

for i=1:m-5

for j=1:n-5

if sum(sum(jg(i:i+5,j:j+5)))20

aa=1;

break;

end

end

end

if aa==0

'无人脸'

else

'有人脸'

end

关于matlab人脸检测代码和基于matlab的人脸检测的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

版权说明:如非注明,本站文章均为 AH站长 原创,转载请注明出处和附带本文链接;

本文地址:http://ahzz.com.cn/post/26771.html


取消回复欢迎 发表评论:

分享到

温馨提示

下载成功了么?或者链接失效了?

联系我们反馈

立即下载