System designHow-to

Remove glare from saturated pixels in defect detection

Mask saturated pixels at 90% of full scale to remove glare from defect detection, dilate, exclude or inpaint, then fix the light with a dome or polarizer.

LumaSpec Vision11 min read Share on LinkedIn

On this page 10 sections
  1. 01What saturation costs you
  2. 02Choose the approach before you edit pixels
  3. 03Build the saturation mask
  4. 04Exclude or inpaint each masked region
  5. 05Worked example: waxed apple, 5 MP camera, 80 mm field
  6. 06Relight: stop the highlight before it forms
  7. 07Verify it worked
  8. 08Common mistakes
  9. 09Order the pipeline and pick the hardware
  10. 10Questions

A white glare spot is not noise you can filter away. The pixels inside it are saturated, clipped at the sensor's full scale, and they carry no information about the surface beneath them — so a Gabor or texture filter reads the rim of the spot as a defect. You can still save the image: threshold the saturated pixels, dilate the mask over the halo, then either exclude those regions from the defect score or inpaint them over known-good texture. The lasting fix is optical: diffuse light that never lets the highlight form. This guide gives you both paths with the arithmetic, for surface-defect inspection on shiny or curved parts such as waxed fruit, caps and fittings.

What saturation costs you

Every photosite holds a finite charge before it clips: 255 in an 8-bit image, 4095 in 12-bit. Three consequences follow. The clipped area has zero contrast, so gradient and texture detectors read its edge as an anomaly. The halo around the spot, from bloom and lens flare, is degraded even where it is not clipped. And no filter recovers what the clip destroyed — the only decision left is what to do with the hole.

The spot itself is geometric, not random. A curved, shiny surface throws one mirror direction at the lens, which is why bright field versus dark field lighting decides where highlights land. Map the geometry, predict the spot, and score the rest of the surface around it.

The same saturation corrupts fringe-based 3D measurement: an intense specular highlight saturates pixels and degrades the captured fringe quality, so run the mask on the raw frames before you compute depth.

Choose the approach before you edit pixels

Approach Use when Risk to manage
Exclude masked regions from scoring Any glare position; you must not pass a hidden defect The masked region goes unverified — log it and re-image
Inpaint the masked region The ring around the glare is uniform, defect-free texture A defect under the glare is erased, not seen
Relight: dome, polarizer, strobe You control the station Costs exposure headroom; needs the light hardware

Inpainting is established practice in surface-defect research, reported both for rebuilding glare-damaged regions and for synthesizing clean training images, and multi-scale inpainting detectors treat the filled patches as background. That is exactly the assumption you must check on your own parts before trusting a fill.

Build the saturation mask

  1. Compute the mask per colour channel, then merge. Take the maximum of red, green and blue: white light on pale surfaces usually clips green first because a Bayer sensor devotes half its photosites to green, but a spot's rim can clip in red or blue alone. A luminance-only mask misses those.
  2. Threshold at 90% of full scale. In 8-bit: 0.90 × 255 = 229.5, so mask every pixel ≥ 230. In 12-bit: 0.90 × 4095 = 3685.5, so mask ≥ 3686. The remaining 10% is headroom on the flat top of the sensor response, where scene changes stop changing the image. If the 90% cut starts eating true texture — pale peel reading 235 and up — raise the cut toward 95% (≥ 243 in 8-bit) and widen the dilation instead.
  3. Read the histogram before trusting the cut. Glare is a small, isolated pile at 250–255; a broad pile above 230 across the frame is overexposure, not glare. Cut exposure or gain first — the response is linear below clipping, so halving exposure halves values: a surface at 240 with a 20 ms exposure reads 240 × (10 ms ÷ 20 ms) = 120 at 10 ms.
  4. Clean the mask, then dilate. Drop 1–2 pixel components as sensor noise, keep the rest, then dilate by 15–25 pixels with a disk-shaped element — the 20-pixel baseline of the classic saturation-mask-and-inpaint recipe — so the mask covers the halo, not just the clipped core. Measure the halo once, zoomed, on your worst part.
  5. Keep the mask. Store it beside the image; exclusion, inpainting and re-image decisions all read the same mask.

A broad, smooth brightness gradient across the frame is shading, not glare — correct it with shading correction rather than masking, as in Uneven brightness in threshold segmentation: shading correction fix.

Exclude or inpaint each masked region

