ApplicationsExplainer

HSV color segmentation with a saturated single-color background

How HSV color segmentation works: a saturated blue background and an S-channel threshold lift a dark object out of clutter, with a worked example.

LumaSpec Vision11 min read Share on LinkedIn

On this page 9 sections
  1. 01Why does gray-level thresholding fail on a dark object against a cluttered background?
  2. 02Does a single-color background really fix the separation?
  3. 03How do you convert RGB to HSV and threshold the saturation channel?
  4. 04Which saturation and hue thresholds do you start with?
  5. 05Which camera and lights make the saturation channel reliable?
  6. 06What does the light controller add to a color segmentation cell?
  7. 07When is HSV segmentation the wrong choice?
  8. 08What do you check before ordering the parts?
  9. 09Questions

Segmenting a dark, low-contrast object — an animal on bedding, a black rubber part on a worn conveyor, dark corrosion on gray castings — defeats the usual gray-level threshold because the object and everything around it share one brightness band. The fix, when you control the scene, is to make the background one saturated color and move the decision from brightness to saturation: convert the image to HSV, threshold the S channel, and gate on hue. This guide gives the conversion, starting threshold values with their margins, a pixel-level worked example, the lighting and camera hardware that keep the channel reliable, and the cases where a different method wins. The numbers assume an 8-bit color camera and a matte blue backdrop; the arithmetic transfers to any hue you choose.

Why does gray-level thresholding fail on a dark object against a cluttered background?

Because the object and the clutter occupy the same brightness band: dark hide, wet bedding and shadowed grass all fall between roughly 20% and 45% gray, so no single gray cut can split them and the mask shreds into fragments.

Set the cut at 30% and half the bedding joins the mask. Set it at 55% and the animal's shaded flank disappears. Otsu's method assumes two separable modes, so it lands on a compromise line that belongs to neither population — the Lab-color Otsu variant changes the axis but still needs the modes to separate. Local adaptive thresholding fixes uneven shading across the frame, which is a different problem: it cannot tell two gray populations that overlap. Shading correction removes vignetting and lamp falloff, not identity. The failure is missing information, not a badly tuned parameter: in a plain grayscale image there is no number that separates a dark animal from dark soil. You have to add one, and the cheapest way is a saturated background.

Does a single-color background really fix the separation?

Yes, if the background is saturated: a blue wash holds the background's saturation near 78% even when its brightness halves, while the object stays below 10%, so the separation moves to an axis that shadows cannot move.

Saturation is a ratio: S = (max − min) / max over the R, G and B counts. Multiply all three channels by the same factor and S does not change. Take a backdrop pixel reading (40, 60, 180): S = (180 − 40) / 180 = 78%. Halve the light on the same surface — (20, 30, 90) — and S = 70 / 90 = 78% again, while the blue channel fell from 180 to 90. A gray cut at 130 accepts one pixel and rejects the other, so the mask grows and shrinks with the lights; the S cut at 40% accepts both. Shadows, working-distance falloff and lamp aging move brightness, not the color identity of a saturated surface.

Two ways to build the backdrop, decided by ambient control. Saturated paint under white light keeps its hue even when daylight wanders, so it is the choice for open pens and yards. A blue LED wash on a white or gray backdrop does the same job inside a cell, needs no paint to wear or clean, and dims on command.

How do you convert RGB to HSV and threshold the saturation channel?

Three steps: split the image into its R, G and B planes, convert to HSV, and threshold the S channel — the split-and-convert pair appears in most vision libraries as decompose3 and trans_from_rgb.

  1. Lock the camera first: fixed exposure, fixed gain, white balance frozen. Any auto mode rescales the channels and drags S with it — see the auto-mode brightness drift fix.
  2. Split the color image into its R, G and B planes — the decompose3-style operator.
  3. Convert to HSV with the trans_from_rgb-style call; you get three planes, H, S and V.
  4. Background pixel: S ≥ 40% and H inside 210°-270°. Object pixel: the complement.
  5. Clean the mask: a 3 × 3 opening drops speckle, hole filling recovers what white markings carved out, and regions under about 1% of the frame are discarded.
S = (max(R, G, B) - min(R, G, B)) / max(R, G, B) background pixel: S >= S_cut AND H inside the hue band object pixel: everything else

Run both channel tests in one decision so a brown patch that clears the S cut still fails the hue band.

Which saturation and hue thresholds do you start with?

Start the saturation cut at 40% and the hue band at 240° ± 30°: in the worked example below that leaves a 38-point margin to the background mode and a 32-point margin to the object mode.

Example 8-bit readings from a blue-lit cell:

