MATLAB Code For Face Recognition Based on Histogram of Oriented Gradients (HOG)

by admin in , , on July 22, 2020

1. Introduction:

Face recognition is the process of identifying one or more people in images or videos by analyzing and comparing patterns. Algorithms for face recognition typically extract facial features and compare them to a database to find the best match. Face recognition is an important part of many biometric, security, and surveillance systems, as well as image and video indexing systems. Face recognition leverages computer vision to extract discriminative information from facial images, and pattern recognition or machine learning techniques to model the appearance of faces and to classify them.

In this project, we introduce an application of the HOG feature method for face recognition. The implementation of this method has been done using the MATLAB environment. The results showed a very good ability of HOG method to recognize faces with an accuracy of about 90%. This method has the advantage of recognizing the faces basing on the different facial gestures. The written code has been attached in appendix 1 at the end of this project.

2. Face Recognition Methods:

We can use computer vision techniques to perform feature extraction to encode the discriminative information required for face recognition as a compact feature vector using techniques and algorithms such as:

  • Dense local feature extraction with SURF, BRISK, or FREAK descriptors.
  • Histogram of oriented gradients.
  • Distance between detected facial landmarks such as eyes, noses, and lips.
  • Machine learning techniques that can be applied to the extracted features to perform face recognition or classification using:
    • Supervised learning techniques such as support vector machines (SVM) and decision trees.
    • Ensemble learning methods.
    • Deep neural networks.

In general, these methods divided into four categories, and the face detection algorithms could belong to two or more groups. These categories are as follows: 1. Feature-based methods, 2. Appearance-based methods, 3. Knowledge-based methods, and 4. Template matching methods.

The knowledge-based method depends on the set of rules, and it is based on human knowledge to detect the faces. Ex- A face must have a nose, eyes, and mouth within certain distances and positions with each other. The big problem with these methods is the difficulty in building an appropriate set of rules. There could be many false positives if the rules were too general or too detailed. This approach alone is insufficient and unable to find many faces in multiple images.

The feature-based method is to locate faces by extracting structural features of the face. It is first trained as a classifier and then used to differentiate between facial and non-facial regions. The idea is to overcome the limits of our instinctive knowledge of faces. This approach divided into several steps and even photos with many faces they report a success rate of 94%.

Template Matching method uses pre-defined or parameterized face templates to locate or detect the faces by the correlation between the templates and input images. Ex- a human face can be divided into eyes, face contour, nose, and mouth. Also, a face model can be built by edges just by using edge detection methods. This approach is simple to implement, but it is inadequate for face detection. However, deformable templates have been proposed to deal with these problems.

The appearance-based method depends on a set of delegate training face images to find out face models. The appearance-based approach is better than other ways of performance. In general appearance-based methods rely on techniques from statistical analysis and machine learning to find the relevant characteristics of face images. This method also used in feature extraction for face recognition.

3. The Chosen Method – HOG:

The method used in this project is the Histogram of Oriented Gradients (HOG) feature descriptor. HOG is a feature descriptor used in computer vision and image processing for the purpose of object detection. The technique counts occurrences of gradient orientation in localized portions of an image. A feature descriptor is the representation of an image or an image patch that simplifies the image by extracting useful information and throwing away extraneous information. Typically, a feature descriptor converts an image of size “width x height x 3” (channels) to a feature vector/array of length n. In the case of the HOG feature descriptor, for example, the input image is of size 112 x 92 x 3, and the output feature vector is of length 4680.

The feature vector is not useful for the purpose of viewing the image, but it is very useful for tasks like image recognition and object detection. The feature vector produced by these algorithms when fed into an image classification algorithm produces good results.

In the HOG feature descriptor, the distribution (histograms) of directions of gradients (oriented gradients) are used as features. Gradients (x and y derivatives) of an image are useful because the magnitude of gradients is large around edges and corners (regions of abrupt intensity changes) and we know that edges and corners pack in a lot more information about object shape than flat regions.

Robert K. McConnell of Wayland Research Inc. first described the concepts behind HOG without using the term HOG in a patent application in 1986. In 1994 the concepts were used by Mitsubishi Electric Research Laboratories. However, usage only became widespread in 2005 when Navneet Dalal and Bill Triggs, researchers for the French National Institute for Research in Computer Science and Automation (INRIA), presented their supplementary work on HOG descriptors at the Conference on Computer Vision and Pattern Recognition (CVPR). In this work, they focused on pedestrian detection in static images, although since then they expanded their tests to include human detection in videos, as well as to a variety of common animals and vehicles in static imagery.

4. HOG Algorithm:

In this section, we will go into the details of calculating the HOG feature descriptor. To illustrate each step, we will use a patch of an image. In the original paper [2], the process was implemented for human detection, and the detection chain was the following in figure 1.

Figure 1. The process was implemented for human detection

Step 1. Preprocessing

First of all, the input images must but of the same size (crop and rescale images). The patches we’ll apply require an aspect ratio of 1.2174, so the dimensions of the input images in our library are 112 x 92.

