Monday, May 27, 2024

Real-Time Face Detection Using MATLAB

Object detection and tracking are important in many computer vision applications, including activity recognition, automotive safety and surveillance. Presented here is an face detection using MATLAB system that can detect not only a human face but also eyes and upper body.

Face detection is an easy and simple task for humans, but not so for computers. It has been regarded as the most complex and challenging problem in the field of computer vision due to large intra-class variations caused by the changes in facial appearance, lighting and expression. Such variations result in the face distribution to be highly nonlinear and complex in any space that is linear to the original image space.

Face detection is the process of identifying one or more human faces in images or videos. It plays an important part in many biometric, security and surveillance systems, as well as image and video indexing systems.

This face detection using MATLAB program can be used to detect a face, eyes and upper body on pressing the corresponding buttons. The program output screen is shown in Fig. 1.

Fig. 1: Program output screen
Fig. 1: Program output screen

Software program

This real-time face detection program is developed using MATLAB version R2012a. A graphic user interface (GUI) allows users to perform tasks interactively through controls like switches and sliders. You can easily create a GUI and run it in MATLAB or as a stand-alone application. The initial program output of this project is shown in Fig. 1.

- Advertisement -

Viola-Jones algorithm. There are different types of algorithms used in face detection. Here, we have used Viola-Jones algorithm for face detection using MATLAB program. This algorithm works in following steps:
1. Creates a detector object using Viola-Jones algorithm
2. Takes the image from the video
3. Detects features
4. Annotates the detected features

MATLAB functions

The program (testing.m) has many functions. Do not edit the functions as these are linkers and non-executable codes. First, you have to find the format supported by the camera and its device ID using the command given below (also shown in Fig. 2):

Fig. 2: Checking the device ID
Fig. 2: Checking the device ID


- Advertisement -

After finding the device ID, you can change the device ID number in your source code.

The device ID here is {1}, so we have written ‘1’ in the code, as mentioned below:

vid = videoinput(‘winvideo’,1,’YUY2_

We also have other formats in MATLAB. You can check which format your camera supports by using the commands below (also shown in Fig. 3):

Fig. 3: Formats supported by the camera
Fig. 3: Formats supported by the camera

info.DeviceInfo (1)

In Fig. 3, you can see that format ‘YUY2_160x120’ is the one supported by the camera by default. But, there are other formats (resolutions) that your camera can support, as shown in the last line of this screenshot. If you select a different format and device number, you should make changes in the source code accordingly. To detect a face or a particular feature on the faces of people, use the following steps in MATLAB program (testing.m):

1. Define and set-up your cascade object detector using the constructor:


It creates a system object detector that detects objects using Viola-Jones algorithm. Its classification model property controls the type of object to detect. By default, the detector is configured to detect faces.

Face detection using matlab
Fig. 4: Face detection
Fig. 5: Eyes detection
Fig. 5: Eyes detection
Fig. 6: Upper body detection
Fig. 6: Upper body detection

Download Source Code for the face detection using MATLAB project: click here

2. Call the step method with input image I, cascade object detector, points PTS and any other optional properties.

Below is the syntax for using the step method. Use the step syntax with input image I, selected cascade object detector and other optional properties to perform detection.

BBOX = step (detector, I)

It returns BBOX, an M-by-4 matrix defining M-bounding boxes, containing detected objects. This method performs multi-scale object detection on input image I. Each row of output matrix BBOX contains a four-element vector (x, y, width and height) that specifies in pixels, the upper-left corner and size of a bounding box. Input image I must be a gray scale or true colour (RGB) image.

3. The third step is:


It inserts rectangles and corresponding labels at the location indicated by the position matrix. The position input must be an M-by-4 matrix, where each row (M) specifies a rectangle as a four-element vector (x, y, width and height). Elements x and y indicate the upper-left corner of the rectangle, and the width and height specify the size.

Face detection using MATLAB system testing

To test this program, follow the steps given below:

  1. Install MATLAB version R2012a or higher version in your system. Launch it from desktop shortcut. You will see a blank command window.
  2. Download the source folder.
  3. Check the device ID, as shown in Fig. 2, and write the device ID number in the source code.
  4. Run the program. A GUI will appear, as shown in Fig. 1.
  5. Click on Start button to initialise camera settings.
  6. Next, click on Face button and the camera will detect the face. Your face will be detected and displayed on the right side of the screen (Fig. 4). Similarly, you can also detect your eyes and upper body by clicking on the respective buttons. The output screenshots for the detected eyes and upper body are shown in Fig. 5 and Fig. 6, respectively. Remember to click Stop button first to stop the previous process in order to detect other two features.
  7. To stop, click Stop button.

The author is a B.Tech in electronics and communication from SRCEM, Gwalior.

More MATLAB projects available here.

The article was first published on 20 April 2016 and was updated on 30 May 2019.


  1. Hi EFY. I had tried this project on my laptop. It worked well, but when i try to change from face to eyes when pressing stop button i am getting some errors.
    after pressing face button,face gets detected. then i pressed stop. then the following errors occured.
    Error using imaqdevice/trigger (line 48)
    OBJ must be running before TRIGGER is used.

    Error in testing>face_Callback (line 102)

    Error in gui_mainfcn (line 95)

    Error in testing (line 42)
    gui_mainfcn(gui_State, varargin{:});

    Error in>@(hObject,eventdata)testing(‘face_Callback’,hObject,eventdata,guidata(hObject))

    Error while evaluating UIControl Callback

  2. Hello guys how can i do the, using my inbuilt laptop camera..

    First, you have to find the format supported by the camera and its device ID using the command given below (also shown in Fig. 2):
    info = imaqhwinfo(‘winvideo’)

  3. Hello, I tried the code but i’m getting the following error:

    >> testing(varargin)
    Attempt to execute SCRIPT varargin as a function:
    C:\Program Files\MATLAB\MATLAB ProductionServer\R2015a\toolbox\matlab\lang\varargin.m

  4. hi I think you probably have issues with your link to the file, when download is clicked the total download size is only 7 kilobytes of corrupted text, i’v also noted that the majority of your communication response is ”The source code is present at the end of the article.” everyone visiting your sight is actually using valuable time trying to get somewhere in their life believe that this could be the right place to be, after a some time people get fed-up with how they get treated on a specific sight and probably find alternative ways but after numerous attempts by visiting your sight neither side besoms a winner and efforts from Bothe sides becomes a waist of time but more so from your prospective as you’ve actually taken time and effort to create this page. I hope you can actually convert this in to a wine for both sides if you understand what is that I’m ting to explain here

  5. Sir,
    I sent many letters regarding for many projects, but you have not replied so kindly send information for the MATLAB face detection and it may not be useful if you are going to publish the projects and moreover
    a lay man like myself could not understand the above projects. So I may request you to send one complete CD and if possible one pen drive with four or five projects and you can charge for the same and kindly write down your opinion without fail.

    With regards,

    (Srinivasa rao kandoti)


Unique DIY Projects

Electronics News

Truly Innovative Tech

MOst Popular Videos

Electronics Components