我正在做一个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
也许能给出正确的答案:你可以测量每张照片的眼睛之间的距离或嘴唇的长度等,然后比较...