clear;
[fname,pname] = uigetfile({'*.jpg';'*.bmp';'*.tif';'*.gif'},'Please choose a color picture...');
[u,v] = size(fname);
y = fname(v);
switch y
case 0
errordlg('You Should Load Image File First...','Warning...');
case{'g';'G';'p';'P';'f';'F'}
I = cat(2,pname,fname);
Ori_Face = imread(I);
subplot(2,3,1),imshow(Ori_Face);
otherwise
errordlg('You should load image file first...','Warining...');
end
p = rgb_RGB(Ori_Face);
subplot(2,3,2);imshow(p);
low_pass = 1/9*ones(3);
image_low = filter2(low_pass,p);
subplot(2,3,3);imshow(image_low);
%自适应阈值程序
[i,j]=size(image_low);
previousSkin2 = zeros(i,j);
i= size(Ori_Face,1);
j= size(Ori_Face,2);
changelist = [];
for threshold = 0.55:-0.1:0.05
two_value = zeros(i,j);
two_value(find(image_low>threshold))= 1;
change = sum(sum(two_value-previousSkin2));
changelist = [changelist change];
previousSkin2 = two_value;
end
[C,I] = min(changelist);
optimalThreshold = (7-I)*0.1;
two_value = zeros(i,j);
two_value(find(image_low>optimalThreshold))=1;
subplot(2,3,4);imshow(two_value);
frontalmodel = imread('C:\Users\Administrator\Desktop\templet.jpg');
FaceCoord = [];
imsourcegray = rgb2gray(Ori_Face);
[L,N] = bwlabel(two_value,8);
for i = 1:N
[x,y] = find(bwlabel(two_value)== i);
bwsegment = bwselect(two_value,y,x,8);
numholes = 1-bweuler(bwsegment,4);
if (numholes>=1)
RectCoord = -1;
[m n]= size(bwsegment);
[cx,cy] = center(bwsegment);
bwnohole = bwfill(bwsegment,'holes');
justface = uint8(double(bwnohole) .* double(imsourcegray));
angle = orient(bwsegment,cx,cy);
bw = imrotate(bwsegment,angle,'bilinear');
bw = bwfill(bw,'holes');
[l,r,u,d]= bianjie(bw);
wx = (r-1+1);
ly = (d-u+1);
wratio = ly/wx;
if((0.8<=wratio)&&(wratio<=2))
S = ly*wx;
A = bwarea(bwsegment);
if(A/S>0.35)
[ccorr,mfit,RectCoord] = mobanpipei(justface,frontalmodel,ly,wx,cx,cy,angle);
end
if (ccorr>=0.6)
mfitbw = (mfit>=1);
invbw = xor(mfitbw,ones(size(mfitbw)));
source_with_hole = unit8(double(invbw).*double(imsourcegray));
final_image = unit8(double(source_with_hole)+double(mfit));
subplot(2,3,5);imshow(final_image);
imsourcegray= final_image;
subplot(2,3,6);imshow(Ori_Face);
end
if (RectCoord~=-1)
FaceCoord = [FaceCoord;RectCoord];
end
end
end
end
[numfaces x] = size(FaceCoord);
for i = 1:numfaces
hd = rectangle('Position',FaceCoord(i,,'LineWidth',3);
set(hd,'edgecolor','y');
end
|