The mask shows where the ball is. Now steer toward it.
Key idea
The target's center (centroid) is the average column of the pixels that passed. The steering error is that column minus the image's middle column, $w/2$. Feed it to a proportional controller: turn toward the target, harder when it is farther off center.
Why it works
This is closed-loop control with a camera as the sensor. Each frame measures the error and the robot corrects; as it turns, the ball moves toward the middle and the error shrinks. It is how many FIRST robots line up with targets.
Worked example: Steering error
Image 320 wide; kept pixels in columns 210, 214, 220, 224.
- Center: $(210 + 214 + 220 + 224)/4 = 217$.
- Error: $217 - 160 = 57$.
Answer: 57 pixels right of center: turn right.
Common mistake
Measuring the error from the left edge instead of from the center.
Hands-on: a ball-chasing robot
Put a camera on your mobile robot. Each frame: threshold for your ball's color, find the center, and set the wheel speeds from the steering error. Start with a small gain and raise it until the robot turns briskly without wobbling.
Safety. Chase a soft ball in an open room at low speed, with the power switch in reach. Keep the robot away from stairs.