Artificial Intelligence 13 min read

Face Detection with Haar Cascade and Face Recognition Using LBPH in OpenCV

This article explains the fundamentals of face detection using the Haar‑cascade algorithm, how to train and apply detectors with OpenCV, and introduces the Local Binary Patterns Histograms (LBPH) method for face recognition, covering data preparation, parameter selection, and matching techniques.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Face Detection with Haar Cascade and Face Recognition Using LBPH in OpenCV

HAAR Cascade Algorithm Basics

HAAR cascade is a machine‑learning method that trains a cascade function from many positive (face) and negative (non‑face) images; image features are extracted to distinguish faces from non‑faces.

The algorithm applies each feature to all training images, assigns equal initial weights, finds the optimal threshold that best separates faces from non‑faces, and selects features with the lowest error rate.

Using OpenCV for HAAR‑Cascade Detection

OpenCV provides training tools (opencv_haartraining, opencv_traincascade) and detectors, allowing you to train classifiers for any object. Training requires a set of samples: negative samples (images without the target) and positive samples (images containing the target), the latter created with opencv_createsamples.

Negative Samples : arbitrary images listed in a text file, each line containing a filename relative to the description file directory.

Positive Samples : generated by opencv_createsamples from images containing the object, requiring a large dataset for perspective transformations.

Negative Samples

Negative samples are taken from any images, added to a text file where each line lists the image filename; the file must be created manually and can contain images of varying sizes.

Positive Samples

Positive samples are created using the opencv_createsamples utility, either from a single image with the object or from a previous collection, and a large dataset is needed before using the utility.

Steps for HAAR Detection with OpenCV

Step‑1: Load the required XML classifiers and read the input image (or video) in grayscale.

Step‑2: After converting to grayscale, optionally resize, crop, blur, or sharpen the image, then perform segmentation to detect multiple objects.

Step‑3: Use the Haar‑like feature algorithm to locate faces, noting common characteristics such as darker eye regions and brighter nose areas.

Step‑4: Extract features using edge, line, and center detection, providing x, y, w, h coordinates to draw a rectangle around detected faces.

Face Recognition Overview

Human face recognition is simple for people but challenging for computers; successful systems extract internal (eyes, nose, mouth) or external (head, hairline) features. The brain responds to local visual features such as edges and motion.

Automatic face recognition extracts meaningful features from images, creates useful representations, and classifies them.

Early systems used geometric features (distances between eyes, ears, nose) forming feature vectors; recognition compares Euclidean distances between probe and reference vectors, which works under varying illumination but suffers from registration difficulties.

Face recognition operates in two modes: verification (1‑to‑1 comparison) and identification (1‑to‑N comparison).

Various algorithms exist, including Eigenfaces, LBPH, Fisherfaces, SIFT, and SURF. This article focuses on the Local Binary Patterns Histograms (LBPH) algorithm.

Introduction to LBPH

LBPH encodes each pixel’s neighborhood into a binary pattern, summarizing local image structure; it was first defined in 1994 as LBP and later adapted for texture classification.

The algorithm extracts local features rather than treating the whole image as a high‑dimensional vector.

LBPH Parameters

LBPH accepts four parameters:

Radius: distance from the central pixel, typically set to 1.

Neighbors: number of sampling points on the circle.

Grid X: number of cells horizontally; more cells increase feature vector dimensionality.

Grid Y: number of cells vertically; more cells increase feature vector dimensionality.

Training the Algorithm : Provide a dataset of facial images with unique IDs; the algorithm learns to associate IDs with image features.

LBP Operation : Compute the LBP image by comparing each pixel to its neighbors, generating a binary code for each neighborhood.

Histogram Extraction : Divide the LBP image into grids (Grid X × Grid Y), compute a 256‑bin histogram for each grid, and concatenate them into a single feature vector.

Face Recognition : For a new image, compute its LBPH histogram and compare it to stored histograms using distance measures (e.g., Euclidean distance). The closest match returns the corresponding ID and a confidence score.

In summary, Haar‑cascade is used for face detection, while LBPH provides a simple yet effective method for face recognition by focusing on local image features.

长按或扫描下方二维码,免费获取 Python公开课和大佬打包整理的几百G的学习资料,内容包含但不限于Python电子书、教程、项目接单、源码等等。

点击 阅读原文 了解更多。

computer visionPythonopencvface detectionHaar cascadeLBPH
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.