Scratches are among the hardest defects to detect reliably, and the failure is almost never in the algorithm. A scratch that reflects light away from the camera is a few grey levels deep at best, and no threshold can separate a 3-level feature from a noisy background. This guide is written for anyone building their first scratch inspection on a part that also carries printed graphics, such as a logo, where scratches that touch or cross the print are the ones that keep failing. You will learn how to light the part so the scratch itself has real contrast, how to size that lighting to your field of view, and how a local dynamic threshold flattens the background so scratches stay separable even where they cross dark print.
Get the light right before touching the threshold
A scratch is a change in surface slope. Whether you can see it depends entirely on where the light sends that slope's reflection. Under a large diffuse source, a shallow scratch on a matte part changes the surface brightness by very little, so the image shows a nearly uniform grey field with a faint line that any threshold will lose.
Under a low-angle directional light, the same scratch behaves differently. The flat surface reflects the light away from the camera and stays dark; the scratch wall catches it and appears bright. This is dark field imaging: the defect is brighter than everything else, often by 50 grey levels or more, and the segmentation becomes almost trivial. The dark field versus bright field trade-off, and when a diffuse dome wins instead, is covered in detail in that guide on domes for shiny and curved parts.
The rule of thumb: flat or gently curved matte part, use a low-angle bar light in dark field. Shiny or strongly curved part, use a dome light, which wraps the part in even diffuse light and removes the hot spots that a directional light would print onto a curved surface.
Choosing between a bar light and a dome
For scratch work on a flat part, a bar light mounted at a shallow angle, typically 15 to 30 degrees from the surface, is the cheapest way to get strong contrast. A diffused bar softens the illumination and helps when the surface has brushed or textured finish, where a hard line of light would create its own false streaks.
- Size
- 200-400 mm
- Type
- Standard
- Voltage
- 24 V
For example, a 290 x 30 mm bar light covers a field of view of roughly that order at a shallow working distance, and its 24 V input lets it run from a standard controller. Mount two bars on opposite sides if scratches run in unknown directions, because a single dark-field bar lights scratch walls that face it and misses those facing away.
When the part is domed, bottle-shaped or polished, a directional light produces specular streaks that look exactly like scratches. A dome fixes this: its inner surface acts as an extended diffuse source, so slope changes remain visible but hot spots vanish. A compact example is the 116 mm emitting-area dome, which suits fields of view up to roughly 100 mm.
- Size
- 100-200 mm
- Voltage
- 24 V
If you are unsure which way your part will go, the lights shop finder lets you filter both families by size and colour, and the LED colour contrast guide explains why a red or blue wavelength can add contrast on coloured print.
Size the light and the field of view together
Work the geometry out before ordering anything. Suppose your part is 60 mm wide and the camera sensor is 6.4 mm wide with a 12 mm lens. The magnification is:
At 100 mm working distance that calls for a lens near 9 mm, so check the lens focal length calculator for your own sensor and part dimensions.
The lighting must cover the field of view at its shallow mounting angle, not just at the perpendicular distance. A bar mounted at 20 degrees and 100 mm away sits roughly 34 mm to the side of the part centre, so its lit length must reach across the whole part from that offset. Add at least a 20 percent margin on every dimension; a light that exactly matches the field of view will show edge falloff in the image corners.
Flatten the background with a dynamic threshold
Once the scratch is bright, segment it with a local-mean threshold rather than a global one. The idea: blur the image with a mean filter to get a slowly varying background estimate, then keep pixels that exceed that background by a fixed offset.
The kernel size k should be wider than the scratch and narrower than the printed features. If your scratch is 4 pixels wide and the logo stroke is 40 pixels wide, start with k around 15 to 25 and an offset c of 5 to 15 grey levels. Because the background estimate follows the local brightness, the dark print stops being a problem: inside the logo, the local mean is also dark, so a scratch crossing it still exceeds the mean by the offset and survives in the mask.
A global threshold cannot do this. One value for the whole image means one compromise between losing faint scratches and keeping the print, and you will keep moving it without ever winning.
Scratches that touch or cross the printed logo
This is the case that defeats most first attempts. Three steps solve it:
- Find the logo with shape-based matching so you have its region, but do not exclude it from the image first. Excluding it before thresholding also removes the scratch where it crosses.
- Apply the dynamic threshold to the whole field of view. The scratch survives across the print, as explained above, and appears as one connected streak.
- Intersect the result with your inspection region. If the logo itself is not a defect, subtract the matched logo region from the final mask, but only after detection. If a scratch must be detected even where it crosses the print, keep the whole region and instead filter candidates by shape: scratches are long and thin, so keep regions whose major axis is at least five times the minor axis.
The shape filter also removes the print's own edges and text speckle. A dot from dust is compact and gets rejected; a 60-pixel-long, 3-pixel-wide streak passes. If matching the logo is itself unstable because glare hides it, the bottle recognition lighting guide shows how to reduce specular glare so printed features match reliably.
Strobe and power settings for line or robot use
On a moving line, strobe the light during the exposure and overdrive it briefly. A brighter pulse raises the scratch signal above sensor noise, and a short pulse freezes motion blur. Keep the duty cycle within the light's rating; overdrive and timing limits are set out in the strobe overdrive and duty cycle guide.
- Output
- Constant voltage 24 V
- Channels
- 4 channels
- Control
- Digital
A four-channel digital controller with 24 V constant-voltage output can run two dark-field bars plus a coaxial or dome channel from one unit, and its 8 A total output budget covers typical strobed bars. Check each light's current draw against the per-channel limit before wiring, and set the exposure so the brightest good part just below saturation; that leaves the full grey-level range for the scratch signal. The light controllers category lists the output types side by side if your lights need constant current instead.
Verify on rejected parts, not good ones
Build the golden sample from parts you know fail. Collect scratches of every severity, including faint ones a human barely sees, and measure the actual contrast: sample the grey level across the scratch and across neighbouring background. If the peak minus background is under about 10 grey levels, go back to the lighting before adjusting any threshold.
Run the full pipeline on at least 20 known-bad and 50 known-good parts. Track two numbers: the faintest scratch detected and the false-call rate on good parts. Tune k and c against those numbers, and record them. A live binary image preview during tuning shows immediately whether a lost scratch is a lighting problem (nothing in the raw image) or a segmentation problem (visible in raw, gone in the mask).
When the usual answer is wrong
Two situations break the pattern above. First, scratches on transparent or glossy curved packaging: dark field gives you reflections of everything in the room, and a dome plus crossed polarisation or a wavelength outside the visible band works better. Second, scratches that are actually material folds or chips with depth: these need a geometric check, and the 360 degree inspection lens guide covers imaging inside bores and walls where scratches hide from a top-down camera. If your defects are cracks in a matte food product rather than polished metal, the biscuit crack detection guide shows how to turn a binary mask into a keep or reject decision.
Ordering checklist
- Field of view and part size measured, with a 20 percent lighting margin
- Scratch orientation range known, or two opposing lights planned
- Lighting type chosen: low-angle bars for flat matte parts, a dome for shiny or curved ones
- Current draw of every light summed and checked against the controller's per-channel and total limits
- Strobe timing planned against line speed and exposure
- Known-bad samples collected before tuning any threshold
If you would rather have the whole chain matched for you, start with the lighting step of the system configurator and work outwards to the lens and camera.
Next step
Pick the lighting family that matches your surface first, then tune the dynamic threshold on known-bad parts. If you are unsure whether a dome or a bar suits your part, send a photo and a description of the surface through the contact page and we will suggest a starting point.
Questions
Why do my scratches disappear after thresholding?
Usually the scratch contrast is below a few grey levels, so any global threshold either keeps the whole surface or loses the scratch. Fix the lighting first: a low-angle directional light turns a shallow scratch into a bright streak against a dark field. Only then apply a local dynamic threshold, which adapts to shading across the part.
How do I detect a scratch that crosses a printed logo?
Do not try to remove the logo before finding the scratch. A dynamic threshold compares each pixel with the local mean, so the logo becomes its own uniform region and the scratch still stands out where it crosses it. You can then mask the logo region out of the result if the logo itself is not a defect.
Dark field or dome light for scratch inspection?
On a flat or gently curved matte part, a low-angle bar light set up for dark field gives the strongest scratch contrast. On a shiny or strongly curved part, a dome light surrounds the part with diffuse light and avoids the hot spots a directional light would create. Test both if the part is partly flat and partly curved.
What filter kernel size should a dynamic threshold use?
The mean filter should be wider than the scratch but narrower than the spacing between printed features. Start with a kernel about three to five times the scratch width in pixels and a grey-level offset of 5 to 15. Increase the offset if noise survives, decrease it if faint scratches drop out, and judge on rejected parts, not on good ones.


