The simplest face recognition


Here comes the code posted: other version bugs on the Internet are very vague, this note can be realized by pasting into the software

Introduction!
This step is to solve the data reading problem and import the data

%文件1
function ImgData = imgdata()
%用法:  ImgData = imgdata();
%分别导入图片
pic1 = rgb2gray(imread('D:\个人文件\脚本文件\matlab\facemodels\faceres\face1.jpg')); pic1 = imresize(pic1,[150,150]);%读取转换为灰度值图片,尺寸大小设置为长150,宽150
pic2 = rgb2gray(imread('D:\个人文件\脚本文件\matlab\facemodels\faceres\face2.jpg')); pic2 = imresize(pic2,[150,150]);%读取转换为灰度值图片,尺寸大小设置为长150,宽150
pic3 = rgb2gray(imread('D:\个人文件\脚本文件\matlab\facemodels\faceres\face3.jpg')); pic3 = imresize(pic3,[150,150]);%读取转换为灰度值图片,尺寸大小设置为长150,宽150
pic4 = rgb2gray(imread('D:\个人文件\脚本文件\matlab\facemodels\faceres\face4.jpg')); pic4 = imresize(pic4,[150,150]);%读取转换为灰度值图片,尺寸大小设置为长150,宽150
pic5 = rgb2gray(imread('D:\个人文件\脚本文件\matlab\facemodels\faceres\face5.jpg')); pic5 = imresize(pic5,[150,150]);%读取转换为灰度值图片,尺寸大小设置为长150,宽150
pic6 = rgb2gray(imread('D:\个人文件\脚本文件\matlab\facemodels\faceres\face6.jpg')); pic6 = imresize(pic6,[150,150]);%读取转换为灰度值图片,尺寸大小设置为长150,宽150
pic7 =  rgb2gray(imread('D:\个人文件\脚本文件\matlab\facemodels\faceres\kuanleung.jpg')); pic7 = imresize(pic7,[150,150]);%读取转换为灰度值图片,尺寸大小设置为长150,宽150
[m,n] = size(pic1);  %取图片大小
% 下面采用一个细胞体结构的数据类型存储多个矩阵
pic_all = {
    
    pic1,pic2,pic3,pic4,pic5,pic6,pic7};%放入同一个细胞体中
for i=1:7 %这个参数是pic_all的大小,为7
    %把m*n的矩阵变换成1*(m*n)的矩阵
    ImgData(i,:) = reshape(pic_all{
    
    i},1,m*n);
end
%讲数据范围缩小到01之间
ImgData = double(ImgData)/255; %输出预处理的图片
%文件2
function FaceFind = facefind(Cell_all,img2find)
%细胞结构体的调用
img_all = Cell_all{
    
    1};
[m1,n1] = size(img_all);
V = Cell_all{
    
    2};
D = Cell_all{
    
    3};
%namud = 0.5;  %图片缩小的倍数
%对需要识别的图像进行灰度等的处理
%pic = rgb2gray(img2find);  %灰度处理
pic = img2find
pic = imresize(pic,[150,150]);  %变换大小
[m2,n2] = size(pic); 
pic = reshape(pic,1,m2*n2);  %重新排列
pic = double(pic)/255;       
pic_done = pic*V*D;  %处理完的数据
%% 归一化  --》避免运算出现特别大的数据
Ma = max(max(pic_done));
Mi = min(min(pic_done));
pic_done = pic_done/(Ma - Mi);
%%
for i=1:m1
    % 归一化  --》避免运算出现特别大的数据
    Ma1 = max(img_all(i,:));
    Mi1 = min(img_all(i,:));
    img_all(i,:) = img_all(i,:)/(Ma1 - Mi1);
    %求范数--》把他们之间的几何距离作为评判与哪一个人脸最近的标准
    error(i) = norm(img_all(i,:)-pic_done);
end
%找到其中最近的就认为是所要识别的人脸
FaceFind = find(error == min(error));
% FaceFind = error;

This function is an algorithm function, principal component analysis, extracts the main components of face features, and performs error matching through the FaceFind function.

%文件3
function Cell_all = PCA(img,k)
%reshape函数:改变句矩阵的大小,矩阵的总元素个数不能变
%img = [1,2;2,1;3,3;3,6;6,3];
% k = 2;
% img = double(img);
[m,n] = size(img);  %取大小
img_mean = mean(img); %求每列平均值
img_mean_all = repmat(img_mean,m,1);%复制m行平均值至整个矩阵
Z = img - img_mean_all;
T = Z'*Z;%协方差矩阵    
[V,D] = eigs(T,k);%计算T中最大的前k个特征值与特征向量
img_new = img*V*D;  %低维度下的各个脸的数据
Cell_all = {
    
    img_new,V,D};

This is the main function, call all function functions

%文件4
%开始调用
pic8 = rgb2gray(imread('D:\个人文件\脚本文件\matlab\facemodels\faceres\kunleung_shiyan.jpg')); ic8 = imresize(pic8,[150,150]);%读取要识别的图像,进行重新尺寸设置为长150,宽150
ImgData = imgdata()
k = 3    %提取主成分的数量
Cell_all = PCA(ImgData,k)
FaceFind = facefind(Cell_all,pic8)   %进行误差匹配,选择误差最小的
%imshow(FaceFind,[])

Image library:
Insert picture description here
I will not post my profile picture.
Note: Recognizing objects, such as drinking glasses, does not work well

Guess you like

Origin blog.csdn.net/qq_42830971/article/details/111670791