ApplicationsHow-to

Identify part front and back by color differences

Identify a part's front from its back by measuring one color or gray feature in a fixed ROI under locked lighting, then thresholding it, with a worked example.

LumaSpec Vision11 min read Share on LinkedIn

On this page 11 sections
  1. 01When color separation is the right tool
  2. 02Lock the lighting and the exposure first
  3. 03Fix the ROI on the part, not on the image
  4. 04Choose one feature: mean gray, channel difference or hue fraction
  5. 05Worked example: light front against a dark back, threshold at 147 gray levels
  6. 06Set the decision rule and the unsure band
  7. 07Prove it holds across a shift
  8. 08Common mistakes that move the mean
  9. 09When the two faces are the same color
  10. 10Parts for this station and where to start
  11. 11Questions

Parts come out of trays, bowls and operators' hands face-up or face-down, and the next station — printing, gluing, assembly — accepts only one of the two. The dependable machine-vision answer is not a full 360° shape match; it is one number: the mean gray value, or a color-channel difference, measured inside a fixed region of interest under lighting you have locked down. Threshold that number and the face is classified in the time it takes to sum a few hundred thousand pixels. This guide takes you through the whole set-up: the measurement that tells you whether color can separate your faces, the locks that keep the number stable, the ROI and feature to use, a worked threshold calculation, and the shift-length checks that prove the decision holds.

When color separation is the right tool

Measure before you model. Mount a candidate light over the station, run 20 parts you know are front-up and 20 known back-up under the camera, and compute the mean gray value of a region of interest on the face each time. The gap between the two group means decides the approach:

  • 30 gray levels or more apart (on the 8-bit 0-255 scale): color classification carries the job; build the threshold of the worked example below.
  • 15-30 levels: workable, but only with tight lighting control and the unsure band in place.
  • Under 15 levels: reflectance will not separate these faces. Change the measurement, not the software — grazing light for texture, back-light for a silhouette difference, or a height reading.

Contrast between two faces comes mostly from the light's angle and spread, not its brightness; the lighting geometry guide to image contrast shows how camera, light and part positions set what you can measure. One hardware note before you continue: if the part moves while it is scanned, use a global shutter sensor. A rolling shutter reads the face line by line and smears motion straight into your mean.

Lock the lighting and the exposure first

The gray gap you measured survives only while illumination stays constant. Four locks, in order:

  1. Fixed light output. Drive the ring light from a current-regulated driver or a strobe controller at a fixed pulse; a dimmer set by eye is not a setting.
  2. Manual exposure, manual gain. Set both once and record the values. Auto exposure re-negotiates every frame and moves your mean with nothing changed on the belt.
  3. Shroud the station. Ambient light adds to the measurement and changes through the day; the guide on controlling ambient light in pick-and-place vision shows how to find and kill the leaks.
  4. Warm-up. LED output shifts as the array reaches temperature; give it 15-30 minutes before any golden-part measurement.

Use white light whenever the feature involves hue — a red-only ring turns a red-and-blue pair into two shades of gray. A diffused white ring pointing straight down treats both faces to the same illumination:

Ring lightsRing light, 120 mm dia., 0°, White (LS-RNG-0426)LS-RNG-0426
Size
100-200 mm
Type
Diffused
Angle
Low angle (0-15°)
Voltage
24 V
$369.00View partBuild a system with it

Fix the ROI on the part, not on the image

The feature lives inside a region of interest, and the ROI has to satisfy three conditions:

  • It sees part, not background. Every background pixel drags the mean toward the tray. If the part can sit anywhere in the field, find the part first with a coarse pattern match or a silhouette threshold, then place the ROI relative to it; the guide on anchoring an ROI to a pattern match works for face classification exactly as it does for OCR.
  • It is big enough to average out noise. One stray pixel shifts the mean of an N-pixel ROI by at most 255/N levels. At 100 pixels that is 2.6 levels; at 10,000 pixels it is 0.03 levels. Take 100 × 100 pixels or more.
  • It sits on area both faces share. Measure the flat face, not a corner that exists on one side only, and keep the ROI well inside the part outline so edge blur and position jitter cannot pull background in; the 10%-field margin rule for small parts is a good sizing guide.

Choose one feature: mean gray, channel difference or hue fraction

Start with the simplest feature that separates, and escalate only if the measured gap falls short:

