Chapter 160 — Computer Vision Basics: The Math Behind How AI Understands Images
1. Introduction: Digitizing the Analog World
In artificial intelligence, the modern landscape is divided into three primary paradigms: Data Science, Computer Vision (CV), and Natural Language Processing (NLP). Among these, Computer Vision represents a massive pillar dedicated to processing and understanding visual media—specifically images and videos.
┌─────────────────────────────────────────────────────────────┐
│ The Three Major AI Paradigms │
├─────────────────────────────────────────────────────────────┤
│ 1. Data Science ──► Tabular & Structured Data │
│ 2. Computer Vision (CV) ──► Images & Video Streams │
│ 3. Natural Language Processing ──► Text & Speech Sequences │
└─────────────────────────────────────────────────────────────┘
Analog vs. Digital Representation
- The Analog Real World: Human eyes perceive light as a continuous, analog stream of varying brightness and wavelengths across physical space.
- The Digital Computer World: Computers cannot process continuous analog signals; they operate strictly on quantitative numerical structures.
- Sampling & Digitization: To allow a computer to perceive an image, the continuous real-world light field is sampled onto a finite 2D spatial grid. Each individual cell within this discrete grid is assigned numerical values representing light intensity.
2. Mathematical Representation of a Digital Image
Formally, a digital image is modeled as a discrete two-dimensional spatial function, denoted as I(x, y):
I(x, y) = Light Intensity at Spatial Coordinate (x, y)
- I: Represents the light intensity (brightness or color value) at a specific coordinate.
- (x, y): Represents the discrete spatial location on the image plane.
┌─────────────────────────────────────────────────────────────┐
│ Image Coordinate System │
├─────────────────────────────────────────────────────────────┤
│ Origin (0,0) │
│ ┌───────────────────────────────► X-axis (Width n) │
│ │ (0,0) (0,1) (0,2) ... │
│ │ (1,0) I(i,j) │
│ │ (2,0) │
│ ▼ │
│ Y-axis (Height m) │
└─────────────────────────────────────────────────────────────┘
The Pixel (Picture Element)
- Definition: A pixel (short for picture element) is the smallest discrete building block of a digital image.
- Matrix Representation: Mathematically, a digital image is stored in computer memory as a 2D matrix or 2D array of size m × n:
- m: Represents the height of the image (total number of rows).
- n: Represents the width of the image (total number of columns).
- I(i, j): Denotes the pixel intensity value at row i and column j.
Standard Coordinate Conventions:
- Origin (0, 0): Positioned at the top-left corner of the image matrix.
- Horizontal Axis (x-axis): Extends from left to right along the image width (columns).
- Vertical Axis (y-axis): Extends from top to bottom down the image height (rows).
3. Grayscale Images & 8-Bit Pixel Intensity Mapping
A grayscale image contains no color hues—only varying levels of brightness across a single channel (the Gray Channel). Each pixel is represented by a single scalar numerical value.
┌─────────────────────────────────────────────────────────────┐
│ 8-Bit Pixel Intensity Scale (0–255) │
├─────────────────────────────────────────────────────────────┤
│ Pixel Value = 0 ──► Pure BLACK (Minimum Intensity) │
│ Pixel Value = 128 ──► Mid-Tone GRAY │
│ Pixel Value = 255 ──► Pure WHITE (Maximum Intensity) │
└─────────────────────────────────────────────────────────────┘
The 8-Bit Storage Standard:
Computer memory stores grayscale pixel values using 8 bits (1 byte) per pixel:
Total Intensity Levels = 28 = 256 Discrete Levels (Ranging from 0 to 255)
- 0: Represents pure black (zero light intensity).
- 255: Represents pure white (maximum light intensity).
- 1 to 254: Represent 254 intermediate shades of gray. For instance, 1 is an almost unnoticeable off-black, 128 is a neutral gray, and 254 is an off-white.
Using 8 bits per pixel maximizes memory efficiency by utilizing a full byte cleanly.
4. Color Images (RGB) & 3D Tensors
Natural color perception is constructed by combining three fundamental primary additive color channels: Red (R), Green (G), and Blue (B).
┌─────────────────────────────────────────────────────────────┐
│ 3D Color Image Tensor Layout │
├─────────────────────────────────────────────────────────────┤
│ │
│ Height (m) × Width (n) × 3 Color Channels (R, G, B) │
│ │
│ ┌───────────┐ Red Channel Matrix (R) │
│ ┌───────────┐ │ Green Channel Matrix (G) │
│ ┌───────────┐ │ │ Blue Channel Matrix (B) │
│ │ Pixel │ │ │ │
│ │ (i, j) │ │ │ Each pixel (i,j) stores vector: │
│ │ │ │─┘ [ R(i,j), G(i,j), B(i,j) ] │
│ └───────────┘─┘ │
└─────────────────────────────────────────────────────────────┘
Pixel Vector Representation:
In a color image, every spatial location (i, j) stores a vector of three numerical values:
Pixel(i, j) = [ R(i, j), G(i, j), B(i, j) ]
Where R(i, j), G(i, j), and B(i, j) each range independently from 0 to 255.
3D Tensor Transformation:
Combining spatial dimensions with color channels transforms a 2D matrix into a 3D Tensor:
Tensor Dimensions = Height (m) × Width (n) × Channels (c)
- For standard RGB images, c = 3.
- Specialized Domains: In medical imaging or remote sensing, additional channels may be included, such as a Depth channel (Z-buffer) or Infrared (IR) data.
Concrete Memory Calculation Example (HD Image):
Consider a standard High Definition (HD) image with a spatial resolution of 1920 × 1080 pixels across 3 color channels:
Total Numerical Elements = 1920 × 1080 × 3 = 6,220,800 numbers
Storing a single uncompressed HD image in computer memory requires 6.22 million numerical values (approximately 6.22 MB).
5. Fundamental Image Mathematical Operations
Because digital images are stored as numerical matrices, image processing algorithms manipulate visuals using matrix arithmetic:
┌─────────────────────────────────────────────────────────────┐
│ Basic Image Matrix Operations │
├─────────────────────────────────────────────────────────────┤
│ 1. Brightness Adjustment (Scalar Addition): │
│ I_new(x, y) = I_old(x, y) + c │
│ │
│ 2. Noise Reduction (Averaging Operation): │
│ I_average = (I₁ + I₂) / 2 │
└─────────────────────────────────────────────────────────────┘
- Brightness Adjustment (Scalar Addition): I_new(x, y) = I_old(x, y) + c. Adding a positive constant factor c to every pixel value increases overall image brightness.
- Noise Reduction (Averaging): I_average = (I₁ + I₂) / 2. Averaging pixel intensity values across neighboring pixels or successive image frames reduces random visual noise and smooths the image signal.
6. Summary Comparison Table: Grayscale vs. Color (RGB)
| Feature / Metric |
Grayscale Image |
Color (RGB) Image |
| Data Structure |
2D Matrix (m × n). |
3D Tensor (m × n × 3). |
| Channel Count |
1 Channel (Gray Channel). |
3 Channels (Red, Green, Blue). |
| Values per Pixel |
1 Scalar Numerical Value. |
Vector of 3 Values [R, G, B]. |
| Bit Depth per Channel |
8 bits (1 byte). |
8 bits per channel (24 bits total per pixel). |
| Intensity Range |
0 (Black) to 255 (White). |
0 to 255 per channel. |
| HD Memory Requirement |
1920 × 1080 × 1 = 2.07 Million Numbers. |
1920 × 1080 × 3 = 6.22 Million Numbers. |