Pixel R G B Δ = max − min S = Δ/max Hue
Lit backdrop 40 60 180 140 140/180 = 78% 231°
Backdrop at half light 20 30 90 70 70/90 = 78% 231°
Dark hide 52 50 48 4 4/52 = 8% —
Brown patch 120 75 48 72 72/120 = 60% ≈22°
White marking 180 178 172 8 8/180 = 4% —

The brown patch passes the S cut at 60% but fails the hue gate: its hue is 60° × (75 − 48) / 72 ≈ 22°, and the band starts at 210°. The white marking sits at 4%, far below the cut. The backdrop hue of 231° comes from the blue-branch of the hue formula; the shadowed pixel keeps it because hue is scale-free like S.

Treat 40% and 240° ± 30° as starting points, not constants. Grab 20-30 images across the shift — morning, noon, lights on and off — draw regions on known background and object, and plot the two S histograms. Put the cut in the valley, and keep at least 20 percentage points between the cut and both modes; below that, noise eats the margin and the mask flickers. If you cannot open the gap, change the lighting, not the software.

One real limit: ratios of small numbers are noise. A near-black pixel at (30, 32, 36) reads S = 6/36 = 17%; swing each channel by ±2 counts and S moves between roughly 12% and 26%. Treat pixels below about 15% of full scale as object without testing S, or add light until the object's darkest relevant pixels clear that line.

Which camera and lights make the saturation channel reliable?

A color sensor is mandatory — a mono camera sees the blue wash as plain gray and the whole method collapses — and the light must be diffuse enough that no specular highlight desaturates the background.

The MV-SC6050C-08M-XBN pairs a 5 MP color sensor with an integrated multi-color light source and an 8 mm M12 lens; the MV-SC3050XC-16M-ATN-H is a 5 MP color vision sensor with a 1/1.45" sensor, global shutter and a Fast Ethernet link. Both appear in the color smart cameras category:

Smart cameras & vision sensorsHikrobot MV-SC6050C-08M-XBN SC6000 Series Smart Camera, 5 MP, Color, 8 mm focal length, M12-mount, Multi-color light sourceMV-SC6050C-08M-XBN
Interface
GigE
Resolution
5 MP
$3,449.00 · quotedView and quote
Smart cameras & vision sensorsHikrobot MV-SC3050XC-16M-ATN-H 5 MP color vision sensorMV-SC3050XC-16M-ATN-H
Interface
Fast Ethernet
Resolution
5 MP
$1,809.00View part

Specular highlights are the enemy: a white reflection adds the same counts to R, G and B, pulling the ratio down. Add 60 counts of glare to the lit backdrop pixel and you get (100, 120, 240): S = 140/240 = 58%. The cut at 40% still holds, but the margin shrank from 38 points to 18 — under the 20-point rule. Diffuse the wash, angle it off the mirror path, or cross-polarize; pixels that already saturate need the fixes in this glare removal guide. An integrated polarized light saves you the optics: the LS-SMC-0063 carries a polarized white light, a 16 mm lens, 60 fps and 1.6 MP on GigE.

The backdrop light is a stock catalogue choice, not custom work: the bar lights finder's color filter lists 1,098 blue models, and 1,098 more in the shop finder break down by size and diffusion. Run two bar lights so their beams overlap across the whole backdrop, mounted 20-30° off the backdrop plane so no specular path reaches the lens. A standard 290 x 30 mm unit and a diffused 372 x 28 mm unit cover most conveyor-width backdrops:

Bar lights290 x 30 mm LED bar light (LS-BAR-1047)LS-BAR-1047
Size
200-400 mm
Type
Standard
$449.00View part
Bar lightsBar light, 372 x 28 mm, White (LS-BAR-1276)LS-BAR-1276
Size
200-400 mm
Type
Diffused
$439.00View part

Daylight that survives the strobe can be blocked at the lens with a bandpass filter matched to the LED — this filter and LED pairing shows the method.

Resolution is rarely the deciding number. The mask needs 3 px across the smallest feature it must keep: over a 600 mm field, 12 mm features need (600 ÷ 12) × 3 = 150 px of sensor width, which every camera in the category clears by an order of magnitude. A 5 MP color sensor in a 4:3 frame has about 2,592 × 1,944 px, so 600 mm gives 4.3 px/mm and the 12 mm feature spans about 52 px. Spend the budget on light quality, not pixels:

Camera resolution for the smallest feature
Units
Pixels needed
–
Sensor size
– MP
Smallest sensor that fits
–
Pixel on the part
– mm
Pixels across the feature, that sensor
–

Pixels needed = field / feature × pixels per feature on each axis (field incl. margin). The lens must resolve the pixels too: Nyquist = 1000 / (2 × pixel µm) lp/mm.

Find cameras with enough pixels Open the full calculator

What does the light controller add to a color segmentation cell?

