Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Haar-Like Features

Give Feedback

All human faces share some similarities. If you look at a photograph showing a person’s face, you will see, for example, that the eye region is darker than the bridge of the nose. The cheeks are also brighter than the eye region. We can use these properties to help us understand if an image contains a human face.

A simple way to find out which region is lighter or darker is to sum up the pixel values of both regions and comparing them. The sum of pixel values in the darker region will be smaller than the sum of pixels in the lighter region. This can be accomplished using Haar-like features.

A Haar-like feature is represented by taking a rectangular part of an image and dividing that rectangle into multiple parts. They are often visualized as black and white adjacent rectangles.

00:00 All human faces share some common similarities. If you look at a photograph showing a person’s face, you will see, for example, that the eye region is darker than the bridge of the nose.

00:14 The cheeks are also brighter than the eye region. We can use these common properties to help us determine if an image contains facial features—and ultimately, a face.

00:25 A simple way to determine how bright or dark a portion of an image is is to first convert it to grayscale, and then add up the values of all the pixels within that portion.

00:38 Remember: lower values represent a darker pixel, while higher ones represent a brighter pixel. So if a specific subregion’s pixels add up to a low number, it’s a dark subregion. If it’s a high number, it’s bright.

00:56 We can determine some features of the images, such as lines and edges, by comparing these pixel readings to readings in an adjacent area. To do this, we use what are called Haar-like features.

01:10 These are ideal clusters of pixels that could represent a specific feature in the image, such as an edge. For example, take a look at these Haar-like features.

01:22 The first two are used to detect edges within a picture, the third one here detects vertical lines, and the fourth one detects horizontal features. If our images were pure black and white, then these Haar-like features would be able to identify where lines and edges are perfectly. But like I said, these features are ideal.

01:45 Our pictures will never be all black and white. That would be too easy. Instead, they’re usually varying shades of gray.

01:54 Take a look at this example using Haar-like features for finding edges.

02:00 The feature on the left represents an edge. That’s because there is a clear contrast between the dark and bright portions of the feature.

02:10 The feature on the right, however, is more realistic. Here, the contrast between the two sides of our potential edge is still pretty distinct, but not as distinct as on the left.

02:24 This contrast is called the feature’s value, and it’s what lets us determine if what the feature represents—like an edge, a line, or a facial feature—exists at this location in the image. To calculate this value, we take the average of the white pixels and subtract from them the average of the black pixels.

02:46 If we get a result that is close to 255, then we’ve got a strong contrast, and this feature is likely to represent the feature we are looking for. It’s telling us that whatever it represents—like an edge or a line—it most likely exists within this region.

03:05 If we get a result closer to 0, then the clusters of pixels we are comparing are very close to one another and there likely isn’t any facial feature of interest here.

03:16 Something like a wall would be likely to have little contrast, as it’s all one shade of brightness.

03:24 This example shows how Haar-like features can be applied to both the eye region and the bridge of the nose. It picks up the darkness around the eyes and the bright bridge of the nose.

03:36 Because the face has so many distinct areas in terms of brightness, Haar-like features work great here. The only problem is this pixel summing has to be calculated for many different subregions of the image at once, and that becomes computationally expensive—aka slow.

03:56 If only there was a way to speed this up.

DrJay on April 3, 2020

I don’t know what is installing pakages please sir, can you explain.

Become a Member to join the conversation.