The used library is downloaded from the Internet from the Face Recognition Homepage site [5]. The library consists of 40 groups of pictures each group has 10 pictures of one person in different facial gestures. The pictures are given with a suffix *.PGM, this type of picture is a light and grayscale image.


Step 2. Compute the Gradient Images

The second step is to compute the horizontal and vertical gradients of the image, by applying the following kernels:

The gradient of an image typically removes non-essential information. We calculate gradient in both directions: X (vertical change) and Y (horizontal). So, X gradient detect any horizontal sharp color change whereas Y gradient detect any sharp vertical color change in the image. Hence gradient calculation detects any sharp change in the image remove any constant color background. Apart from this for each pixel, we also calculate magnitude and angle of gradient. For color images, separate gradients for each color channel is calculated and channel with the largest norm is taken as gradient vector.

Figure 2. X and Y directional gradient of a single image in the project database

From figure 2, the second image is showing a vertical gradient (x-gradient), which is sensitive to vertical change. Likewise, the third image is showing a horizontal gradient(y-gradient) which is sensitive to vertical changes.

Step 3. Compute the Histogram

This step created a histogram over the binning of magnitude and direction. These bins can be spaced over a 0-180 degree (unsigned gradient) or 0-360 degrees (signed gradient). Dalal and Triggs found that a bin of 9 (0,20, 40, 60…160) with an unsigned gradient is giving the best result.

Now we create a cell block. All the pixels in the cell block has magnitude and direction of gradient, we select the bin bucket using the gradient direction and put the gradient magnitude value int he selected bin. We repeat this process for all the pixel present in the cell. Thus, magnitude value gets accumulated in these 9 bins. Advantage of using bins is algorithm remain resistant to small change in pose.

Figure 3. Example of 9 bins histogram

Step 4. Block Normalization

A 16×16 block can be applied in order to normalize the image and make it invariant to (not affected by) lighting variations. This is simply achieved by dividing each value of the HOG of size 8×8 by the L2 – norm of the HOG of the 16×16 block that contains it, which is in fact a simple vector of length 9*4 = 36.

Let’s see how a vector of length 3 is normalized. Let’s say we have an RGB color vector [128, 64, 32]. The length of this vector is. This is also called the L2 norm of the vector. Dividing each element of this vector by 146.64 gives us a normalized vector [0.87, 0.43, 0.22]. Now consider another vector in which the elements are twice the value of the first vector 2 x [128, 64, 32] = [256, 128, 64]. We can see that normalizing [256, 128, 64] will result in [0.87, 0.43, 0.22], which is the same as the normalized version of the original RGB vector. We can see that normalizing a vector removes the scale.

Now that we know how to normalize a vector, we may be tempted to think that while calculating HOG you can simply normalize the 9×1 histogram the same way we normalized the 3×1 vector above. It is not a bad idea, but a better idea is to normalize over a bigger sized block of 16×16. A 16×16 block has 4 histograms that can be concatenated to form a 36 x 1 element vector and it can be normalized just the way a 3×1 vector is normalized. The window is then moved by 8 pixels and a normalized 36×1 vector is calculated over this window and the process is repeated.

Step 5. Flattening into a Feature Vector

Finally, all the 36×1 vectors are concatenated into a large vector. And we are done! We have our feature vector, on which we can train a soft SVM classifier.

This is the final step where all normalized 36×1 vectors are flattened into a single vector. This final vector is our feature detector.

Figure 4. HOG Descriptor

SVM Classifier

Support-Vector Machines (SVMs) are supervised learning models with associated learning algorithms that analyze data used for classification and regression analysis. Given a set of training examples, each marked as belonging to one or the other of two categories, an SVM training algorithm builds a model that assigns new examples to one category or the other, making it a non-probabilistic binary linear classifier (although methods such as Platt scaling exist to use SVM in a probabilistic classification setting).

An SVM model is a representation of the examples as points in space, mapped so that the examples of the separate categories are divided by a clear gap that is as wide as possible. New examples are then mapped into that same space and predicted to belong to a category based on the side of the gap on which they fall.

6. Application and Results:

In this section, we will build our MATLAB code to detect faces stored in our database of 40 faces each with 10 situations.

The first step in our code is to clear the workspace, command window, and to close all the opened pictures. Then we need to load the images information from the face database directory. This done by using the following part of code:



close all

%% Load Images Information from FaceDatabase Face Database Directory

faceDatabase = imageSet(‘FaceDatabase’,’recursive’);


The next step is to check if the pictures are loaded, and this done by testing and presenting all ten positions of one of the 40 faces we loaded, we selected the first face positions to test. See figure 5.

%% Display Various Sets of First Face

figure(1); montage(faceDatabase(1).ImageLocation);

title(‘Images of the First Face’);

Figure 5. Images of the First Face

          In the following step, we will show the whole faces and the test face beside them. The following code do this task, see figure 6.

%%  Display Test Image and Database Side-Side

testperson = 1;

galleryImage = read(faceDatabase(testperson),1);


for i=1:size(faceDatabase,2)

