Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Matlab
Respected sir,
I am doing my project on face recognition using neural network.
I am facing some problems while executing code. please help me.
vid = videoinput('winvideo',1,'RGB24_768x576');
%set(vid,'FramesPerTrigger',1);
%set(vid,'TriggerFrameDelay',1);
%set(vid,'ReturnedColorspace','rgb');
%start(vid)
pic =imread('ali42.jpg');
%stop(vid)
fprintf('Image is acquired\n')
[x,y,z]=size(pic);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% White Balance Correcting
rav=mean2(pic(:,:,1));
gav=mean2(pic(:,:,2));
bav=mean2(pic(:,:,3));
gavg=(rav+bav+gav)/3;
kr=gavg/rav;
kg=gavg/gav;
kb=gavg/bav;
picwb=pic;
picwb(:,:,1)=picwb(:,:,1)*kr;
picwb(:,:,2)=picwb(:,:,2)*kg;
picwb(:,:,3)=picwb(:,:,3)*kb;
fprintf('Detecting the skin\n')
res_orj=zeros(x,y);
res_wb=zeros(x,y);
for i=1:1:x %% RGB skin detection on both corrected and original image
for j=1:1:y
rgb=[pic(i,j,1),pic(i,j,2),pic(i,j,3)];
if rgb(1)>95 && rgb(2)>40 && rgb(3)>20 && (max(rgb)-min(rgb))>15 ...
&& abs(rgb(1)-rgb(2))>15 && rgb(1)>rgb(2) && rgb(1)>rgb(3)
res_orj(i,j)=1;
end
rgbwb=[picwb(i,j,1),picwb(i,j,2),picwb(i,j,3)];
if rgbwb(1)>95 && rgbwb(2)>40 && rgbwb(3)>20 && (max(rgbwb)-min(rgbwb))>15 ...
&& abs(rgbwb(1)-rgbwb(2))>15 && rgbwb(1)>rgbwb(2) &&rgbwb(1)>rgbwb(3)
res_wb(i,j)=1;
end
end
end
res=and(res_orj,res_wb);
res_clear=bwareaopen(res,100);
res_close=imclose(res_clear,strel('square',5));
[B,L] = bwboundaries(res_close,'noholes');
s = regionprops(L, 'all');
fprintf('Finding face candidates\n')
set(figure,'WindowStyle','docked');
imshow(pic,'InitialMagnification','fit')
hold on
num_of_face=0;
for k=1:length(s)
area=[];
text(s(k).Centroid(1),s(k).Centroid(2),num2str(k),'FontWeight','bold','Color','b','Fontsize',15);
ratio(k)=s(k).BoundingBox(1,3)/s(k).BoundingBox(1,4);
fillidity(k)=(s(k).FilledArea-s(k).Area)/s(k).FilledArea;
fillidity(k)=100*fillidity(k);
if (ratio(k)>0.3) && (ratio(k)<1.5) && (fillidity(k)>0.5)
num_of_face=num_of_face+1;
rectangle('Position',s(k).BoundingBox,'EdgeColor','r');
c=s(k,1).ConvexHull(:,1);
r=s(k,1).ConvexHull(:,2);
mask = roipoly(pic,c,r);
fgray=immultiply(mask,res);
fbw=bwareaopen(fgray,50);
[Bf,Lf] = bwboundaries(fbw,'noholes');
sf = regionprops(Lf, 'BoundingBox','Area');
for ii=1:length(sf)
area(ii)=sf(ii).area;
end
[max_area_ind]=max(area);
BB=sf(max_area_ind).BoundingBox;
BB(4)=BB(3)/0.78;
rectangle('Position',BB,'EdgeColor','g','LineWidth',2 );
eval(['faces.face' num2str(num_of_face) '= imcrop(picwb,BB);']);
end
end
hold off
%% Eyes - Mouth Finding
nof=1;
dnof=0;
for nof=1:num_of_face
fprintf('Finding eyes and mouth\n')
right_up_part=[];
left_up_part=[];
left_up_idx=[];
right_up_idx=[];
down_part_idx=[];
mouth_idx=[];
mouth_co=[];
right_eye_idx=[];
left_eye_idx=[];
eval(['face=faces.face' num2str(nof) ';']);
%
b=imfilter((face(:,:,1)),fspecial('log'));
d=imadjust(b);
e=imfilter(d,fspecial('average'));
f=uint8(255/max(e(:))*e*1);
g=im2bw(f,0.9*graythresh(f));
h=bwareaopen(g,50);
%
[Btri,Ltri] = bwboundaries(h,4,'noholes');
stri = regionprops(Ltri, 'Area','Centroid','BoundingBox');
j=h;
j(:,:,2)=h;
j(:,:,3)=h;
j=uint8(j*255);
set(figure,'WindowStyle','docked');
64
imshow(imadd(face,j),'InitialMagnification','fit')
hold on
plot(size(face,2)/2,size(face,1)/2,'bo','MarkerSize',3,'LineWidth',2);
jr=1;
jl=1;
jd=1;
for i=1:length(Btri)
pos=stri(i).Centroid;
if size(face,1)/2>pos(2) && ... % y coor.
pos(1)<0.81*(size(face,2)/2) && ... % x coor.
pos(1)>(size(face,2)/2-0.75*size(face,2)/2) && ... % x coor.
stri(i,1).Area>100 && ...
stri(i,1).BoundingBox(3)/stri(i,1).BoundingBox(4)<4 &&... %% eye
rectangle
stri(i,1).BoundingBox(3)/stri(i,1).BoundingBox(4)>1.2
right_up_part(jr)=(size(face,1)/2-pos(2));
right_up_idx(jr)=(i);
jr=jr+1;
end
if size(face,1)/2>pos(2) && ... % y coor.
pos(1)>size(face,2)/2+0.17*(size(face,2)/2) && ... % x coor.
pos(1)<(size(face,2)/2+0.75*size(face,2)/2) && ... % x coor.
stri(i,1).Area>100 && ...
stri(i,1).BoundingBox(3)/stri(i,1).BoundingBox(4)<4 &&...
stri(i,1).BoundingBox(3)/stri(i,1).BoundingBox(4)>1.2
left_up_part(jl)=(size(face,1)/2-pos(2));
left_up_idx(jl)=(i);
jl=jl+1;
end
if pos(2)> size(face,1)/2 && ...
pos(1)<(size(face,2)/2+0.75*size(face,2)/2) && ...
pos(1)>(size(face,2)/2-0.75*size(face,2)/2) && ...
stri(i,1).Area>50
down_part_idx(jd)=i;
jd=jd+1;
end
end % image is divided to right, left and down regions
if isempty(right_up_part)==0 && ...
isempty(left_up_part)==0 && ...
isempty(down_part_idx)==0
if length(right_up_part)>1 % right eye is chosen
[idx]=min(right_up_part);
right_eye_idx=right_up_idx(idx);
else
right_eye_idx=right_up_idx;
end
if length(left_up_part)>1 % left eye is chosen
[idx]=min(left_up_part);
left_eye_idx=left_up_idx(idx);
else
left_eye_idx=left_up_idx;
end
plot(Btri{right_eye_idx,1}(:,2),Btri{right_eye_idx,1}(:,1),'m','LineWidth',1.7);
plot(Btri{left_eye_idx,1}(:,2),Btri{left_eye_idx,1}(:,1),'m','LineWidth',1.7);
r_eye_co=stri(right_eye_idx).Centroid;
l_eye_co=stri(left_eye_idx).Centroid;
eye_distance=sqrt((l_eye_co(1)-r_eye_co(1))^2+(l_eye_co(2)-r_eye_co(2))^2);
eye_mid(1)=r_eye_co(1)+(l_eye_co(1)-r_eye_co(1))/2;
eye_mid(2)=r_eye_co(2)+(l_eye_co(2)-r_eye_co(2))/2;
plot(eye_mid(1),eye_mid(2),'go','MarkerSize',3,'LineWidth',2);
for i=1:length(down_part_idx)
candidate_co=stri(down_part_idx(i)).Centroid;
r_dis=sqrt((candidate_co(1)-r_eye_co(1))^2+(candidate_co(2)-r_eye_co(2))^2);
l_dis=sqrt((candidate_co(1)-l_eye_co(1))^2+(candidate_co(2)-l_eye_co(2))^2);
mid_dis=sqrt((candidate_co(1)-eye_mid(1))^2+(candidate_co(2)-eye_mid(2))^2);
mouth_check(1,i)=down_part_idx(i);
mouth_check(2,i)=abs(r_dis-l_dis);
mouth_check(3,i)=abs(eye_distance-mid_dis);
mouth_check(4,i)=0.25*eye_distance;
mouth_check(5,i)=mouth_check(2,i)+mouth_check(3,i);
end
% mouth candidates are found
% check Isosceles Triangle possible parts
iso_cond=mouth_check(:,mouth_check(2,:)<mouth_check(4,:)>mouth_check(3,:)<mouth_check(4,:));>
[val,mouth_label]=min(iso_cond(5,:)); mouth_idx=(iso_cond(1,mouth_label)); %
mouth is found
if isempty(mouth_idx)==0 && mouth_idx<=length(stri)
mouth_co=stri(mouth_idx).Centroid;
plot(Btri{mouth_idx,1}(:,2),Btri{mouth_idx,1}(:,1),'m','LineWidth',1.7);
plot(r_eye_co(1),r_eye_co(2),'ro','MarkerSize',9,'LineWidth',3);
plot(l_eye_co(1),l_eye_co(2),'ro','MarkerSize',9,'LineWidth',3);
plot(mouth_co(1),mouth_co(2),'ro','MarkerSize',9,'LineWidth',3);
line([r_eye_co(1) l_eye_co(1)],[r_eye_co(2)
l_eye_co(2)],'LineWidth',2,'Color','r');
line([r_eye_co(1) mouth_co(1)],[r_eye_co(2)
mouth_co(2)],'LineWidth',2,'Color','r');
line([mouth_co(1) l_eye_co(1)],[mouth_co(2)
l_eye_co(2)],'LineWidth',2,'Color','r');
x_right_up=r_eye_co(1)-eye_distance/3;
y_right_up=r_eye_co(2)-eye_distance/3;
x_left_up=l_eye_co(1)+eye_distance/3;
y_left_up=l_eye_co(2)-eye_distance/3;
x_right_down=x_right_up;
y_right_down=mouth_co(2)+eye_distance/3;
x_left_down=l_eye_co(1)+eye_distance/3;
y_left_down=y_right_down;
y_up=min(y_right_up,y_left_up);
BB=[x_right_up,y_up,(x_left_up-x_right_up),(y_left_down-y_up)]; 
%Face image
rectangle is found
plot(x_right_up,y_right_up,'bo','MarkerSize',4,'LineWidth',3)
plot(x_left_up,y_left_up,'co','MarkerSize',4,'LineWidth',3)
plot(x_left_down,y_left_down,'yo','MarkerSize',4,'LineWidth',3)
plot(x_right_down,y_right_down,'go','MarkerSize',4,'LineWidth',3)
rectangle('Position',BB);
detected_face=imcrop(face,BB);
dnof=dnof+1;
eval(['detected_faces.face' num2str(dnof) '=imcrop(face,BB);']);
orient(dnof)=atand((l_eye_co(2)-r_eye_co(2))/(l_eye_co(1)-r_eye_co(1)));
hold off
fprintf('Face validated\n')
set(figure,'WindowStyle','docked');
imshow(detected_face,'InitialMagnification','fit')
end
end
end
 
