How to Detect Face in Image Processing Using MATLAB?


In digital image processing, face detection is a process of identifying face of a person within a digital image or video. It plays vital role in the field computer vision. It is used in various fields such as self−driving cars, facial recognition, face lock systems, emotion analysis, estimation of age, robotics, and many other. MATLAB is an efficient tool to perform image processing, therefore we can use it to detect human face in an image using this software.

Before going to learn about MATLAB codes to detect face in an image, let us get a brief overview of face detection in image processing.

What is Face Detection in Image Processing?

In the field of image processing, there is process that used to detect human face within an image or a video is termed as face detection or face recognition. Therefore, the main objective of face detection is to locate and identify the human faces present in a digital image.

Today, face detection has become a crucial technology in the various field such as facial recognition, emotion and expression analysis, age and gender estimation, robotics and self−driving cars, human−computer interaction, security, and more.

Now, let us discuss the steps involved in implementing MATLAB code to detect face in image processing.

How to Detect Face in Image using MATLAB?

In MATLAB, there are various techniques to detect faces in an image. The most widely used method is using MATLAB’s built-in Computer Vision Toolbox. This toolbox provides pretrained face detection models.

The step−by−step process to detect face in an image using MATLAB’s Computer Vision Toolbox is explained here.

Step (1) − Read the input image. For this, you can use the "imread" function as follows:

img = imread('Image.jpg');

Step (2) − Create a face detector object. For this you can use the "vision.CasecadeObjectDetector" function in computer vision toolbox. The syntax of this function will be as follows:

face_detector = vision.CasecadeObjectDetector();

Step (3) − Detect the faces present in the input image. For this, you can us the "detect" of the face detector object. The syntax of this code will be as follows:

bbox = step(face_detector, img);

This code will return bounding boxes "bbox" around the faces detected within the image,

Step (4) − Display the detected faces. To highlight the detected faces, you can draw the rectangles around the faces using the "insertObjectAnnotation" function as follows:

out_img = insertObjectAnnotation(img, 'rectangle', bbox, 'Face');
imshow(out_img);

This is how we can easily detect faces within an image using a built-in face detection tool in MATLAB.

Example

Now, let us take an example to practically understand how to detect faces in an image.

% Read the input image
img = imread('https://www.tutorialspoint.com/assets/profiles/586222/profile/200_1257779-1673957458.jpeg');

% Create a face detector object
face_detector = vision.CascadeObjectDetector();

% Detect the faces in image using the face detector object
bbox = step(face_detector, img);

% Highlight the detected faces
out_img = insertObjectAnnotation(img, 'rectangle', bbox, 'Face');

% Display the output image
imshow(out_img);
title('Image with Detected Faces');

Output

Code Explanation

In this MATLAB code, we start by reading the input image using the "imread" function. You can specify address of your image in place of this image. Then, we create a face detector object by using the "vision.CascadeObjectDetector()" function which is a pre−trained face detection tool in computer vision toolbox in MATLAB.

After that we use this face detector object to detect faces in the input image. Next, we highlight the detected faces by putting a rectangle around the face. Finally, we display the output image using the "imshow" function.

Conclusion

In conclusion, face detection is a process of recognizing and locating human faces present in a digital image or video. It is an important technology used in various application such as gender estimation, facial expression analysis, face locks in security systems, and more. Since MATLAB is a very efficient tool for image processing, we can use it to perform face detection in a digital image. To accomplish this task, MATLAB provides a built-in computer vision toolbox that contains various tools to perform face detection.

In this tutorial, I have explained the step−by−step process of detecting faces in a digital image using MATLAB’s built-in face detection capabilities. Also, I have added an example for better understanding of code implementation in MATLAB.

Updated on: 10-Oct-2023

82 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements