Computer vision basics
What you’ll learn
- How a computer stores an image
- Finding a coloured object with thresholding in HSV
- Contours, and the step up to AI object detection
Images are grids of numbers
A colour image is a grid of pixels, and each pixel has three numbers (blue, green and red in OpenCV’s order), each 0–255. A 640 × 480 image is 307,200 pixels.
HSV is better for colour
Lighting changes RGB values a lot. HSV separates Hue (the colour itself), Saturation (how strong) and Value (how bright), so “red-ish” stays in a narrow hue range even as light changes.
Finding a coloured object
- Convert the image to HSV.
- Threshold: keep only pixels within a hue/saturation/value range → a black-and-white mask.
- Clean the mask (erode/dilate to remove speckles).
- Find contours (outlines of white areas) and take the biggest.
- Its centre gives the object’s position; its area hints at distance.
Beyond colour
To recognise things (people, cups, signs) robots use trained neural networks (for example YOLO-family detectors) that output boxes and labels. These need more computing power: a Raspberry Pi 5 can run small models, and AI accelerators make it faster (see AI on small boards).
Find a red ball (Python + OpenCV)
import cv2import numpy as np
cam = cv2.VideoCapture(0)while True: ok, frame = cam.read() if not ok: break hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) # red wraps around hue 0, so use two ranges mask = cv2.inRange(hsv, (0, 120, 70), (10, 255, 255)) | cv2.inRange(hsv, (170, 120, 70), (180, 255, 255)) mask = cv2.erode(mask, None, iterations=2) mask = cv2.dilate(mask, None, iterations=2) contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) if contours: c = max(contours, key=cv2.contourArea) (x, y), r = cv2.minEnclosingCircle(c) if r > 10: cv2.circle(frame, (int(x), int(y)), int(r), (0, 255, 0), 2) print("ball at x =", int(x)) # steer to keep x near the centre cv2.imshow("view", frame) if cv2.waitKey(1) == 27: # Esc quits breakSteering from the result
error = x - frame_width / 2, then turn proportionally, as in Feedback and PID.
Pixel mathsEasy
How many numbers are stored in one 320 × 240 colour image? 320 × 240 × 3 = 230,400.
Why HSV?Medium
Explain why a threshold in HSV works better than in RGB when a cloud passes over the sun. The light change mostly affects V (brightness); the hue stays similar, so the same H range still matches the object. In RGB all three values change together.
Run itHard
On a computer with a webcam and Python, install opencv-python and run the red-ball example. Change the HSV range to track a green object instead.
Green hue is roughly 35–85 in OpenCV’s 0–180 hue scale.
Quick check
Finished this lesson?