mean gray = (sum of gray values in the ROI) / (number of pixels in the ROI) channel difference = (mean red in ROI) - (mean blue in ROI) hue fraction = (ROI pixels with hue inside the print's band) / (ROI pixels)
  • Mean gray when one face is brighter than the other under your light. A monochrome camera is enough, and a colored light or a color filter on a mono sensor can exaggerate the difference.
  • Channel difference when the faces match in brightness but differ in hue — copper against stainless, or a plain white face against one with a red print. Needs white light and a locked white balance.
  • Hue fraction when the distinguishing color occupies only part of the face — a logo, a seal, a painted band. Count the pixels whose hue falls inside the band and divide by the ROI area.

When one global number cannot do it — several colors, cluttered background, faces that differ statistically rather than absolutely — that is segmentation territory; the Bayesian color segmentation walkthrough covers train-classify-threshold on color features.

For a PC-based station, a compact color GigE camera with a global shutter keeps moving faces clean:

Area scan camerasHikrobot MV-CU020-90GC 2.5 MP Area Scan Camera, GigE, GMAX4002, ColorMV-CU020-90GC
Sensor format
1/1.7"
Lens mount
C-mount
Interface
GigE
Resolution
Up to 3 MP
$309.00View partBuild a system with it

Worked example: light front against a dark back, threshold at 147 gray levels

Inputs. The part presents flat and stopped under the light. The lighting is locked as in the second section: diffused white ring at fixed current, manual exposure and gain, no auto modes, station shrouded. The ROI is 400 × 400 px = 160,000 px, anchored to the part outline. Calibration samples are 20 operator-marked front parts and 20 back parts. Measured ROI means on the 8-bit scale (0-255): front 178, back 116.

Rule. One threshold, halfway between the two class means, so the margin is the same on both sides; anything too close to the threshold goes to an unsure band instead of a decision.

Arithmetic.

  • Gap: 178 − 116 = 62 gray levels.
  • Threshold: (178 + 116) / 2 = 294 / 2 = 147 gray levels.
  • Margin each side: 178 − 147 = 31 levels, identical to 147 − 116.
  • Unsure band, ±15 levels: 147 − 15 = 132 to 147 + 15 = 162.

Result. A new part with an ROI mean above 162 is front-up; below 132 is back-up; anything from 132 to 162 is not decided — it goes to the re-inspection output.

Decision. The 62-level gap leaves 31 levels of margin, of which 15 are spent on the unsure band, so illumination drift of up to about 16 levels — LED aging over months, residual ambient, finish variation between batches — only moves borderline parts, which the band catches. Ship this threshold only with the lighting locked: if illumination scales up 10 percent, both means scale with it (178 becomes about 196, 116 becomes about 128) and the stored midpoint moves to about 162. Re-run the 20-and-20 calibration after any LED change, and if a fresh measurement lands the gap under 30 levels, fix the light or change the feature rather than trimming the band.

Set the decision rule and the unsure band

Two stored limits do all the work: accept-front at 162 and above, accept-back at 132 and below, everything between to the unsure output. That third output is the part most builds skip:

One upgrade makes the number survive illumination drift: put a diffuse white patch in the corner of the field and divide. Store the decision as a ratio — part ROI mean divided by patch mean — and re-measure the two calibration groups once to get the ratio limits. The ROI and reference-patch method for measuring light intensity is this same technique used for light monitoring.

Without a PC, a color vision sensor computes the feature on board and switches its own output to the flipper:

Smart cameras & vision sensorsHikrobot MV-SC3050XC-16M-ATN-H 5 MP color vision sensorMV-SC3050XC-16M-ATN-H
Interface
Fast Ethernet
Resolution
5 MP
Sensor format
1/1.45"
Shutter
Global
$1,809.00View part

Prove it holds across a shift

Run this sequence before the classifier sees production:

  1. Golden parts, again. After the light has warmed up, run the 40 calibration parts and confirm the two clouds of means still do not overlap and the gap is still near 62 levels.
  2. Log every part for one shift. Record the ROI mean with each decision. The unsure-band rate is your early warning: under 1 percent is healthy, above 2 percent means the lighting has moved and needs re-locking.
  3. Watch for a slide, not a spike. A steady drift through the day is ambient light or temperature; a step change is a moved shroud or a knocked light bracket.
  4. Re-run after any change. New part batch, new LED board, cleaned diffuser — each one re-baselines the calibration.
  5. Cross-check 50 production parts against manual sorting before the reject output runs unattended; zero flips is the target, and any flip points at its cause in the logged means.

The broader pattern — fix lighting, lens and exposure before touching the algorithm — is the subject of the guide to stable 2D inspection systems.

Common mistakes that move the mean

  • Auto functions left on. Auto exposure and auto gain re-balance every frame; auto white balance shifts hue features on color cameras. Turn all of them off and record the manual values — the color gain and hue troubleshooting guide starts from exactly this failure.
  • ROI catching background. A tray edge or the part's own side wall inside the ROI pulls the mean; re-check ROI placement against the worst-case part position.
  • Specular hotspot on a glossy face. Direct LEDs mirror off a shiny face and saturate those pixels; add diffusion or cross-polarize lens and light — the cross-polarization guide shows the set-up.
  • White balance re-locked per frame. Lock it once, to the actual light in the station; the auto white balance fix for repeatable color gives the routine.

When the two faces are the same color

If the 20-and-20 measurement lands under 15 levels of gap and stays there after the lighting work, reflectance is not your feature. Move to a different physical contrast:

  • Grazing light. A low-angle light turns texture, engraving and stamping into bright edges against dark flats; the low-angle versus dome comparison for engraved text shows how strongly angle changes what a face reveals.
  • Silhouette. Back-light the part: if the two faces differ in outline even by a chamfer, a simple area or contour check in the same ROI decides.
  • Height. A lid and a base differ in height even when painted identically; a profile sensor or a light section makes it a one-number decision, just like the gray mean above.

The discipline carries over unchanged: one feature, locked illumination, measured gap, halfway threshold, unsure band.

Parts for this station and where to start

The build is short: a color or mono camera with a global shutter from the area scan cameras page, a diffused ring light at fixed current, and — if you want the decision without a PC — a vision sensor from the smart cameras range. The light finder and the camera finder filter by size, angle and interface, and the configurator below starts at the lighting step, which is where this method starts:

Build the whole station

The configurator matches a camera, lens, light, light controller and cables to each other, step by step, and lists only parts that fit what you already picked.

Start with the light

If you send two photos of each face through the contact page together with the part size and line speed, we will propose the light geometry, the feature and a starting threshold for your parts.

Questions

Can a smart camera decide front and back without a PC?

Yes. A color vision sensor measures the ROI feature on board, compares it with the stored limits and switches a digital output straight to the reject flipper. The MV-SC3050XC-16M-ATN-H, a 5 MP color vision sensor with a global shutter and an M12 mount, is one such part; the same ROI, feature and threshold design applies unchanged. A ratio feature needs a second ROI for the white patch — confirm the sensor supports one before designing around it.

Should the threshold be a raw gray value or a ratio to a reference patch?

A ratio. Store part mean divided by white-patch mean and both calibration limits become dimensionless, so a 10 percent swing in illumination moves numerator and denominator together and the ratio barely moves. Raw thresholds are fine only inside a shrouded, current-regulated station — and even there, re-measure after every LED swap. Re-derive the two calibration ratios once per camera, because sensor response changes the absolute values but not the gap logic.

How many sample parts does the calibration need?

Twenty per face is the working minimum: enough to see the spread of finish and printing across a batch. Move to 50 per face when the measured gap approaches the 30-level floor, because the distribution tails start to reach the unsure band. Keep the marked parts as a golden set and re-run them whenever the light, the lens or the exposure changes.

Why did the means drift slowly through the day?

Two usual suspects: ambient light leaking past the shroud — strongest through windows and skylights, so it tracks the sun — and LED output shifting with temperature. The fix is the ratio feature plus a tighter shroud; give the light its 15-30 minute warm-up before golden runs. If the unsure-band rate climbs past 2 percent, stop and re-lock the lighting rather than adjusting the threshold to match the drift.

Do I need a color camera, or will a monochrome one do?

Monochrome is enough whenever one illumination makes the faces differ in brightness — add a colored ring light or a wavelength filter to exaggerate the gap. A color camera earns its place when the faces match in brightness but differ in hue under white light, or when you want the channel-difference or hue-fraction features. Measure the 20-and-20 gap under white light before you choose hardware.