Exclusion is the safe default. Hand the mask to the detector, zero the defect score inside it, and log the masked area per part. A glare mask that grows or wanders between frames of the same part means the part is sitting differently or the illumination drifted — not that the surface changed. The same mask doubles as a validity mask when you train a learned detector: the validity-mask method for excluding ambiguous regions from detection training uses the identical pattern.

Inpaint only where the ring around the mask is uniform, defect-free texture. Texture-synthesis and Poisson-blending fills work there because the surface genuinely continues under the highlight. On a curved, shiny part a defect can sit under the glare, so route those parts to a re-image at another angle instead of filling them.

Clipping is not a fix. Setting every pixel above the cut equal to the cut flattens the spot into a plateau whose rim still differs from local texture, so the filter fires exactly as before. The pixels were unusable; now they are unusable and disguised.

Worked example: waxed apple, 5 MP camera, 80 mm field

Inputs: an 8-bit colour image, full scale 255; the saturation cut at 90% of full scale, so pixels ≥ 230 are masked; the 20-pixel dilation baseline; and the case optics — a 5 MP sensor, 2592 × 1944 pixels, over an 80 mm field across the 1944-pixel height, a typical set-up for a 70 mm apple with positioning margin. The optics are this example's inputs; substitute your own field of view and pixel count.

Rule in words: millimetres per pixel equals field of view divided by pixels across that field, and the hidden band around each spot equals twice the dilation radius times that scale.

mm_per_px = FOV / px_across_FOV hidden_band = 2 × dilation_px × mm_per_px

Arithmetic:

  • Scale: 80 mm ÷ 1944 px = 0.0412 mm/px.
  • Hidden radius: 20 px × 0.0412 mm/px = 0.82 mm.
  • Hidden band: 2 × 0.82 mm = 1.65 mm across.

Decision: any defect smaller than about 1.6 mm that sits within 0.8 mm of a highlight is erased along with it. If the smallest defect you must report on this apple is 0.5 mm, a 20-pixel dilation is too coarse. Either measure the halo, confirm it is narrower, and dilate 10 px (10 × 0.0412 mm/px = 0.41 mm) — or stop editing and relight with a dome. If the mask covers roughly 1% of the frame or more, the cause is overexposure rather than glare, so cut exposure first.

The pixels-per-defect sizing method applies the same scale arithmetic when you choose the camera. The calculator below turns your own field of view and pixel count into the millimetres-per-pixel figure:

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

Relight: stop the highlight before it forms

A saturation mask is a patch. Taking the mirror path away is the cure.

Diffuse dome for curved parts. A dome light illuminates the part from a wide cone of angles at once, so no single reflection direction reaches the lens as a spot and the highlight spreads into an even sheen. Choose an emitting area wider than the part's widest view, and expect the camera hole to leave a small dark patch — a known artifact with known fixes, described in Dark spot from the camera hole in dome lights: how to fix it. The full selection logic, dome size versus part size included, is in Dome lights for shiny, curved and wrinkled parts.

How a dome light works LEDs at the rim light the white inner surface of the dome, which lights the part evenly from almost every direction. The camera looks through a hole at the top; on flat mirror-like areas the hole reflects as a dark spot.Cameradiffuse inner surfacelights the part from every sideLED ringcamera holecurved or wrinkled part; dark spot where the hole reflects
How a dome light works. LEDs at the rim light the white inner surface of the dome, which lights the part evenly from almost every direction. The camera looks through a hole at the top; on flat mirror-like areas the hole reflects as a dark spot.

A dome with a Ø116 mm emitting area covers that field with room for the halo:

Dome lightsØ166 mm dome light, Ø116 mm emitting area (LS-DOM-0085)LS-DOM-0085
Size
100-200 mm
Voltage
24 V
from $409.00View partBuild a system with it

Its Ø166 mm outer diameter keeps the station compact, and it runs from 24 V.

Crossed polarizer when geometry is fixed. A polarizer on the light set 90° to a polarizer on the lens blocks the polarization-preserving mirror reflection and passes the depolarized diffuse component. Budget two to four stops of light for the pair: longer exposure, wider aperture or more strobe power. The alignment procedure is in Cross polarizer on lens and light to kill specular glare. If you want the polarization built in at the camera, the LS-SMC-0063 pairs a 1.6 MP sensor with a 16 mm lens and polarized white light:

Smart cameras & vision sensors1.6 MP smart camera, 60 fps, 16 mm lens, white light (polarized, transparent light) (LS-SMC-0063)LS-SMC-0063
Interface
GigE
Resolution
Up to 3 MP
$1,110.00View part

