如何在不使用数据集的情况下在Matlab上比较不同的人脸图像?

如何在不使用数据集的情况下在Matlab上比较不同的人脸图像?,matlab,face-recognition,Matlab,Face Recognition,我正在从事一个Matlab人脸识别项目 我要做的是并排阅读同一个人的两张不同图像,并提取他们的局部特征(如眼睛、鼻子和嘴巴) 我的代码读取图像并检测特征,我需要一个函数来比较任意两张人脸图像,并判断它们是否属于同一个人 代码如下所示 clear all; clc % Read the input image I = imread('12.jpg'); I2 = imread('1.jpg'); I=rgb2gray(I); I2=rgb2gray(I2); %Detect objects

我正在从事一个Matlab人脸识别项目

我要做的是并排阅读同一个人的两张不同图像,并提取他们的局部特征(如眼睛、鼻子和嘴巴)

我的代码读取图像并检测特征,我需要一个函数来比较任意两张人脸图像,并判断它们是否属于同一个人

代码如下所示

clear all;
clc

% Read the input image
I = imread('12.jpg');
I2 = imread('1.jpg');
I=rgb2gray(I);
I2=rgb2gray(I2);


%Detect objects using Viola-Jones Algorithm

%To detect Face
FaceDetect = vision.CascadeObjectDetector;
BB=step(FaceDetect,I);
BB1 = insertObjectAnnotation(I,'rectangle',BB,'Face');
hn1 = imhist(BB)./numel(BB);

h1=subplot(1,2,1);

imshow(I);imshow(BB1); hold on




title('Facial Feature Detection');
%To detect Mouth
MouthDetect = vision.CascadeObjectDetector('Mouth','MergeThreshold',180);
BB=step(MouthDetect,I);
 hold on
for i = 1:size(BB,1)
 rectangle('Position',BB(i,:),'LineWidth',2,'LineStyle','-','EdgeColor','r');
end
%To detect Nose
NoseDetect = vision.CascadeObjectDetector('Nose','MergeThreshold',12);
BB=step(NoseDetect,I);

 hold on
for i = 1:size(BB,1)
    rectangle('Position',BB(i,:),'LineWidth',2,'LineStyle','-','EdgeColor','b');
end

%To detect Eyes
EyeDetect = vision.CascadeObjectDetector('EyePairBig');
BB=step(EyeDetect,I);
hold on
rectangle('Position',BB,'LineWidth',2,'LineStyle','-','EdgeColor','g');
%imshow(BB1);
%%%%%%%%%%%%%%%%%%%%%%%%%%---FACIAL FEATURES OF 2ND IMAGE----%%%%%%%%%%%%%%%%%%%%%%%%%%%
%To detect Face
FaceDetect2 = vision.CascadeObjectDetector;
BB2=step(FaceDetect2,I2);
B = insertObjectAnnotation(I2,'rectangle',BB2,'Face');
hn2 = imhist(BB2)./numel(BB2);

h2=subplot(1,2,2);

imshow(I2);imshow(B); hold on


title('Facial Feature Detection');
%To detect Mouth
MouthDetect2 = vision.CascadeObjectDetector('Mouth','MergeThreshold',160);
BB2=step(MouthDetect2,I2);
 hold on
for i = 1:size(BB2,1)
 rectangle('Position',BB2(i,:),'LineWidth',2,'LineStyle','-','EdgeColor','r');
end
%To detect Nose
NoseDetect2 = vision.CascadeObjectDetector('Nose','MergeThreshold',12);
BB2=step(NoseDetect2,I2);

 hold on
for i = 1:size(BB2,1)
    rectangle('Position',BB2(i,:),'LineWidth',2,'LineStyle','-','EdgeColor','b');
end

%To detect Eyes
EyeDetect2 = vision.CascadeObjectDetector('EyePairBig');
BB2=step(EyeDetect2,I2);
hold on
rectangle('Position',BB2,'LineWidth',2,'LineStyle','-','EdgeColor','g');
linkaxes([h2,h1])

score_euclidean = norm(hn1-hn2);
 %score_euclidean = pdist2(feature1, feature2);

%Set a threshold for euclidean similarity where smaller is more similar
euclidean_threshold = 0.1;
disp('Euclidean Compare')
disp(score_euclidean)
if score_euclidean < euclidean_threshold 
    disp('similar images');
