Lesson 26: Color Recognition & Computer Vision

Teaching robots to see and understand colors in the world around them

🎯 Learning Objectives

By the end of this lesson, students will:

  • ✅ Understand color theory and RGB/HSV color spaces
  • ✅ Learn computer vision basics and image processing
  • ✅ Program ESP32-S3 camera for color detection
  • ✅ Implement color tracking and following algorithms
  • ✅ Build practical color recognition applications

Skills Developed:

  • 🔍 Computer vision programming
  • 🎨 Color space manipulation
  • 📸 Camera interfacing and control
  • 🤖 Real-time image processing
  • 🎯 Object detection and tracking

Lesson Content

1. Understanding Color and Computer Vision

Color recognition allows robots to identify, track, and respond to colored objects. This enables applications like traffic light recognition, object sorting, and color-based navigation.

Color Theory:

  • 🔴 RGB: Red, Green, Blue (0-255)
  • 🌈 HSV: Hue, Saturation, Value
  • 🎨 Perception: How cameras see color
  • 💡 Lighting: Affects detection

Computer Vision:

  • 📷 Images: Pixels and resolution
  • 🔍 Processing: Filtering and enhancement
  • 🎯 Detection: Finding colored regions
  • 📊 Analysis: Shape and size measurement

2. ESP32-S3 Camera Programming

Basic Color Detection:

#include "esp_camera.h"

// Color range structure
struct ColorRange {
  uint8_t r_min, r_max;
  uint8_t g_min, g_max;
  uint8_t b_min, b_max;
};

// Red color range
ColorRange redRange = {150, 255, 0, 100, 0, 100};

bool detectColor(uint8_t r, uint8_t g, uint8_t b, ColorRange range) {
  return (r >= range.r_min && r <= range.r_max &&
          g >= range.g_min && g <= range.g_max &&
          b >= range.b_min && b <= range.b_max);
}

Color Tracking Algorithm:

void followColorObject() {
  camera_fb_t * fb = esp_camera_fb_get();
  if (!fb) return;
  
  uint16_t* pixels = (uint16_t*)fb->buf;
  int sumX = 0, sumY = 0, count = 0;
  
  // Find color centroid
  for (int y = 0; y < fb->height; y++) {
    for (int x = 0; x < fb->width; x++) {
      int index = y * fb->width + x;
      uint16_t pixel = pixels[index];
      
      uint8_t r = (pixel >> 11) << 3;
      uint8_t g = ((pixel >> 5) & 0x3F) << 2;
      uint8_t b = (pixel & 0x1F) << 3;
      
      if (detectColor(r, g, b, redRange)) {
        sumX += x;
        sumY += y;
        count++;
      }
    }
  }
  
  if (count > 50) {
    int centerX = sumX / count;
    int error = centerX - 160; // Image center
    
    // Control robot based on object position
    if (abs(error) < 20) {
      moveForward(100);
    } else if (error > 0) {
      turnRight(50);
    } else {
      turnLeft(50);
    }
  }
  
  esp_camera_fb_return(fb);
}

3. Practical Applications

Traffic Light Recognition:

  • 🔴 Red: Stop completely
  • 🟡 Yellow: Slow down
  • 🟢 Green: Proceed forward

Object Sorting:

  • 📦 Detect object color
  • 🤖 Sort into categories
  • 📍 Move to correct location

🛠️ Hands-On Activity: Color-Following Robot

Build a robot that detects, tracks, and follows colored objects using the ESP32-S3 camera.

Materials:

  • 🤖 miniAuto robot with ESP32-S3
  • 📷 Camera module
  • 🔴 Colored objects
  • 💡 Good lighting

Steps:

  1. Initialize camera
  2. Calibrate color ranges
  3. Test detection
  4. Implement following

Safety: Keep speeds low, test in open areas, have emergency stop ready.

📝 Assessment & Homework

Quick Assessment

  • ✅ Explain RGB vs HSV color spaces
  • ✅ How does ESP32-S3 detect colors?
  • ✅ What affects color recognition accuracy?
  • ✅ Design a color-based sorting system

Homework Assignment

  • 📝 Research real-world color recognition applications
  • 🎨 Test different lighting conditions on color detection
  • 🤖 Program a simple traffic light response system
  • 📊 Document which colors work best/worst and why
← Semester Overview ← Previous Lesson
Take Quiz → Next Lesson →