Flat, mirror-like parts need different geometry. On a flat specular surface a dome still bounces straight back, so use low-angle dark field or coaxial diffuse. Ring light vs bar light: choosing by part geometry and glare maps part geometry to light shape, and the coaxial-diffuse set-up for a part inside a metal can covers the deep, fully shiny case.

Strobe to buy the exposure back. Domes and polarizers both cost light; a strobe pulse restores a short, bright exposure and freezes part motion. Fire the pulse inside the exposure window — Strobe light on but camera image is black: fix the timing shows the alignment. A light controller with a strobe output drives it; the LS-CTL-0070 switches 4 channels at a 200 W rating and is configured over RS232:

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

Verify it worked

  • Histogram: after relighting, the pile at 250–255 is gone and the brightest true surface pixels sit below the 230 cut with headroom.
  • Golden parts: image parts carrying seeded defects of your smallest size with the mask pipeline running. Every seeded defect must still score. One that passes under a former highlight means the mask is hiding it — flag those regions for a re-image instead of a fill.
  • Mask stability: on a repeated part the masked area stays near constant, frame to frame. Drift usually means auto exposure or auto gain is still steering brightness — turn every automatic mode off and lock exposure and gain, as in Camera image brightness drifts over time: turn every auto mode off.

Common mistakes

  • Masking from luminance or one channel only. A single-channel rim survives the mask and still fires the filter.
  • Clipping instead of masking. The plateau keeps firing texture filters; nothing was removed.
  • Inpainting where a defect can hide. The fill erases the evidence; exclude those regions and re-image.
  • Dilating wider than your smallest defect. At 0.0412 mm/px a 20-pixel radius hides a 1.65 mm band, and smaller defects vanish with the glare.
  • Leaving auto exposure or auto gain on. The mask drifts with the brightness, and so does the detection result.

Order the pipeline and pick the hardware

Run it in this order: mask, exclude or flag, then inpaint only where the ring is clean texture — and fix the light so the mask shrinks toward nothing. If you are building the station rather than rescuing images, start at the light: the dome lights category filters by size and wavelength, the light finder narrows by mount and voltage, and the configurator walks camera, lens, light and controller as one chain. The same mask logic runs on-device on modern smart cameras, so exclusion works without a PC. Send a photo of the glare on your part through the contact page and we will propose the lighting geometry before you spend an afternoon tuning masks.

Questions

What pixel value counts as saturated glare?

Mask from 90% of full scale upward: in an 8-bit image that is every pixel at or above 230 (0.90 × 255 = 229.5); in 12-bit, at or above 3686. Confirm the cut on the histogram. Glare appears as a small, isolated pile at 250–255; a broad pile above 230 means overexposure, so halve exposure time to halve pixel values (240 at 20 ms becomes 120 at 10 ms) instead of masking.

How far should I dilate the glare mask?

Start at 20 pixels and adjust within 15–25 after checking the halo once, zoomed, on your worst part. Convert to millimetres with your own scale: at 0.0412 mm per pixel, a 20-pixel dilation hides a band about 1.65 mm across around each spot. If your smallest reportable defect is smaller than that, dilate less or fix the lighting instead.

Why not clip saturated values to the threshold instead?

Clipping flattens the spot into a constant plateau. The plateau's rim still breaks the local texture, so Gabor and similar filters fire there exactly as before, and the pixels hold no surface information either way. Mask the region and exclude it from scoring or inpaint it — or change the lighting so the highlight never saturates the sensor.

When is inpainting glare safe?

When the ring around the mask is uniform, defect-free texture, so the fill genuinely continues the surface. Surface-defect research uses inpainting both to rebuild glare-damaged regions and to synthesize clean training images. If a defect could sit under the highlight, do not inpaint: exclude the region from scoring and re-image the part from another angle.

Which channel should the saturation mask come from?

Take the maximum across red, green and blue. On white light over pale surfaces green usually clips first, because a Bayer sensor devotes half its photosites to green, but the rim of a spot can clip in red or blue alone. On a mono camera the single channel is the mask; merge nothing.

What lighting removes glare on a curved, shiny part?

A diffuse dome light, sized so its emitting area is wider than the part's widest view: it lights from many angles, so no single mirror direction reaches the lens as a hot spot. Flat mirror finishes need low-angle dark field or coaxial diffuse instead, and a crossed polarizer helps where the geometry cannot change. Budget two to four stops of light for the polarizer pair.