我正在尝试检测照片中的脸部。所以我找到了一个代码来执行此操作,但根据我的图片输入,有时它会找到面部,而对于其他图片则不起作用。
所以我认为是因为照片上的人脸颜色。因此,当我更改阈值时,有时它会起作用。
我的门槛值:
%Threshold
hlow = 0.0; hhigh = 0.4;
slow = 0.3; shigh = 0.8;
vlow = 0.4; vhigh = 0.9;
Blob_Threshold = 120;
Edge_Threshold = 650;
Edge_Tolerance = 500;
Percentage_Threshold = 35;
Tolerance = 0.25;
这是我的代码:
function [Faces_List inx_face] = FaceDetection(img)
%----------------Initialization----------------------
Temp_img = img;
img_height = size(Temp_img,1); % tedade satr
img_width = size(Temp_img,2); % tedade sotoona
Temp_img = im2double(Temp_img);
Skin = zeros(img_height,img_width);
%Threshold
hlow = 0.0; hhigh = 0.4;
slow = 0.3; shigh = 0.8;
vlow = 0.4; vhigh = 0.9;
Blob_Threshold = 120;
Edge_Threshold = 650;
Edge_Tolerance = 500;
Percentage_Threshold = 35;
Tolerance = 0.25;
goldenRatio = (1+sqrt(5))/2;
%----------------Convert Color Space From RGB2HSV----
Temp_img = rgb2hsv(Temp_img);
for i = 1:img_height
for j = 1:img_width
H = Temp_img(i,j,1);
S = Temp_img(i,j,2);
V = Temp_img(i,j,3);
bh = H >= hlow && H <= hhigh;
bs = S >= slow && S <= shigh;
bv = V >= vlow && V <= vhigh;
if (bh && bs && bv)
Skin(i,j) = 1;
end
end
end
figure;imshow(Skin);title('Skin Color Detection');
%----------------Initialization----------------------
%Skin_Blob = bwconncomp(Skin,8);
[Skin_Blob,Blob_Number] = bwlabel(Skin);
BlobData = regionprops(Skin_Blob,'all');
%Blob_Number = Skin_Blob.NumObjects;
inx_face = 0;
Faces_List = [];
%----------------Main Face Detection-----------------
for i = 1:Blob_Number
if (BlobData(i).Area > Blob_Threshold)
myFace = BlobData(i).Image;
figure;imshow(myFace);
%pause(1);
myHeight = size(myFace,1);%height
myWidth = size(myFace,2);%width
b1 = (myHeight/myWidth) >= goldenRatio - Tolerance;
b2 = (myHeight/myWidth) <= goldenRatio + Tolerance;
b3 = (myWidth/myHeight) >= goldenRatio - Tolerance;
b4 = (myWidth/myHeight) <= goldenRatio + Tolerance;
Percentage_skin = (sum(myFace(:)) / (myHeight * myWidth)) * 100;
b5 = Percentage_skin > Percentage_Threshold;
boundingBox = BlobData(i).BoundingBox;
x1 = fix(boundingBox(1));
y1 = fix(boundingBox(2));
x2 = fix(boundingBox(3));
y2 = fix(boundingBox(4));
edgeFace = imcrop(img,[x1 y1 x2 y2]);
imGray = rgb2gray(edgeFace);
imEdge = edge(imGray,'sobel');
sumEdge = sum(imEdge(:));
b6 = sumEdge >= Edge_Threshold - Edge_Tolerance;
b7 = sumEdge <= Edge_Threshold + Edge_Tolerance;
if (((b1 && b2) || (b3 && b4)) && b5 && (b6 && b7))
inx_face = inx_face + 1;
Faces_List(inx_face).x1 = x1;
Faces_List(inx_face).y1 = y1;
Faces_List(inx_face).x2 = x2;
Faces_List(inx_face).y2 = y2;
end
end %end if
end %for i
end%Function