Please correct it.
Posted 30-Jul-12 1:20am
v2
Comments
Richard MacCutchan at 30-Jul-12 6:49am
   
There is no way anyone is going to correct this for you without more information. Please format your question properly, and add the full details of the problem you face.
Manfred R. Bihy at 30-Jul-12 7:59am
   
Want beer now!
Tadit Dash at 30-Jul-12 9:17am
   
:D :D :D
Marcus Kramer at 30-Jul-12 9:48am
   
It's not even 8:00 AM where I am and I want one too now.
aspnet_regiis at 30-Jul-12 8:06am
   
i am facing some problems while executing code
 
I am facing a lot of problems reading this code...

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You need to be more clear in what your actual error is (be specific, show error codes or describe what's occurring). You can't expect a bunch of strangers helping for free to go through all your code and find anything and everything that's wrong. Improve your question and maybe you'll get better results.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 425
1 OriginalGriff 165
2 DamithSL 130
3 Dave Kreskowiak 120
4 Suvendu Shekhar Giri 110
0 OriginalGriff 7,645
1 DamithSL 5,614
2 Sergey Alexandrovich Kryukov 5,344
3 Maciej Los 4,961
4 Kornfeld Eliyahu Peter 4,539


Advertise | Privacy | Mobile
Web02 | 2.8.141223.1 | Last Updated 30 Jul 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100