Two things: output stability, so the saturation histogram sits in the same place at 6 a.m. and 6 p.m., and strobe timing, so the pulse fills the exposure and daylight never reaches the sensor.

Regulated constant-voltage or constant-current output holds the LED's spectrum and brightness fixed from shot to shot; an unregulated supply drifts with mains and temperature and moves your histogram with it. Strobing does two jobs at once: the pulse fills the exposure at rated current, freezing a walking animal or moving part, and ambient light outside the pulse gets no exposure time. Give each backdrop light run its own channel so both fire together — a 4-channel box covers two backdrop runs plus a spot on the object. Verify the timing once with a scope using this pulse-overlap procedure.

The LS-CTL-0070 is a 4-channel, 200 W digital control box with strobe output on 24 V constant-voltage channels and an RS232 link; the rest of the range sits in the light controllers category and finder:

Light controllersDigital control box, 4 ch, 200 W, RS232 (LS-CTL-0070)LS-CTL-0070
Output
Constant voltage 24 V
Channels
4 channels
Control
Digital
Feature
Strobe
$549.00View partBuild a system with it

When is HSV segmentation the wrong choice?

When the part itself is saturated in the background's hue: blue parts on a blue background have overlapping S and H distributions, and no threshold recovers them cleanly — change the background color first, software second.

Pick a backdrop hue at least 90° away from every hue the part can be; the light-color contrast rules and this LED color chooser both walk the selection. The second wrong case is a fully enclosed station whose parts never share a gray band with the background: there, a mono camera with a blue wash and a plain gray threshold — the mono contrast method — has fewer failure modes, uses every pixel of the sensor and needs no color math. The trade is brightness: gray level follows the lamp, so the enclosure must keep ambient out for good.

Decide it this way: color plus HSV when the scene sees daylight, varied lamps, or part colors you must separate; mono plus a wash when you control everything and simplicity wins. The other segmentation workflows sit on the applications topic page.

What do you check before ordering the parts?

Four checks: color sensor confirmed, blue bar lights long enough to overlap on the background, a strobe controller with a channel per light run, and 20 test images spanning the worst brightness you will see on the line.

  • Color sensor with global shutter if the object moves — both cameras above are global-shutter parts; filter the camera finder by color and shutter.
  • Backdrop lights that overlap across the frame: the bar-light finder splits into 335 models in the 200-400 mm class and 744 above 400 mm.
  • One strobe-capable controller channel per light run, sized to the light's power.
  • A diffuser or polarizer in the path so glare never eats the 20-point margin.
  • Twenty sample images from the worst-case lighting, histogram-checked, before you freeze the thresholds.

Start with the lighting configurator to match lights and controller to the camera, and lean on the free expert support that comes with every order if you want your S histograms reviewed before the parts ship. The decision in one line: dark object, cluttered background, controlled scene — saturate the backdrop, cut S at the valley with a 20-point margin, gate the hue band, and the gray-level problem disappears.

Questions

Can a mono camera and a colored light replace the color camera?

Yes, in a closed cell. Blue light on a white background makes the background bright and a dark part stays dark, so a plain gray threshold works and the mono sensor uses every pixel. Choose it when you can control all ambient light; keep color plus HSV when daylight, lamps or saturated part colors vary.

How small a feature can the saturation mask still resolve?

Apply the 3-pixel rule: a feature must span at least 3 px to survive thresholding and 5 px for a measurement. At the worked example's 4.3 px/mm, that is about 0.7 mm detectable and 1.2 mm measurable; a 12 mm feature spans 52 px, so edges stay smooth. Recompute with the camera resolution calculator for your field.

Why do my hue numbers not match the degrees in this guide?

Libraries encode hue differently: some keep the 0-360° wheel, others fold it into a 0-255 or 0-180 integer range to fit an 8-bit channel, and saturation may print 0-255 instead of a percentage. Convert by proportional scaling, and read the documentation range before you type the hue band — a 30° half-width becomes about 15 units on a 0-180 scale and 21 on a 0-255 scale.

What keeps the mask stable over months of production?

Saturation is a ratio, so LED aging and dust dim the background without moving S much; the drift to watch is ambient leaks and auto modes. Turn auto exposure, gain and white balance off, re-run the 20-image histogram check each quarter, and clean the diffuser. If the valley between the S modes narrows below a 20-point margin, raise the light before touching the threshold.

Does the background have to be blue?

No. Any hue at least 90° away from the part's dominant hues works: orange or red backdrops suit blue-gray metal, green suits red-brown parts. What matters is that the lit background keeps saturation at least twice your S cut — with a 40% cut that means 80% or more — and that no part color shares the band. Blue is simply the most common choice against gray and brown objects.