Computer vision with OpenCV: teaching a robot to see
OpenCV is the standard library for computer vision. It takes a camera frame, a grid of pixels, and helps you extract what matters: a line, a marker, an object. Here are the ideas you will use again and again.
Color: think in HSV, not RGB
To find "the green marker" or "the black line", you filter by color. But RGB is bad at this because brightness and color are mixed. HSV (Hue, Saturation, Value) separates the color itself from how bright it is, which makes color filtering far more robust to lighting.
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, low_green, high_green)The result is a black-and-white mask: white where the color is, black elsewhere.
Contours: from pixels to shapes
Once you have a mask, contours find the outlines of the blobs. From a contour you can measure area (is it big enough to matter?), position (where is it?), and orientation (what angle?). This is how a robot turns "some green pixels" into "a marker on the left, so turn left".
Cleaning up
Real masks are messy. Two operations fix most of it: erode removes tiny specks of noise, dilate fills small holes. A quick erode-then-dilate makes blobs solid and reliable.
The reality of vision
The biggest challenge is not the code, it is the lighting. The same green looks different under different lights, so you must calibrate your color ranges on the actual environment, and often add your own lighting for consistency. When detecting complex things like objects in an open area, teams increasingly use a small neural network instead of hand-tuned color rules, because it adapts better to messy real-world scenes.