Ambient light quietly breaks front-lit inspection cells. A window, a skylight or the hall's high-bay LEDs add more light at the top of the field than at the bottom, the background climbs about 100 grey levels from one edge of the frame to the other, and a single global threshold holds in one region and fails in the next. The first instinct, reaching for a smoothing operator, makes it worse. This guide gives you the decision instead: why smoothing cannot fix a gradient, how a divide-by-reference correction flattens the frame with one extra capture, what to do when no empty reference exists, and when a shroud, a backlight or a strobe kills the gradient at the source.
Why does one global threshold fail when the top of the frame is brighter?
A single threshold works only while part pixels and background pixels occupy separate grey-level intervals at every point in the field, and an ambient gradient makes the bright top background overlap the interval the part occupies at the bottom of the frame.
The threshold test compares every pixel with one number T. To separate a darker part from a brighter background, T must sit above the part's brightest grey and below the background's darkest grey, in every corner of the image at once. A gradient breaks that in one direction. Say the background reads 200 grey levels near the top of the field and 100 near the bottom, and the part reflects 60 percent of its local background, so it reads 120 up top and 60 below. The top needs a threshold somewhere between 120 and 200; the bottom needs one between 60 and 100. No single number satisfies both, so the same part segments cleanly in one region and sinks into the background in the other.
Some of the unevenness usually starts in your own geometry: working distance and beam angle change the incident light across the field, and ambient adds on top. Both enter the image the same way, as a low-frequency addition to the illumination term, which is why one method fixes both. Lighting geometry for image contrast: camera, light and part positions shows how much placement alone moves the level.
Why not smooth the image instead?
Smoothing averages the gradient away with the edges, so a kernel large enough to flatten a 100-level gradient also blurs the part boundaries you need for segmentation, leaving a softer image that still fails a single global threshold.
A smoothing kernel is a local average, and it removes a gradient only where the kernel spans a large fraction of the gradient's length scale. Shading that changes across half the frame height needs a kernel hundreds of pixels wide; at that size the kernel averages part pixels with background pixels, drags a 120-level part edge halfway to the local mean, and spends the contrast the threshold still needs. Mild blur keeps the gradient and eats the edges; strong blur removes the gradient and the edges together. Division and subtraction behave differently: they rescale every pixel by its local illumination and preserve the part-to-background ratio, which is exactly what a threshold tests. Remove the illumination term; do not average it.
How does divide-by-reference shading correction flatten the frame?
Divide the live image pixel by pixel by a reference frame captured from the same scene without parts, multiply by a target level such as 128, and the illumination term cancels, so background and part land in the same grey-level interval everywhere.
The correction is two lines of arithmetic:
Compute the gain map once per scene, keep the division in floating point or 16-bit, and round to 8 bits only at the end. Sensor and camera pipelines run the same maths internally as flat-field correction for pixel uniformity; applied at scene level like this, it also removes shading from the light and the room.
Worked example, 8-bit camera, part darker than its background:
- Reference frame, line empty, lights and ambient exactly as they run: the background reads 200 at the top row and 100 at the bottom row.
- Gain map at target 128: top 128 / 200 = 0.64; bottom 128 / 100 = 1.28.
- Live frame with parts: background 200 top and 100 bottom; the part reflects 60 percent of its local background, so part pixels read 120 top and 60 bottom.
- Corrected: background 200 × 0.64 = 128 and 100 × 1.28 = 128; part 120 × 0.64 ≈ 77 and 60 × 1.28 ≈ 77.
| Region | Background raw | Part raw | Background corrected | Part corrected |
|---|---|---|---|---|
| Top of field | 200 | 120 | 128 | 77 |
| Bottom of field | 100 | 60 | 128 | 77 |
Before correction, no valid global threshold exists: the top window is 121-199, the bottom window is 61-99, and the two do not meet. After correction, every background sits at 128 and every part pixel at 77, so any threshold from 78 to 127 separates them, a 50-level window. A threshold of 100 leaves 23 levels of dark margin and 28 of bright margin. Division also equalises contrast, because it preserves the part-to-background ratio: 120/200 and 77/128 are both about 60 percent.
The reference carries the ambient contribution as it was at capture time, so ambient drift is the method's one weakness. The next section puts a number on it.
How do I capture a reference frame that keeps the correction valid?
Capture with the line running empty, the same optics, position, f-number and exposure locked by hand, an average of about 16 frames, and recapture whenever the background mean moves more than roughly 10 grey levels or 5 percent from the stored reference.
The habits that keep the gain map honest:
- Turn every automatic camera mode off first. Auto exposure and auto gain re-scale frames after the fact and silently invalidate a stored map; camera image brightness drifts over time: turn every auto mode off is that failure seen from the other side.
- Capture with parts out of view, same lens, same focus and f-number, same position, same exposure time.
- Average about 16 frames: temporal noise falls as 1/√N, so 16 frames cut random noise to roughly a quarter.
- Smooth the reference with a Gaussian of about 1 to 2 percent of the image width: LED structure and pixel noise leave, the low-frequency shading stays.
- Keep headroom: with a 128 target and a 100-level reference the gain is 1.28, so a 210-level live highlight lands at 210 × 1.28 ≈ 269 and clips at 255. Lower the target or shorten the exposure so live × gain stays inside 0-255.
- Recapture after any optical change, lens, filter, f-number, light unit or camera mount, and at shift start whenever the hall's ambient runs uncontrolled.
Mains-powered room light adds a second trap: with a free-running exposure, ambient brightness differs frame to frame and no stored reference matches every frame (image brightness changes frame to frame: fix mains lighting flicker). Set the exposure to a multiple of the ripple period, 10 ms on 50 Hz mains and 8.3 ms on 60 Hz, so every frame samples the same phase.
What if the part never leaves the field of view?
Estimate the background from the live image itself with a large-scale morphological opening or a blur whose kernel is several times the widest part feature, then divide or subtract that estimate; adaptive local thresholding is the last resort, not the first.
Three estimates, in the order to try them:
- Large-kernel background model: a morphological opening with a structuring element wider than the brightest feature, or a Gaussian with a sigma three to four times the widest part, yields a part-free shading image to divide or subtract. This is the dynamic-threshold method behind scratch defect detection with lighting and dynamic thresholding.
- Plane fit: a top-to-bottom ambient gradient is close to linear across most fields, so sample pixels you know are background, fit grey = a + bx + cy by least squares and subtract it. Two coefficients and a handful of samples, refitted every few hundred frames.
- Local adaptive thresholding: it computes a threshold per neighbourhood and tolerates any shading, but edges sit slightly off-true on a slope and grey levels stop comparing across the image. Dark region extraction: adaptive thresholding for uneven part shading and local mean adaptive thresholding for bright-object segmentation show the method from both sides.
Changing the segmentation method works, and occasionally it is the right call. It leaves the gradient in place, though: the part's measured grey still tracks the room, edge positions bias a little on the slope, and every grey-level feature, blob intensity, contrast score or defect threshold, drifts with it. Removing the gradient keeps those numbers comparable between shifts, seasons and halls.
Can I stop the gradient at the source instead of correcting it?
Yes — shroud the field against room light, move to a backlight so ambient never enters the measurement path, or strobe the light hard enough that its pulse delivers far more photons than the room delivers during the exposure, and the gradient never forms.
Shroud first. A black tunnel of sheet metal around the field, stiff cardboard in a pinch, deletes the ambient term outright and makes the reference frame permanently valid, because the only light left is your own.
Move to a backlight for silhouettes. When the feature is an edge, an outer diameter, a hole or plain presence, put the light behind the part; the collimated or diffuse backlight for edge gauging decision is then the only one left to make.
- Size
- 100-200 mm
- Type
- Standard
- Size
- 400 mm +
- Type
- High power / strobe
The LS-BKL-0750 covers a 180 x 100 mm lit area for compact fields; the LS-BKL-0865 is a high power unit with a 400 x 300 mm lit area and 92.2 W for larger fields and brighter rooms.
Use a diffused front light when the feature lives on the surface and genuinely needs front illumination, and even the field by mounting a diffused bar light close to the part, long axis parallel to the flow.
- Size
- 200-400 mm
- Type
- Diffused
- Voltage
- 24 V
The LS-BAR-1276 is a diffused white bar light with a 372 x 28 mm face.
Strobe over the top. Drive the light only during the exposure, pulsed well above its continuous rating, and the pulse delivers more photons than the room delivers across the whole frame; the margin becomes a measured number, and the procedure sits in strobed lighting ambient rejection: measure and improve it. Strobing also extends LED service life, as LED lifetime L70: why strobe mode makes lights last longer explains.
- Output
- Constant voltage 24 V
- Channels
- 4 channels
- Control
- Digital
- Feature
- Strobe
The LS-CTL-0070 is a 4-channel digital control box rated at 200 W with a strobe feature and an RS-232 port.
Whichever route you take, stability lives in the drive electronics: matched light controllers hold pulse energy constant as the LEDs age, and the light controllers finder filters by channels, control type and strobe feature.
How do I verify the correction before it goes on the line?
Measure the residual gradient: take the background mean in equal-size ROIs at the top and bottom of the corrected image, require the difference to sit within about 5 grey levels, and confirm one threshold holds across a full day of ambient change.
The checks, in order:
- Residual gradient: two fixed background ROIs, one in the top quarter and one in the bottom quarter; the ROI-and-reference procedure in measure light intensity in machine vision: ROI and reference method is exactly this test. In the example above the raw ROIs sit 200 - 100 = 100 levels apart; after correction they should agree within about 5 levels.
- Histogram: the corrected background forms one peak near 128; two peaks mean a stale reference or a flickering light.
- Threshold window: recompute it on corrected images from live ROIs instead of trusting the design numbers; if it narrows below your contrast requirement, with 20 levels a workable floor for 8-bit gauging, the light is aging, dirty or the reference is stale.
- Day test: capture at shift start, midday and end of shift, once with hall lights on and once off; the corrected background mean should stay within a few grey levels of 128 through all of it.
- Prevention: keep the shroud and the locked exposure, log the background mean every frame, and alarm and recapture when it drifts past 10 levels. Closed-loop lighting control for repeatable vision images extends the same idea to trimming drive current automatically.
If a gradient survives the shroud, the corrected reference and the strobe, the cell needs better optics rather than more code. Pick a shrouded backlight or a diffused bar light from the machine vision lights finder, or step through the lighting configurator with your camera and field size at hand. When the same cell fights ambient every shift, send two images, one corrected and one not, through the contact page; the fastest diagnosis starts from that pair.
Questions
Does divide-by-reference correction work with a colour camera?
Yes, but correct each channel separately: capture the reference with the same white balance, then divide red, green and blue by their own reference planes. A white light keeps the channels balanced, while a coloured ambient source shifts them, which is another reason to shroud. For gauging, a monochrome camera avoids the issue entirely.
How often should the reference frame be recaptured in production?
Recapture at every shift start while the hall's ambient runs uncontrolled, and otherwise when the logged background mean drifts past 10 grey levels or 5 percent. A shrouded cell with a locked exposure can go a week or a month between references; judge by the drift alarm, not the calendar alone.
Why does divide-by-reference do nothing on an 8-bit pipeline?
Integer rounding: a gain of 0.64 stored as an 8-bit integer becomes 1, so 200 × 1 = 200 and the correction disappears; rounded to 0 it blacks the frame out. Keep the gain and the ratio in floating point or 16-bit, multiply, then round the final image to 8 bits.
Can a polariser reduce the ambient contribution?
Sometimes. A polariser on the lens halves unpolarised ambient light, and crossed polarisers also strip glare from shiny surfaces. It costs one to two stops of sensitivity, so your own light must run brighter or the exposure longer. It trims the gradient term without removing it, so pairing it with a shroud or a strobe still wins.
Can I capture the reference from a white target instead of an empty line?
Yes, when stopping the line is impractical: place a matte white target at part height, capture the reference from it and treat it as the empty scene. The target must cover the whole field, sit at the working distance and reflect diffusely; glossy or curved samples corrupt the gain map. Recapture against the real background when you can, because surface finish shifts the grey ratio.



