liveBook · Manning

Histogram equalization

As explained in Build Your Own Robot: Using Python, CRICKIT, and Raspberry Pi

Histogram equalization is an image-processing technique that remaps intensity values to improve the use of available contrast.

Histogram equalization prepares a grayscale image for face detection by improving its contrast. In a face-following camera workflow, convert the color frame to a single intensity channel first, then equalize that grayscale image before running the classifier. Better contrast helps improve face-detection accuracy. section 9.3.1 section 9.6

Use it as a small image-preparation step:

  • Load a camera image or saved photo into a frame. section 9.3.1
  • Convert the color frame to grayscale with cv2.cvtColor(..., cv2.COLOR_BGR2GRAY), producing one intensity value per pixel from 0 (black) to 255 (white). section 9.3.1
  • Call cv2.equalizeHist(gray) and pass the resulting clean image to detectMultiScale. section 9.3.1
  • Keep the original frame when drawing the detected face rectangle and center marker for display. section 9.3.2
def prep_face(frame):
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    return cv2.equalizeHist(gray)

Put grayscale conversion and histogram equalization in a reusable prep_face function so every frame is prepared the same way before detection. section 9.3.2

Prepare a photo before locating a face

A photo is converted from color to grayscale, equalized into clean, and checked with a Haar-cascade classifier. After a match is found, the application calculates the rectangle center, draws a blue rectangle and marker on the original image, and displays it. section 9.3.1 section 9.3.2

How Build Your Own Robot: Using Python, CRICKIT, and Raspberry Pi covers it

How deep it goes
Demonstrates the OpenCV call in a reusable preprocessing function but does not explain the equalization algorithm.
Examples
Uses a face photo, then draws a rectangle and center marker around the detected face.
What it assumes
Assumes a color image and teaches grayscale conversion before histogram equalization.
What you can do after
Apply it via OpenCV cv2.equalizeHist to prepare grayscale face images for Haar-cascade detection.
How it connects
Links grayscale conversion, pixel intensity, Haar cascade classifiers, detectMultiScale, and face detection.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build Your Own Robot: Using Python, CRICKIT, and Raspberry Pi. The section links point to the parts of the book it draws on.