MATLAB Application in Face Recognition: Code, Description & Syntax

Every time you unlock your mobile phone with your face or pass through your high-tech office surveillance system, an elaborate facial recognition technology is operating in the background. So what is face recognition, and how can you carry out face recognition using MATLAB

Facial recognition is the process of identifying human faces through technology. The facial recognition system uses computer vision and Machine Learning techniques to model and classifies facial features extracted from images and videos. Algorithms for face identification extract and map facial features and compare them to a database of known faces to find the best match. 


MATLAB in Face Recognition

It is possible to achieve face recognition using MATLAB code. The built-in class and function in MATLAB can be used to detect the face, eyes, nose, and mouth. The object vision.CascadeObjectDetector System of the computer vision system toolbox recognizes objects based on the Viola-Jones face detection algorithm. 

Description of the MATLAB Object Detector

The vision.CascadeObjectDetector makes use of the Viola-Jones algorithm for the identification of faces, eyes, mouth, nose, or the upper body. A custom classifier can be trained by using MATLAB’s Image Labeler and used along with the System object. So how are facial features or the upper body detected in an image? Here are the steps:

  1. The first step involves the creation of the vision.CascadeObjectDetector object and setting its properties.
  2. In this step, the object is invoked with arguments (as if it were behaving like a function).

Read: 15 Interesting MATLAB Project Ideas & Topics For Beginners

Syntax for the Creation of the Object Detector

The syntax used for the creation of the Object Detector is as follows:

  • detector = vision.CascadeObjectDetector
  • detector = vision.CascadeObjectDetector(mode1)
  • Detector = vision.CascadeObjectDetector(Name,Value)
  • detector = vision.CascadeObjectDetector(XMLFILE)

Description of the Syntax

  • detector = vision.CascadeObjectDetector: This syntax is used for the creation of a detector that detects objects using the Viola-Jones algorithm.
  • detector = vision.CascadeObjectDetector(mode1): This syntax is used for the creation of a detector that is configured for detecting objects defined by the input vector – mode1.
  • detector = vision.CascadeObjectDetector(Name,Value): This syntax is used for setting properties by using one or more than one name-value pairs, where each property name is enclosed within quotes. For example: detector = vision.CascadeObjectDetector(‘ClassificationModel’,’UpperBody’)

Check out: Machine Learning Project Ideas


Unless otherwise specified, the features of the cascade Object Detector are non-tunable, meaning that after calling the object, their values cannot be altered. Objects get locked when they are invoked, and their unlocking is done by the ‘release’ function.

On the other hand, a tunable property would mean that its value can be changed at any time. So, let us have a look at some of the features before we understand face recognition using MATLAB codes.

  • ClassificationModel: Defined as a character vector, this property is responsible for controlling the type of object to detect. The default configuration of the detector detects faces.
  • MinSize: The size of the smallest recognizable object is denoted as a two-element vector [height width]. Unless a property value is specified, the detector sets it to the image size used for training the classification model.
  • MaxSize: The size of the smallest recognizable object is denoted as a two-element vector [height width]. Unless a property value is specified, the detector sets it to size (I). 
  • ScaleFactor: It has a specified value higher than 1.0001. This property is for incremental scaling of the detection resolution between MinSize and MaxSize.
  • MergeThreshold: It has a specified integer value equal to 4. In case there are multiple detections around a target object, the threshold defines the final detection criteria.
  • UseROI: Specified as false, this property can be set to true for the detection of objects within a rectangular region of interest in the input image.

FYI: Free nlp course!

Syntax for Using the Object Detector

  • bbox = detector(I)
  • bbox = detector(I,roi)

Description of the Syntax

  • bbox = detector(I) returns bbox, an M-by-4 matrix, that defines ‘M’ bounding boxes that contain the detected objects.
  • bbox = detector(I,roi) is used for detecting objects within the rectangular region of interest, specified by roi. 

Input Arguments

  • I — Input image: It is specified as true color or grayscale (RGB).
  • model — Classification model: It is specified as a character vector and describes the object type to be detected. 
  • XMLFILE — Custom classification model: Specified as an XML file, it can be created using OpenCV training functionality or the trainCascadeObjectDetector function.
  • roi — Rectangular region of interest: A four-element vector [x y width height] is used to specify this input argument.

Output Arguments

bbox — Detections: Detections are returned as an M-by-4 element matrix, each row of which contains the four-element vector [x y width height].

Object Functions Common to All System Objects

  • step: For running System Object algorithm
  • release: For releasing system resources 
  • reset: For resetting the internal states of System Object.

MATLAB code for face recognition

In this section, we will see an example of face recognition using MATLAB code.

Face Detection



The step(Detector,I) will return Bounding Box value containing [x,y,Height,Width] of the objects under detection:


Nose Detection




  • The passing of the argument ‘Nose’ denotes that the object of interest is the nose.
  • The default nose detection syntax is vision.CascadeObjectDetector(‘Nose’)
  • The default parameter values passed to vision.CascadeObjectDetector can be modified based on the input image.
  • The ‘MergeThreshold’ value can be overridden to avoid multiple detections around the target object (as in the image above). 

Eye Detection



Mouth Detection



Also Read: While loop in MATLAB: Everything You Need to Know

Wrapping Up

While face recognition technology is beneficial for verification of personal identity, it does raise privacy issues. Since the technology uses an individual’s faceprint, it is often regarded as a breach of one’s privacy, safety, and security. Face recognition using MATLAB can be employed in several cases where security is of utmost concern. From airports and offices to smartphones, facial recognition has become an integral component of many systems and organizations.

If you’re interested to learn more about machine learning, check out IIIT-B & upGrad’s PG Diploma in Machine Learning & AI which is designed for working professionals and offers 450+ hours of rigorous training, 30+ case studies & assignments, IIIT-B Alumni status, 5+ practical hands-on capstone projects & job assistance with top firms.

What is face recognition?

Face recognition can be used to protect against identity theft, as well as to identify individuals by name. However, given that face recognition is relatively new and is continuously being developed, it's important to understand the basics of face recognition in order to use it effectively. Face recognition refers to the process of determining whether a face image belongs to a known person. The problem of face recognition can be divided into two tasks: 1) Face Detection – locating the face in the image, 2) Face Recognition – identification of the person whose face it is.

What is Matlab?

Matlab is a programming language for numerical calculation. Basically, it is a matrix programming language. It is used a lot in scientific and engineering calculations. Unlike the other programming languages, MATLAB was designed to be a matrix language, one that is suitable for computation on matrices. Matrices are used in a lot of different equations, especially in scientific and engineering calculations. Matlab is a high-level programming language which comes with a large number of functions. It is used to solve mathematical problems, analyze data and create graphs.

What is Viola Jones in Matlab?

The Viola Jones algorithm is used for face detection and facial expression recognition. Viola Jones algorithm is based on Histograms of Oriented Gradients (HOG) that was first introduced by Paul Viola and Michael Jones in 2001. It is used in computer vision, machine learning, and image processing. Viola Jones algorithm provides a complete object detection system and it may be used in pedestrian detection, object detection, or human detection. Viola Jones algorithm consists of a feature extraction step, a clustering step, and an object classification step.

Want to share this article?

Prepare for a Career of the Future

Enroll Today

Leave a comment

Your email address will not be published. Required fields are marked *

Our Popular Machine Learning Course

Get Free Consultation

Leave a comment

Your email address will not be published. Required fields are marked *

Get Free career counselling from upGrad experts!
Book a session with an industry professional today!
No Thanks
Let's do it
Get Free career counselling from upGrad experts!
Book a Session with an industry professional today!
Let's do it
No Thanks