The fix is at capture, not in the threshold: swap the directional front light for a backlight silhouette, stop the lens down until depth of field covers the height spread (in the worked example below, from f/2.8 to f/5.6 turns 0.63 mm of depth of field into 1.26 mm over a 1.0 mm height spread), and keep background drift under about 12 gray counts of a 50-count edge separation. You binarize the frame and the particles in one zone segment cleanly, while particles sitting a little higher or lower smear into the background — so the outlines get traced by hand, and every traced number carries a hand with it. The fault is almost never the threshold algorithm. Under a directional light, height variation becomes gray-level variation, and a depth of field shorter than the height spread softens edges until no single gray level separates particle from background. This guide gives two tests that isolate the cause, then three fixes that make one global threshold work across the whole field, with the arithmetic that sizes each.
Why a global threshold cannot survive height variation
A global threshold applies one gray level to every pixel. It holds only while two assumptions survive: the background and particle modes stay separated everywhere in the field, and they stay separated from frame to frame. Height variation attacks both.
Raking a spot light across a tray turns every height step into intensity gradients, shadows and specular glints, so the background gray slides across the field — easily by tens of counts, which can exceed the particle-to-background separation itself in a low-contrast image. Separately, a particle outside the depth of field shows an edge transition spread over five or ten pixels instead of two, so the gradient at the true boundary collapses and a small threshold shift walks the boundary across many pixels of area.
The standing order for stable 2D work runs lighting, then lens, then exposure, algorithm last — the same order that stabilizes any 2D inspection system. The algorithm is where the failure surfaces, not where it lives.
Size the illumination to two numbers and the threshold stops hunting. Keep the gray separation between bright background and dark particle at 50 counts or more on an 8-bit camera, and keep the background's drift — across the field and across the height range — under a quarter of that separation, about 12 counts when the separation is 50. With those margins an automatic global method such as Otsu's threshold lands in the histogram valley on every frame. Without them, every zone and every frame wants its own threshold, which adds a second source of variance next to your hand.
Two tests that isolate the fault
Spatial test. Clear the field of parts, or pick a clean region, and compare the mean gray of the four quadrants at the exposure you intend to run. If quadrant means spread by more than about 12 counts at a 50-count separation, the light is shading the field and no single threshold will serve every zone.
Temporal test. Lock the scene, capture 20 frames and watch the background mode. If it wanders while nothing moved, an automatic exposure or gain mode is renormalizing your gray levels — switch every auto mode off before touching anything else — or the LED output is drifting with temperature, which Fix 3 addresses.
One software stopgap earns its keep: adaptive or per-zone thresholding masks mild shading well enough for detection, and adaptive thresholding for uneven part shading covers the trade-offs. For measurement it quietly moves area from one side of each particle to the other, so treat it as a patch while you fix the light. If you are writing the threshold logic yourself, start from the threshold selection basics.
Likely causes, in order of probability
Match your symptom to the first column; the test column settles it in minutes.
| Clue in the image | Cause | Test | Fix |
|---|---|---|---|
| Failures cluster on one side, tracking the light direction | Directional light shades height steps | Blank-field quadrant means differ by more than a quarter of the edge separation | Fix 1: backlight or diffuse geometry |
| High parts crisp, low parts fuzzy with wide, soft gray edges | Depth of field shorter than the height spread | Edge transition wider than about 3 px at one height and narrow at the other | Fix 2: stop down to the DOF budget |
| Same parts, different gray levels frame to frame | Auto exposure or gain, LED warm-up | Locked scene, 20 frames: the background mode moves | Fix 3: manual modes, constant current |
| Particle interiors as mottled as their edges | Specular surfaces mirror the light direction | Rotate a polarizer: the pattern shifts | Diffuse dome or axial diffuse, crossed polarizer |
| Blank background still uneven under a translucent substrate | Background transmission varies | Blank backlight frame reproduces the pattern | Diffuser under the substrate, flat-field reference |
Fix 1: a backlight turns height into a silhouette
For particle size and shape, the strongest fix is geometric: put a backlight behind the parts and image the silhouette. Opaque particles simply block the light, so the gray level at their edge reports whether light reached the sensor — not how high the particle sits. Height variation stops producing gray variation, which is exactly the property a global threshold needs, and edge contrast hits its maximum because you are no longer asking a surface to reflect light evenly, only to block it.
Run red for most mono sensors; choose 850 nm when ambient light or colored substrates leak into the silhouette. One physics note before picking infrared: diffraction blur scales with wavelength, so at the same f-number an 850 nm edge is about 1.5x softer than a red one, and at tight apertures that eats subpixel accuracy. When edges must hold subpixel accuracy and the parts are thin, a collimated backlight trades throughput for edge sharpness; the light finder filters both by size and wavelength.
No transparent substrate? Put a bright, mirror-smooth plate under the parts and light them coaxially: the plate returns the illumination straight back to the camera and reads as a bright background, while every raised particle scatters light off-axis and goes dark. A small spot light such as the LS-SPT-0133, rated 4 W, drives that rig comfortably at bench working distances. For rounded or glossy particles where even this leaves highlights, a diffuse dome flattens the surface gray, exactly as it does for shiny metal pins.
Backlights sized for common bench fields:
- Size
- 100-200 mm
- Type
- Standard
- Size
- 100-200 mm
- Type
- Standard
- Size
- 100-200 mm
- Type
- Collimated
The LS-BKL-1468 is the red generalist: a flat 150 x 150 mm panel on 24 V. The LS-BKL-0416 exposes a 136.4 x 136.4 mm lit area at 850 nm, constant-on with no driver needed. The LS-BKL-0830 is the collimated option at 120 x 120 mm.
Fix 2: size the depth of field to the height spread
A silhouette fixes the gray level, but the aperture still has to hold the highest and lowest particle inside focus: an out-of-focus silhouette edge is a blurred edge, and a blurred edge is area that changes with every threshold click. Budget it with the thin-lens estimate:
Here N is the f-number and c the circle of confusion — set c to two pixels for measurement work, one pixel for the strictest edges. The estimate is good enough to pick an aperture; confirm it by sweeping a shim stack through the height range and checking that the edge transition stays under about three pixels.
Decision rule: pick N so the total depth of field is at least 1.2 times the height spread, with the 20% margin covering stage flatness and focus drift. Then check diffraction before committing: the Airy disk diameter is about 2.44 · λ · N_eff, with N_eff = N · (1 + m) at these magnifications. Once the disk passes roughly three pixels, the edge is diffraction-limited and no threshold choice will sharpen it.
Worked example: 0.25 mm particles across a ±0.5 mm height spread
Inputs, illustrative but typical of a bench particle station:
- Sensor: 2448 x 2048 pixels at 3.45 µm pitch (a common 5 MP class sensor — substitute your own numbers).
- Smallest particle to size: 0.25 mm, with a budget of at least 20 pixels across it.
- Height spread within one field: 1.0 mm total (±0.5 mm).
- Circle of confusion c = 2 px = 6.9 µm; λ ≈ 0.55 µm.
Magnification. m ≥ pixel pitch × 20 / particle size = 3.45 µm × 20 / 250 µm = 0.276, so run m = 0.28. The object-side pixel is 3.45 µm / 0.28 = 12.3 µm, the smallest particle spans 250 / 12.3 ≈ 20 px, and the field is 2448 × 12.3 µm ≈ 30.2 mm wide. Why 20 pixels: a boundary that moves one pixel of diameter changes a particle's area by about 2/d, which is 10% at d = 20 px. Subpixel edge methods cut the edge uncertainty to a tenth of a pixel and bring the same 20 px particle near 1%.
Aperture. The depth-of-field budget is 1.2 × 1.0 mm = 1.2 mm. With m = 0.28: DOF = 2 · N · 0.0069 mm · 1.28 / 0.0784 = 0.225 · N mm. At f/2.8 that is 0.63 mm — only ±0.31 mm around focus, less than half the spread. At f/5.6 it is 1.26 mm, which covers the 1.0 mm spread with margin. At f/8 it is 1.80 mm.
Diffraction ceiling. At f/5.6, N_eff = 5.6 × 1.28 = 7.2, so the Airy disk is 2.44 × 0.55 µm × 7.2 ≈ 9.7 µm ≈ 2.8 px — just inside the 3 px limit. At f/8, N_eff = 10.2 and the disk grows to about 4 px: the edges go soft.
Decision. Run m = 0.28 at f/5.6: a 30 mm field, 20 px on the smallest particle, 1.26 mm of depth of field over a 1.0 mm spread, diffraction under 3 px. Stopping from f/2.8 to f/5.6 costs two stops, i.e. 4x the light — pay it back with exposure time or with a brighter strobed pulse from the backlight. If the spread grows toward 1.5 mm, no aperture serves both budgets at this pixel pitch; level the presentation instead of buying 4 px of blur at f/8.
Re-run the aperture for your own sensor and spread with the depth-of-field calculator:
Fix 3: pin the gray level frame to frame
Switch off every automatic exposure, gain and white-balance mode; a static scene must produce a static histogram, and if gray levels still wander with everything manual, turning every auto mode off is the first move. If the wander persists, the LEDs are drifting: output falls as the array warms even at a fixed voltage. Constant-current drive pins the current, and a controller that monitors and corrects output pins the light itself — closed-loop lighting control shows how the loop is built.
For moving parts, strobe the backlight hard and short. The pulse freezes motion, and because average power stays low the array runs cooler and the calibration holds longer — the same mechanism that extends LED life in strobe mode. Both constant-voltage and constant-current light controllers handle strobing; pick by total wattage and channel count.
A constant-current controller sized for that station:
- Output
- Constant current
- Channels
- 4 channels
- Control
- Digital
- Feature
- Constant current
The LS-CTL-0180 drives four channels at 120 W total, measures 89 x 147 mm and is made for 24 V lights — room for the backlight plus two auxiliary channels.
Set-up sequence and acceptance check
- Mount the backlight under the substrate, diffuser facing up, camera squared over the field.
- Set magnification for at least 20 px across the smallest particle.
- Focus at the mid-height of the spread, then stop down until depth of field covers 1.2 times the spread; check the Airy disk stays under 3 px.
- Set manual exposure so the background sits near 200 of 255 counts, with the darkest particle edge clear of clipping.
- Lock exposure and gain; put the light on constant current; strobe if the parts move.
- Run the blank-field quadrant test at the top and bottom of the height range; quadrant means must stay within a quarter of the edge separation.
- Threshold with Otsu as the default and capture 20 frames of a stationary field, recording per-particle area.
- Accept when area repeatability meets the pixel budget: about 10% with ±1 px edges at a 20 px particle, about 1% with subpixel edges.
When a step fails, it fails loudly and points back: quadrant means out of band returns you to Fix 1, edge transitions wider than 3 px on the low parts return you to Fix 2, a wandering background mode returns you to Fix 3.
When the usual answer is the wrong one
- Translucent particles: the silhouette is graded by thickness, not just position. Pick a wavelength where the material is opaque, or accept a thickness-biased edge and record that fact with the measurement.
- Surface defects — scratches, residue, stains: a backlight deletes exactly the surface contrast those defects live on. Switch geometry instead; choosing the right illumination geometry and the reflection-law logic of bright versus dark field decide between grazing, diffuse and coaxial.
- If what you actually need is each particle's height, stop fighting the threshold: that is a profiling job, and a clean laser height line measures it directly.
- Very small particles at low magnification: if the 20 px budget forces a field wider than one frame, step the stage and stitch, or split the job into two magnifications.
Prevention checklist
Each item removes one of the three failure modes: gray drift, blurred edges, wandering levels.
Lighting
- Backlight every size and shape measurement; front light only surface features.
- Diffuser under the substrate, plus a captured blank-field reference for flat-field correction.
- Constant-current drive from day one; strobe for moving parts.
Optics
- Depth of field at least 1.2 times the height spread, checked with a shim sweep.
- Airy disk under 3 px at the working f-number.
- At least 20 px across the smallest particle, with subpixel edges for percent-level areas.
Exposure and threshold
- Every auto mode off; background near 200 of 255 counts, no clipping.
- Blank-field quadrant test at both height extremes after every lamp or lens change.
- A 20-frame static-field repeatability run before any measurement batch.
If you are sizing the station now, the configurator walks light, lens and controller in that order and filters to parts that match the budgets above; for anything unusual, the contact page gets the field sketch and particle sizes to the team for a check before you order.
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.
Questions
Why does one threshold work on half the image and fail on the other half?
That is spatial shading, not a bad threshold value. Directional light plus height variation moves the background gray across the field, so the histogram valley sits at different levels in different zones. Confirm with the blank-field quadrant test, then fix the geometry with a backlight silhouette or diffuse lighting. As a stopgap, split the field into zones with separate thresholds: acceptable for detection, biased for measurement.
How much gray-level contrast does binarization need to be repeatable?
Aim for at least 50 gray counts of separation between background and particle on an 8-bit camera, with background drift under about 12 counts across the field and the height range. Below roughly 20 counts, sensor noise alone moves the boundary every frame and no threshold choice stabilizes it. A backlight silhouette reaches several hundred counts of separation, which is why it is the default for size measurement.
Does an infrared backlight measure differently from a red one?
The silhouette geometry is identical, and mono silicon sensors respond well at 850 nm. Two differences matter: diffraction blur grows with wavelength, so edges are about 1.5x softer at 850 nm than at 550 nm for the same f-number, and the light is invisible, so align on the camera image rather than by eye. Colored substrates that leak in visible light often read fully opaque in infrared.
How many pixels do I need across the smallest particle?
Budget at least 20 pixels across the smallest particle you must size. A boundary that moves one pixel of diameter changes area by roughly 2/d, so a 20 px particle moves about 10% and a 60 px particle about 3%. Subpixel edge fitting cuts edge uncertainty to a tenth of a pixel and brings a 20 px particle near 1% area repeatability, usually cheaper than 200 px of magnification.
Can flat-field correction rescue a shaded image?
Sometimes, and it is worth trying before rebuilding the station: capture a blank reference at the same exposure, divide every frame by it, and mild multiplicative shading largely cancels. It cannot recover hard shadows, specular hotspots or out-of-focus edges, because those are not multiplicative. Treat corrected images as inspection-grade; for dimensional measurement, fix the lighting geometry so the raw histogram is already clean.



