liveBook · Manning

Grayscale image

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

Grayscale is an image representation in which each pixel records a single intensity value rather than separate color components.

A grayscale image stores one intensity value for each pixel instead of three color components. In this workflow, intensity runs from 0 for black through gray values to 255 for white. A 640 by 480 color image therefore has shape (480, 640, 3), while its grayscale version has shape (480, 640). section 9.3.1

Convert a camera image to grayscale before face detection so the classifier can compare pixel intensities across image regions. With OpenCV, call cv2.cvtColor on the loaded frame using cv2.COLOR_BGR2GRAY. section 9.3.1

>>> gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

This conversion produces a single-channel image named gray from a BGR camera frame. section 9.3.1

Grayscale conversion is preparation rather than the final detection step. Histogram equalization can then improve contrast, producing a cleaned image for detectMultiScale. Packaging both operations in a prep_face function makes the same preparation reusable whenever a frame is checked for a face. section 9.3.1 section 9.3.2

Prepare a face image

Load photo.jpg, convert it to grayscale, and equalize its histogram. Pass the prepared image to the face classifier, while retaining the original color frame for drawing the detected rectangle and center marker. 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
Defines single-channel intensities, shows image shapes and conversion code, then combines conversion with histogram equalization.
Examples
Uses a 640-by-480 face photograph, converts it with cv2.cvtColor, and marks the detected face on the original frame.
What it assumes
Uses a loaded image frame and basic Python function calls; introduces the grayscale conversion directly.
What you can do after
Apply it via the OpenCV cv2 library to prepare images for a face classifier.
How it connects
Connects grayscale images with face detection, histogram equalization, pixel intensity, and OpenCV color conversion.

Also explained in

Compare how 3 books explain grayscale image →

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.