Detect face area using Matlab
destination source:https://www.programming-techniques.com/2012/05/detect-face-area-using-matlab.html
Image processing can be used to process various objects in various ways and one for them is the face detection. The face detection system has got many applications and developing such a system is even easier by use of the Image Processing ToolBox available in MATLAB.

The detection of face involves various tasks. They can be summarized in three steps as:-
- reading of a RGB image
- minimisation of background portion
- detection of face object
Here is the code for the detection of the face area. Save it as filename.m file and execute the code in matlab.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | %%%%% Reading of a RGB image i=imread('imagepath'); % for eg. C:P1140119.jpg I=rgb2gray(i); BW=im2bw(I); figure,imshow(BW) %%%%% minimisation of background portion [n1 n2]=size(BW); r=floor(n1/10); c=floor(n2/10); x1=1;x2=r; s=r*c; for i=1:10 y1=1;y2=c; for j=1:10 if (y2<=c | y2>=9*c) | (x1==1 | x2==r*10) loc=find(BW(x1:x2, y1:y2)==0); [o p]=size(loc); pr=o*100/s; if pr<=100 BW(x1:x2, y1:y2)=0; r1=x1;r2=x2;s1=y1;s2=y2; pr1=0; end imshow(BW); end y1=y1+c; y2=y2+c; end x1=x1+r; x2=x2+r; end figure,imshow(BW) %%%%% detection of face object L = bwlabel(BW,8); BB = regionprops(L, 'BoundingBox'); BB1=struct2cell(BB); BB2=cell2mat(BB1); [s1 s2]=size(BB2); mx=0; for k=3:4:s2-1 p=BB2(1,k)*BB2(1,k+1); if p>mx & (BB2(1,k)/BB2(1,k+1))<1.8 mx=p; j=k; end end figure,imshow(I); hold on; rectangle('Position',[BB2(1,j-2),BB2(1,j-1),BB2(1,j),BB2(1,j+1)],'EdgeColor','r' ) |
The sample output for the image read from my computer is shown below:-
The final output is the one with a detected area of the face.
The algorithm can be extended to detect even more precisely the area of the face.

Related Article
destination source:https://www.programming-techniques.com/2012/05/detect-face-area-using-matlab.html