imageList(i) = faceDatabase(i).ImageLocation(5);


subplot(1,2,1);imshow(galleryImage); title(‘Test Image’)

subplot(1,2,2);montage(imageList); title(‘Database’)

diff = zeros(1,9);

Figure 6. Displaying test image and the database

          Now we need to prepare two groups of images, the first one is a training group and the second is a testing group. This is done by using the built-in function in MATLAB “partition”.

%% Split Database into Training & Test Sets

[training,test] = partition(faceDatabase,[0.8 0.2]);

The next step is to extract the HOG feature from the tested image. Built-in function in MATLAB named “extractHOGFeatures” does this mission totally saving our time of computing the gradients, histogram, and normalization. See figure 7.

%% Extract and display Histogram of Oriented Gradient Features for single face

person = 1;

[hogFeature, visualization]= extractHOGFeatures(read(training(person),1));


subplot(1,2,1);imshow(read(training(person),1));title(‘Input Face’);

subplot(1,2,2);plot(visualization);title(‘HoG Feature’);


Figure 7. Extracting the HOG feature

In the following step we intended to extract all the features from the training images to build our feature database so we can compare with it the extracted features of any test image.

%% Extract HOG Features for training set

trainingFeatures = zeros(size(training,2)*training(1).Count,4680);

featureCount = 1;

for i=1:size(training,2)

for j = 1:training(i).Count

trainingFeatures(featureCount,:) = extractHOGFeatures(read(training(i),j));

trainingLabel{featureCount} = training(i).Description;

featureCount = featureCount + 1;


personIndex{i} = training(i).Description;


On the extracted features of the training database then applied the Support-vector machine SVM classifier using the built-in function “fitcecoc”.

%% Create 40 class classifier using fitcecoc

faceClassifier = fitcecoc(trainingFeatures,trainingLabel);

The final step is to pic an image from the test group and test if and see if the algorithm can find this tested image in the database based on matching extracted features.  See figure 8.

%% Test Images from Test Set

person = 4;

queryImage = read(test(person),1);

queryFeatures = extractHOGFeatures(queryImage);

personLabel = predict(faceClassifier,queryFeatures);

% Map back to training set to find identity

booleanIndex = strcmp(personLabel, personIndex);

integerIndex = find(booleanIndex);

subplot(1,2,1);imshow(queryImage);title(‘Query Face’);

subplot(1,2,2);imshow(read(training(integerIndex),1));title(‘Matched Class’);

Figure 8. Test image and Matched one

Now let’s do mass testing of 5 faces in pairs (two situations for each face) and present the results here.

7. Conclusion:

Face recognition is still a challenging problem in the field of computer vision. It has received a great deal of attention over the past years because of its several applications in various domains. HOG feature is very useful algorithm for the tasks of image recognition and object detection. In this project we presented the various method used in the field of face recognition. We focused and selected HOG feature descriptor method to be our subject.

This method did not need a huge computation time where it is about five steps algorithm. The HOG method was implemented in MATLAB and applied on a database of 40 faces each of 10 positions. The method gave a good possibility of recognizing faces. The accuracy of this method we can say that is about 90% as it missed one face in the last test of N3 figure 11.


  1. Kresimir Delac and Mislav Grgic. Face Recognition. 2007 I-Tech Education and Publishing. The University of Zagreb. Faculty of Electrical Engineering and Computing. Department of Wireless Communications
  2. Navneet Dalal and Bill Triggs. “Histograms of Oriented Gradients for Human Detection”. INRIA Rhone-Alps, 655 avenue de l’Europe, Montbonnot 38334, France. Url:
  3. G. Lowe. Distinctive image features from scale-invariant keypoints. IJCV, 60(2): 91–110, 2004. Url:
  4. Navneet Dalal, Bill Triggs. Object Detection using Histograms of Oriented Gradients. Url:
  5. Face Recognition Homepage Site. Url: Access date 25.03.2020.
  6. Navneet Dalal, Bill Triggs, and Cordelia Schmid. Human Detection Using Oriented Histograms of Flow and Appearance. Url:
  7. Qiang Zhu, Shai Avidan, Fast Human Detection Using a Cascade of Histograms of Oriented Gradients. Url:
  8. Leo (2020). Histograms of Oriented Gradients (, MATLAB Central File Exchange. Retrieved March 24, 2020.
  9. Sanyam Garg (2020). Histogram of Oriented Gradients (HOG) code using Matlab (, MATLAB Central File Exchange. Retrieved March 24, 2020.
  10. Dalal, N. and B. Triggs. “Histograms of Oriented Gradients for Human Detection”, IEEE Computer Society Conference on Computer Vision and Pattern Recognition, Vol. 1 (June 2005), pp. 886–893.

You might be interested in:


2 Sales

Share Now!

Release Information

  • Price


  • Released

    July 22, 2020

  • Last Updated

    July 22, 2020

  • File Included

    All the files shown in the video with database of 400 picture and report of 25 pages written in MS Word.

  • File Size

    4.6 Mb

  • Compatible With

    Matlab R19

Share Your Valuable Opinions