MediumHaar Cascade Model-Behind the curtain | by Raji Lini | Medium
Sitemap

Haar Cascade Model-Behind the curtain

4 min readDec 4, 2021

--

Haar cascade is an object detection algorithm mainly used for identifying faces in an image or from real-time video. It is a machine learning classifier algorithm. Viola and Jones in the year 2001 described in their paper ‘Rapid object detection using a boosted cascade of simple features’ about Haar features using which the face detection is performed in a powerful way. Despite of all deep learning techniques, Haar cascade model is still being used for face detection.

The algorithm follows 4 steps.

  1. Haar feature selection:-select the relevant Haars for the obect detection
  2. Integral image formation:-Create the integral image for easy computations
  3. Adaboost training:- For the feature selection and optimization
  4. Cascading of classifiers:- Helps to reject unwanted subsections in image

Haar Features

Haar features are features that help determining the likelihood of a certain portion in an image being part of the object searched. The idea of haar features comes from the Haar wavelets in signal processing and filters applied over it. Haar features are similar to kernels in convolution operation, but represented as black and white rectangles. The white area includes value 0 and black area includes value 1. Haars can be of 2-rectangle, 3-rectangle or 4-rectangle. We need to decide which haar feature is suitable for our object detection process. In face detection, we mainly use edge features(2-rectangle) and line features(3-rectangle). It helps detecting facial features like eyes, nose, mouth etc. The representation of some of the haar features are shown below.

Haar Features used in face detection

Each haar feature, when operated on image, outputs in a single number. The working behind haar features can be summarized as the difference of the sum of pixels under black region and sum of pixels under white region. According to the model, if the Haar feature calculation results in a value greater than the threshold, the particular portion in the image has the possibility of detecting the object.

Get Raji Lini’s stories in your inbox

Join Medium for free to get updates from this writer.

The viola-jones model uses a 24X24 sliding sub-window over the image and calculates the selected haar feature values from the pixels under the sub-window region. The process is continued over the image by sliding the sub-window. This process includes huge computations and results in millions of features for classification. In order to reduce the computations, the model put forwards the idea of creating an integral image. In order to reduce and optimize the feature values generated, the model applies the Adaboost algorithm.

Integral image

An integral image can be formed from the original image by replacing each pixel by the sum of all the pixels above and left of it. The diagram below shows the integral image formed from the original image of “Lena”.

“Lena” Integral image

Integral images speed up the calculation of Haar features. With the help of integral image, Harr features can be computed at any scale or location in constant time.

Instead of computing haar feature at every image pixel, the model create integral images of every 24x24 sub-window and computes the Haar features. From a 24x24 subwindow, more than 1,60,000 features are generated by applying multiple selected haars. Some features from this set are irrelevant in object detection and others are weak classifiers. The Haar cascade model use Adaboost as the optimization algorithm to select best features out of the millions of features generated.

Adaboost

The machine learning algorithm “Adaboost” is a boosting algorithm which combines multiple weak classifiers into a single strong classifier.

In Haar cascade model, considering a base window of 24x24 size, will lead to more than 1,60,000 features. Adaboost algorithm discards the irrelevant features and collects all the relevant features. The relevant feature set may contain many weak classifiers. Instead of discarding the weak classifiers, Adaboost constructs a strong classifier as a linear combination of these weak classifiers. Thus the model will be trained intensely and the output becomes very robust.

Cascading of Haars

The cascading of Haars is a method for applying boosting algorithm only onto promising regions of the image which increases the speed of the object detection from the image. Classifier on each sub window decides whether any part of object was found in the area(positive) or the considered part contains nothing relevant to object detection(negative). It includes methods to reject negative samples and retain the positive ones.

Conclusion

Hope you are familiar with the 4 stages of the Haar cascade algorithm. In the next article, we can see the Python implementation of Haar cascade algorithm for face detection.

--

--

Raji Lini
Raji Lini

Written by Raji Lini

By profession an Instructor and now a Research scholar at IIITM-K