RTU
Posts:
1
Registered:
5/23/12
|
|
troubleshot this coding
Posted:
May 23, 2012 12:09 PM
|
|
i made a coding for a feature extraction but i am facing some problem while running this coding. can any one help me to run this coding? before running this coding take one image.
%% Start pic =imread('image.jpg');
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 %% Recognition load('D:\matlab\face recognition\database\net26p1.mat') for j=1:dnof fprintf('Trying to Recognize\n') eval(['input=detected_faces.face' num2str(j) ';']); dface=input; input=histeq(rgb2gray(input)); input=imresize(input,[30 30]); input=double(input); input=input(:); output=sim(net,input); output=output*100; [g,i]=max(output); set(figure,'WindowStyle','docked'); imshow(dface,'InitialMagnification','fit') hold on
|
|