Open In App

How To Detect Face in Image Processing Using MATLAB?

Last Updated : 05 Oct, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

MATLAB  is a programming platform that is mainly used by engineers and scientists to analyze and design systems. Image processing is a process to perform some operations on an image to get an enhanced image or to extract some useful information from it. Each picture is stored as an array and each pixel is an element of the array. 

Face Detection:

Face detection also called facial detection is an artificial intelligence (AI) based computer technology used to find and identify human faces in digital images.  Face Detection is widely used in projects and software. it can be called a subset of image processing. MATLAB provides the interface to practice and learn image processing skills in an effective manner.

Example 1:

Matlab




% MATLAB program for GeeksforGeeks 
% logo detection
X=imread("monochrome1.png"); 
Y=imread("GeeksforGeeks.svg.png"); 
subplot(2,1,1)   
imshow(X);   
subplot(2,1,2)   
imshow(Y);


Output:

 

Explanation:

  1. HERE “imread” is the command to MATLAB to take input of image address on which we are working. “imread” takes image address which can be taken from the image which is present in MATLAB drive. 
  2. “subplot” is the command we used to add multiple images in a single frame. subplot(2,1,1) works as (row=2, column=1, position=1) .  
  3. This will work as a matrix formation of 2×1 matrix 2 rows, 1 column, and the third number (2,1,1) shows the position of the image and as result, the image is considered to be an element of the matrix. 
  4. The “imshow” command is used to get the desired result. the interface will give you desired output on compiling it.   “imshow” is the fundamental image display function in MATLAB, optimizing figure, axes, and image object property settings for image display.
     

image processing is not limited to a certain scope, it has many uses and features such as changing image color, extracting color, making filters, etc. Any image comprises RGB values which can be extracted from the image to make it different in appearance.

filters are like matrix as same as the size of our image suppose ” image1 is matrix A” and “filter is matrix B” these both matrix gets multiplied and gives a new filtered image [A x B = C]. 

Example 2:

Matlab




% MATLAB code for changing image color 
% gray %changing image color to gray  
 X=imread("monochrome1.png");   
 Y=imread("GeeksforGeeks.svg.png");  
 subplot(2,2,1)  
 imshow(X);  
 subplot(2,2,2)   
 z=im2gray(Y); 
 imshow(z);   
 subplot(2,2,3) 
 imshow(Y); 
 subplot(2,2,4) 
 G=Y(:,:,2); 
 imshow(G);


Output:

 

Explanation:

  1. Adding images, subplot them in the same frame adding image address will remain same in all cases.  
  2. Here new terms are “im2gray” (image to gray ) it changes the color image in gray color. im2gray(RGB) converts the true color image RGB to the grayscale intensity image I. Grayscale inputs to im2gray are returned unchanged. For any device binary scale image is easy and more compatible to use in their functioning, This is one of the advantages.
  3.  “G=Y(:,:,2)” stands for green color and the code shows how we can extract the green color components from an image. 

Now, Facial detection has now become an important feature in today’s technological development there are many advantages of face detection (image processing).

Example 3: 

Matlab




% MATLAB code for face detection  
  Detector=vision.CascadeObjectDetector('EyePairBig');         
  Detector.MinSize=[11 45];                                          
  Detector.MergeThreshold=16;         
  DDetector=vision.CascadeObjectDetector('Mouth');                                           
  DDetector.MinSize=[15 25];                                                                              
  DDetector.MergeThreshold=16;                 
  EDetector=vision.CascadeObjectDetector('Nose');               
  EDetector.MinSize=[15 18];                                                  
  EDetector.MergeThreshold=16;    
  CDetector=vision.CascadeObjectDetector('EyePairSmall');                 
  CDetector.MinSize=[5 22];                                                                                   
  CDetector.MergeThreshold=16;  
    
% Read an images
  I=imread("face.jpg");    
  J=imread("face.jpg");  
  K=imread("face.jpg"); 
  L=imread("face.jpg"); 
  bbox=step(Detector,I);      
  box=step(DDetector,J);           
  cbox=step(EDetector,K);      
  dbox=step(CDetector,L);   
    
% Functions for face fecefeatures detection
  FaceFeature=insertObjectAnnotation(I,'rectangle',bbox,'Detected');    
  FaceFeature2=insertObjectAnnotation(J,'rectangle',box,'Detected'); 
  FaceFeature3=insertObjectAnnotation(K,'rectangle',cbox,'Detected');  
  FaceFeature4=insertObjectAnnotation(L,'rectangle',dbox,'Detected');   
  figure;   
  
% Plot 
  subplot(2,2,1);  
    
% For showing facefeatures
  imshow(FaceFeature);                                                     
  title('EYES detected');                  
  subplot(2,2,2);                             
  imshow(FaceFeature2);                                          
  title('MOUTH detected');                
  subplot(2,2,3);                
  imshow(FaceFeature3);           
  title('NOSE detected');   
  subplot(2,2,4);                   
  imshow(FaceFeature4);          
  title('EYE SMALL detected');


Output:

 

Explanation:

  • It follows viola jones’s algorithm and vision.CascadeObjectDetector is act as a function or command. all 4 features can be easily detected from this code like mouth, nose, etc. as four features will display at the same time eventually code length increases but the concept for all 4 phases is the same. This detector is capable of detecting a variety of objects, including faces and a person’s upper body.  
  • MinSize Size of the smallest object to detect Specify the size of the smallest object to detect, in pixels as a two-element vector, [height width]. Use this property to reduce computation time when the minimum object size is known.  Here Minsize [11 45] height =11 and width = 45 prior to processing the image. Detector.MinSize=[X Y];  denotes coordinates as we get desired output from these. 
  •  insertObjectAnnotation is again used as a command that we want the image in the desired box.  A subplot has the same role to add multiple images in same frame. Insert annotation in image or video stream.   This function inserts labels and corresponding circles or rectangles into an image or video.                     insertObjectAnnotation(I,’rectangle’,box,’Detected’);  shows image ‘I’ we want it in rectangular box as it detects. 
  • MergeThreshold Threshold for merging detections Specify a threshold value as a scalar integer. This property defines the minimum number of colocated detections needed to declare a final detection. MergeThreshold 16 scalar int here is “16” . 
  • Box all other just used as variable store the command to detect image.  In last phase of code we just written the basic steps of subplot and imshow to get the desired result. Subplot will work same to gather images position wise. title we add in last phase determines title one wants to add with the output image or result. 


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads