LightingGuide

Gray level threshold measurement for stain distance on a white board

Turn stain darkness into a number: a gray level threshold on the board image gives a distance measurement in mm that repeats for any operator.

LumaSpec Vision12 min read Share on LinkedIn

On this page 10 sections
  1. 01The metric: one gray level, one distance
  2. 02What a gray level actually is
  3. 03Lighting that makes gray mean stain, not shadow
  4. 04Camera and settings you freeze
  5. 05Worked example: 120 mm of board at 300 mm
  6. 06Setting the thresholds from real boards
  7. 07From pixels to millimetres
  8. 08Checks before you order
  9. 09When the usual answer is wrong
  10. 10Questions

A white board graded by eye fails the way every subjective test fails: two inspectors, two verdicts, and nothing on record. The fix is to turn the stain into a number. A mono camera at a locked exposure, two symmetric diffused bar lights and one threshold convert stain darkness into an 8-bit gray level and stain extent into a distance in millimetres. What follows is the metric, the optics arithmetic behind it, the threshold rule, the parts that hold the number still, and the cases where the method needs a change. Any operator who loads the same board gets the same result.

The metric: one gray level, one distance

Each image gives you two measurements and one reported number:

  1. G0 — the median gray level of a clean reference patch, always somewhere in the image.
  2. The farthest row from the stain source where gray drops below T = 0.60 × G0.
  3. Report D, the distance from source edge to that crossing, in millimetres.

On a board reading G0 = 200, the threshold is 120 gray levels. A heavier stain reaches farther before fading to that level, so D grows with dirt. The pass rule becomes a distance you set — D ≤ 15 mm passes, for instance — and the operator reads one number instead of an opinion.

G0 = median gray of a clean reference ROI in the same image T = 0.60 × G0 D = N × s (N = pixels from source edge to the T crossing, s = mm per pixel)

Recomputing G0 on every image is what makes the number portable: lamps dim as they age and board batches differ in whiteness, and the 60% rule rides over both instead of drifting with them. It is the same reference-patch thinking behind measuring light intensity with an ROI and a reference method.

Method Output Operator agreement Where it breaks
Visual grading beside a reference card A grade from memory Poor — one to two gray steps of disagreement No record, no trend
Single-point gray or colour sensor ON/OFF at one spot, level shown as a number Good at that spot Distance needs a scan axis or many sensors
Mono camera, diffused light, locked exposure Gray map, D in mm Identical — software computes it Needs lighting control and frozen camera settings

The camera route wins because the sensor is already an array of thousands of those single-point detectors, and the software is the inspector.

What a gray level actually is

An 8-bit pixel reports 0 to 255 counts; one count is 1/255 = 0.39% of full scale. Below saturation, counts scale with the light the pixel collects:

gray = k × R × E R = reflectance of the board at that spot E = light on the board × exposure time k = gain factor

A stain reading 120 on a board reading 200 reflects 120/200 = 60% as much light as clean board. That ratio — not the absolute count — is the physical content of the number, which is why the threshold is written as a fraction of G0 rather than a constant. Three settings bend the scale if left on: auto exposure, auto gain and gamma correction. Lock all three, set gamma to 1.0, and the camera becomes a reflectance meter.

Lighting that makes gray mean stain, not shadow

The stain must be the only thing changing the gray level, so the light must have no direction of its own. Use two diffused bar lights, aimed from opposite sides at roughly 45°, at equal distance and equal drive. A ridge or dust pile shadows under one bar and stays lit under the other, and the diffusers stop the individual LEDs from printing bright stripes across the board. Each bar should be at least as long as the field of view it lights.

The LS-BAR-1276 is a diffused white bar light, 372 × 28 mm, 24 V — run two of them, one per side.

Bar lightsBar light, 372 x 28 mm, White (LS-BAR-1276)LS-BAR-1276
Size
200-400 mm
Type
Diffused
Voltage
24 V
$439.00View partBuild a system with it

Drive the pair strobed. The LS-CTL-0070 is a 4-channel, 200 W digital control box with an RS232 interface and a strobe feature — two channels for the bars, two spare.

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

A 1 ms pulse at full output during a 1 ms exposure makes room light a negligible share of the counts, which is the simplest way to reject ambient light in acceptance testing. White light keeps the gray judgement close to what a person sees; a coloured stain sometimes reads darker under a contrasting LED, as covered in which LED colour gives the best contrast. Whatever white you pick, hold one colour-temperature bin across replacement bars so the numbers transfer — the point argued in white LED colour temperature specification.

Camera and settings you freeze

Use a mono area scan camera. On a colour sensor, demosaicing fills each pixel from its neighbours, so gray values along the stain edge are partly computed rather than measured; mono gives one true count per pixel. Any mono GigE or USB3 model with a C-mount works — pick by cable run and host inventory, because the optics do not care. If you want headroom for a wider board or tighter tolerance in the same station, the MV-CS200-10UM is a 20 MP mono USB3.0 camera on a 1" sensor with a C-mount. A 1" format spans about 13 mm against 7.2 mm for 1/1.8", so at the same working distance it covers about 1.8× the board width at a similar pixel pitch.

Area scan camerasHikrobot MV-CS200-10UM 20 MP Area Scan Camera, USB3.0, IMX183, 2nd Gen., MonoMV-CS200-10UM
Sensor format
1"
Lens mount
C-mount
Interface
USB3
Resolution
16-25 MP
$759.00View partBuild a system with it

A rolling shutter is fine while the board is stationary between index steps.

Set the strobe width and exposure together: start at 1.0 ms, image a clean board, and adjust until G0 lands on 200. That is 78% of full scale — high enough for quiet gray values, with 55 counts of headroom before a whiter board saturates. The exposure ceiling to respect is:

E_max = E0 × 235 / G0

At E0 = 1.0 ms and G0 = 200, E_max = 1.0 × 235/200 = 1.18 ms, so 1.0 ms sits inside it with 20 counts still in reserve below saturation. Then lock exposure, lock gain, and turn every auto mode off — the whole method rests on that lock, the same foundation as any stable 2D vision inspection system.

Worked example: 120 mm of board at 300 mm

Inputs: an inspection zone 120 mm wide, a working distance of 300 mm, and a 6 MP sensor taken as 3,072 × 2,048 pixels on a 7.2 mm-wide 1/1.8" format — 2.34 µm pixels.

  1. Magnification for a 120 mm field: m = 7.2 / 120 = 0.060.
  2. Focal length: f = WD × m / (1 + m) = 300 × 0.060 / 1.060 = 17.0 mm. The stock size below that is a 16 mm lens.
  3. Field of view with 16 mm: m = f / (WD − f) = 16 / 284 = 0.0563, so FOV = 7.2 / 0.0563 = 128 mm.
  4. Scale: s = 128 / 3,072 = 0.0416 mm per pixel.
  5. Depth of field at f/8, with acceptable blur set to two pixels (c = 0.0047 mm): total DOF ≈ 2 × N × c / m² = 2 × 8 × 0.0047 / 0.0563² = 24 mm, or ±12 mm around focus — enough for a board that sits a few millimetres high or low. The DOF line is an estimate from the thin-lens formula; the rest is arithmetic on the inputs.
  6. Exposure: strobe 1.0 ms, clean board reads G0 = 200, threshold T = 0.60 × 200 = 120 gray levels.
  7. The threshold crossing sits 240 pixels from the source edge: D = 240 × 0.0416 = 9.98 ≈ 10.0 mm.

Run your own working distance and field of view through the lens focal length calculator for steps 1–3.

Lens focal length, field of view and working distance
Units
Solve for
Focal length
– mm
Field of view
– mm
Working distance
– mm
Magnification
– x
Angle of view
– °
Pixel on the part
– mm

Thin-lens model: m = sensor width / field of view, f = WD × m / (1 + m). Accurate to a few percent above about 150 mm; check the lens's minimum object distance.

Find lenses with this focal length Open the full calculator

Setting the thresholds from real boards

Two thresholds, two jobs:

  • Extent threshold T = 0.60 × G0 — where the stain officially ends. It sets D, the reported distance.
  • Detection threshold Tdet = G0 − 5σ — the faintest mark worth flagging. Measure σ as the standard deviation of the clean ROI; under strobed diffuse light it lands near 1–2 counts, so 5σ ≈ 10 and Tdet = 190 on a 200 board.

Calibrate the 60% figure against physical boards: image five clean boards and take the median G0 (say the spread is 198–204, use 200), then image a known-fail board and read the stain core (say 90). At T = 120 the crossing sits 80 counts below clean board and 30 above the stain core — clear of noise on one side, deep enough in the stain on the other. When your histogram is cleanly bimodal, Otsu's method in Lab colour space finds the split automatically.

One more correction earns its keep: a flat-field. Image a known clean reference board, store it, and divide every inspection image by it pixel by pixel. That removes the few percent of falloff at the field edges that no bar pair fully eliminates; the alternative is adaptive thresholding for uneven shading, which trades some of the physical meaning of the gray value for robustness against uneven light.

From pixels to millimetres

Calibrate the scale instead of trusting the lens math: place two dots of a target 50.00 mm apart across the board, measure their separation as 1,202 pixels, and s = 50.00 / 1,202 = 0.0416 mm per pixel — matching the optical estimate from step 4, which is the cross-check you want before production. The full calibration procedure is in measuring planar distances with a calibrated camera.

Locate the crossing between rows, not on a row: if row k reads 126 and row k+1 reads 112, the 120 crossing sits at k + (126 − 120)/(126 − 112) = k + 0.43 pixels. With the crossing at 240.43 pixels, D = 240.43 × 0.0416 = 10.0 mm. Under fixed lighting this interpolation repeats to a fraction of a pixel, so the boundary location — not the lighting — is the limit.

Checks before you order

  • Scale against tolerance. Make the pass/fail tolerance span at least five pixels: a 2 mm tolerance needs s ≤ 0.4 mm/px, a 0.2 mm tolerance needs s ≤ 0.04 mm/px. Work backwards from tolerance to pixels to field of view before buying anything.
  • Bar length. Each bar at least as long as the field of view; two of the same model, driven identically.
  • Controller channels. One strobed channel per bar, plus spares for a second inspection face later.
  • Shutter type. Rolling is fine if the board is still during exposure; if it moves, choose a global-shutter model and a pulse short enough that blur stays under one pixel.
  • Lens. f/8 at this magnification gives about ±12 mm of depth; check that your mounting tolerance fits inside it.
  • Reference hardware. A dot target for scale and one archived clean board for G0 and the flat-field.

When the usual answer is wrong

Glossy or laminated boards. A specular highlight reads 255 and moves with board tilt, and the threshold crossing then maps the hot spot, not the stain. Cross polarization kills the highlight at the cost of most of your light budget, so plan a stronger strobe pulse or a wider lens opening.

Stains lighter than the board. White residue, splatter, fingerprints. Invert the rule: T = G0 + 0.40 × (255 − G0) = 222 on a 200 board, and measure D outward exactly as before.

Coloured stains. Gray collapses hue differences, so a yellow film and a grey film of equal darkness read alike. Move the measurement into a colour space, as in colour inspection beyond raw RGB, and keep D as the extent metric within the stained hue region.

Boards that move during exposure. At 50 mm/s and s = 0.042 mm/px, exposure must stay under 0.042/50 = 0.84 ms or the crossing smears. Strobe width is your shutter.

Uniform films with no gradient. There is no crossing to find, so D is meaningless. Report reflectance instead: film index = G0(board)/G0(reference); a board at 196 against a 200 reference is 0.98. Set the pass limit on that ratio.

Start the parts list in the configurator at the lighting step, compare candidates in the camera finder, or send the board width, the working distance you have and the pass distance through the contact page and the parts list comes back matched to those numbers.

Questions

Why does my threshold distance change when a new lamp batch arrives?

Because an absolute gray threshold carries the lamp's brightness with it. Recompute G0 on every image from a clean reference patch and set T = 0.60 × G0; the crossing then sits at the same stain density regardless of lamp output or board batch. Lock exposure and gain too, or the camera re-introduces the drift you removed. From the worked example: board at 200 gives T = 120; board at 190 gives T = 114, and D stays put.

Can I run this inspection with a colour camera I already own?

Yes, with a caveat. Convert the raw Bayer data to gray, or measure a single colour plane; a demosaiced colour image interpolates each pixel from its neighbours, so values along the stain edge are partly computed rather than measured. For extent measurement that costs about a pixel of boundary sharpness, which sub-pixel interpolation mostly recovers. If the stains are coloured rather than grey, measure in Lab colour space instead of gray level.

How do I pick the pixel scale for a required distance tolerance?

Make the pass/fail tolerance span at least five pixels. A 2 mm tolerance needs s ≤ 0.4 mm per pixel; a 0.2 mm tolerance needs s ≤ 0.04 mm per pixel, which with 3,072 pixels across the sensor means a field of view no wider than about 123 mm. When the tolerance tightens, halve the field of view or double the pixel count.

Do the bar lights need strobing, or is continuous drive enough?

Continuous is enough inside a light-tight enclosure. In an open station, strobe: a 1 ms pulse at full output during a 1 ms exposure makes the room light's share of the pixel counts negligible, and the low duty cycle keeps LED junction temperature stable, which keeps gray levels stable run after run. Two bars, two strobed channels, one trigger from the camera.

The stain is a uniform film with no gradient. What do I report?

Report reflectance, not distance. With no fade from the source there is no crossing to find, so use the clean-patch gray itself: film index = G0 of the board divided by G0 of an archived clean board, both imaged in the same session with the same settings. A board reading 196 against a 200 reference has a film index of 0.98; set the pass limit on that ratio.