else
    disp('dissimilar images');
end
全部清除;
clc
%读取输入图像
I=imread('12.jpg');
I2=imread('1.jpg');
I=RGB2灰色(I);
I2=RGB2灰色(I2);
%使用Viola-Jones算法检测对象
%探脸
FaceDetect=vision.ObjectDetector;
BB=步骤(面部检测,I);
BB1=插入对象注释(I,“矩形”,BB,“面”);
hn1=imhist(BB)。/numel(BB);
h1=子批次(1,2,1);
imshow(I);imshow(BB1);等等
标题(“面部特征检测”);
%探口
MouthDetect=vision.CascadeObjectDetector('Mouth','MergeThreshold',180);
BB=步骤(I);
等等
对于i=1:尺寸(BB,1)
矩形('Position',BB(i,:),'LineWidth',2,'LineStyle','-','EdgeColor','r');
终止
%探鼻
NoseDetect=vision.CascadeObjectDetector('Nose','MergeThreshold',12);
BB=步骤(NoseDetect,I);
等等
对于i=1:尺寸(BB,1)
矩形('Position',BB(i,:),'LineWidth',2,'LineStyle','-','EdgeColor','b');
终止
%探眼
EyeDetect=vision.CascadeObjectDetector('EyePairBig');
BB=步骤(眼睛检测,I);
等等
矩形('Position','BB','LineWidth',2','LineStyle','-','EdgeColor','g');
%imshow(BB1);
%%%%%%%%%%%%%%%%%%%%%%%%%%---第二张图像的面部特征---%%%%%%%%%%%
%探脸
FaceDetect2=vision.ObjectDetector;
BB2=步骤(面检测t2,I2);
B=插入对象注释(I2,'rectangle',BB2,'Face');
hn2=imhist(BB2)。/numel(BB2);
h2=子批次(1,2,2);
imshow(I2);imshow(B);等等
标题(“面部特征检测”);
%探口
MoutDetect2=视觉。CascadeObjectDetector('Mouth','MergeThreshold',160);
BB2=步骤(t2,I2);
等等
对于i=1:尺寸(BB2,1)
矩形('Position',BB2(i,:),'LineWidth',2,'LineStyle','-','EdgeColor','r');
终止
%探鼻
NoseDetect2=视觉。级联对象检测器(“鼻”,“合并阈值”,12);
BB2=步骤(t2,I2);
等等
对于i=1:尺寸(BB2,1)
矩形('Position',BB2(i,:),'LineWidth',2,'LineStyle','-','EdgeColor','b');
终止
%探眼
EyeDetect2=vision.CascadeObjectDetector('EyePairBig');
BB2=步骤(目测t2,I2);
等等
矩形('Position',BB2,'LineWidth',2,'LineStyle','-','EdgeColor','g');
链接轴([h2,h1])
分数=欧几里德范数(hn1-hn2);
%分数=pdist2(特征1,特征2);
%设置欧几里德相似性阈值,其中越小越相似
欧氏阈值=0.1;
disp(‘欧几里德比较’)
disp(分数和欧几里得)
如果分数为欧几里德<欧几里德阈值
disp(“类似图像”);
其他的
disp(“不同图像”);
终止

也许可以给出正确的答案:
您可以测量眼睛之间的距离或嘴唇的长度等,并对每张照片进行比较…

这是一个很难用CascadeObjectDetector解决的问题:它只会返回您请求的特征的边界框。它只进行检测,不进行识别。你看过吗?使用用于识别人脸的功能,您可以潜在地构建一个分类器。我需要一个函数来比较两张人脸,而无需使用数据库!如果没有数据库,这是一个非常困难的问题。执行人脸检测相对简单,但在没有图像数据库的情况下检测两张脸是否相同几乎是不可能的。基于特征的方法在旋转、平移、透视和其他变化中具有鲁棒性。如果没有数据库,你所能依赖的就是你是否看到了一张脸。Checkout FaceNet,它在预训练时,将人脸映射到欧几里得空间。在那里你可以做一些常用的事情,比如最近邻搜索或距离计算。有一些非常流行的实现(例如),尽管它们主要是python/LuaTorch。你需要检查是否有matlab-alternative。根据我的说法,欧几里德距离应该可以做到这一点。我想知道是什么阻止了它。你可以上传一些你结果的图片吗?