face detection code in image

2 views (last 30 days)
Chandra Shekhar
Chandra Shekhar on 21 Sep 2012
Please any one suggest how to detect face in a image..in following code its detecting faces but closely standing persons face is not detecting properly..two closely faces is displaying as single face..here i used matlab..not cv toolbox. i need to detect individual faces from image and have to extract..please where is the mistake tell me..take any image having so many faces and check it..
clc; clear all; close all; I=imread('test2.jpg'); I=double(I); R=I(:,:,1); G=I(:,:,2); B=I(:,:,3);
cb = 0.148* I(:,:,1) - 0.291* I(:,:,2) + 0.439 * I(:,:,3) + 128; %calculating cb value using formula cr = 0.439 * I(:,:,1) - 0.368 * I(:,:,2) -0.071 * I(:,:,3) + 128; [w h]=size(I(:,:,1)); for i=1:w for j=1:h if 140<=cr(i,j) && cr(i,j)<=165 && 140<=cb(i,j) && cb(i,j)<=195 segment(i,j)=1; else segment(i,j)=0; % end end end
segment(200:end,1:end)=0; %lower plane masking % I(200:end,1:end,:)=0; I1=rgb2gray(I); % imshow(segment);
[m n]=size(segment); morp=bwareaopen(segment,150); %MORPHOLOGICAL Processing%
imag(:,:,1)=R.*morp; imag(:,:,2)=G.*morp; imag(:,:,3)=B.*morp; % figure(1),imshow(uint8(imag));
RI=imag(:,:,1); GI=imag(:,:,2); BI=imag(:,:,3);
L = bwlabel(morp,8); BB = regionprops(L, 'BoundingBox'); bboxes= cat(1, BB.BoundingBox); widths=bboxes(:,3); heights=bboxes(:,4); hByW=heights./widths;
lenRegions=size(bboxes,1); foundFaces=zeros(1,lenRegions);
rgb=label2rgb(L); %figure,imshow(rgb); %title('face candidates');
%%%%%%%%%%%%%%%% CHECK FACE CRITERIONS %%%%%%%%%%%%%%%%%%%%%%%%%%% % figure(2) arr=[]; x=0; for i=1:lenRegions if (hByW(i)>2 hByW(i)<0.75) continue; end
if (heights(i)<10 && widths(i)<10)
continue;
end
CurBB=bboxes(i,:);
XStart=CurBB(1);
YStart=CurBB(2);
WCur=CurBB(3);
HCur=CurBB(4);
% crop current region%
rangeY=int32(YStart):int32(YStart+HCur-1);
rangeX= int32(XStart):int32(XStart+WCur-1);
RIC11=RI(rangeY, rangeX);
GIC=GI(rangeY, rangeX);
BIC=BI(rangeY, rangeX);
RIC1=imresize(RIC11,[100,100]);
R1=R(rangeY, rangeX);
R2=G(rangeY, rangeX);
R3=B(rangeY, rangeX);
R11=imresize(R1,[100,100]);
R22=imresize(R2,[100,100]);
R33=imresize(R3,[100,100]);
RIC(1:100,1:100,1)=histeq(uint8(R11)); %histogram equalization
RIC(1:100,1:100,2)=histeq(uint8(R22));
RIC(1:100,1:100,3)=histeq(uint8(R33));
arr=[arr,RIC];
% subplot(5,4,i)
% imshow(uint8(RIC));
end
figure(3) imshow(arr);
j=0; temp=0; N=length(arr)/100; for i=1:length(arr)/100 temp=temp+arr(1:100,j+1:(i*100),:)./N; %calculating template j=i*100; end temp1=rgb2gray(temp);
  1 Comment
Image Analyst
Image Analyst on 22 Sep 2012
And just how are we supposed to be able to test your code?

Sign in to comment.

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!