ROBOTICS 101Concept explainer

Thresholding and Color Segmentation

Thresholding keeps the pixels whose values fall in a chosen range and drops the rest, making a black and white mask. It is the quickest way for a robot to find a brightly colored object. Choosing the right color space and ranges decides whether it still works when the lighting changes.

A threshold mask

A pixel passes when every channel lies in its range. In HSV, hue wraps around at 360°, so red, near 0°, needs a range that crosses zero:

$$\text{mask}(x, y) = \big[\,\lvert H - H_0\rvert_{360} \le \Delta H\,\big] \wedge \big[S \ge S_{\min}\big] \wedge \big[V \ge V_{\min}\big]$$

The object's position is then the average position of the masked pixels, its centroid.

What to notice

In HSV mode the ball stays found as you dim the light or add a shadow, as long as the value minimum is low enough. Switch to RGB mode and a fixed red minimum loses the ball in the shadow, or, if set low, starts grabbing the brown floor as well. Lowering the saturation minimum also lets in grayish floor pixels, since gray has no reliable hue. Thresholding is fast and simple, but fragile: modern robots use it for markers and bright objects, and learned segmentation for everything else.