A dark printed band inside a wrapper should be the easiest feature on the belt to segment: it is the darkest thing on the part. Under real lighting it is not. Wrapper shading shifts the same band by more than 100 gray levels between one edge of the part and the other, so a single cutoff that finds the band on the shaded edge loses it on the lit edge. This recipe fixes the order of operations: find the outer contour, shrink it into an interior ROI, then run a two-pass threshold with a deterministic NG path. It suits packaging and label lines where the dark region is a print, a foil window or a filled cavity.
Why one global threshold fails on a shaded part
The gray value at each pixel is the part's reflectance multiplied by the light falling on that spot. Shading is smooth and low-frequency; the band's contrast is local. A global cutoff compares every pixel to one absolute level, so it can only work if the band sits on the same side of that level everywhere on the part.
Two normalizations fix this, and the recipe uses both in sequence. Histogram equalization stretches the contrast inside a bounded ROI so one absolute level works again. A local-mean threshold compares each pixel to its neighborhood average instead, so it follows the shading sweep automatically:
The same operator with the sign flipped drives local mean adaptive thresholding for bright-object segmentation; for dark targets the test is pixel below local mean minus C. If threshold vocabulary is new to you, edge detection without a vision library covers the basics.
What you need
- A mono camera with enough pixels — the worked example below sizes it. Mono reads one gray value per pixel, with no color interpolation between your image and your thresholds.
- Fixed lighting: a diffuse panel or dome, a polarizer for glossy wrappers, and exposure and gain locked to manual values. Auto modes re-balance every frame and quietly move your gray references — the mechanism behind auto exposure ROI instability.
- A strobe-capable controller when parts move, or whenever you want identical illumination geometry for every part.
- Four software operators: a binary threshold (spelled bin_threshold in some libraries), histogram equalization, a local or dynamic mean threshold, and region tools for erosion, domain reduction (spelled reduce_domain in some libraries) and feature checks on area, centroid and fill.
Most vision toolsets ship all four operators. The recipe itself takes an afternoon to build and one production shift to defend.
The recipe: outer contour, ROI, then two threshold passes
- Lock the acquisition. Manual exposure, manual gain, hardware trigger, one image per part. If the first frame arrives dark or half-lit, sort out trigger timing and light settling before touching any threshold — first camera frame dark walks through that diagnosis.
- Find the outer contour on the raw image, with no equalization. The part outline against the belt is the highest-contrast edge in the scene, so a plain binary threshold separates it reliably. Keep the largest region and fill its holes. If this step is unstable, belt contrast or part presentation is the problem — fix it here, because every later step inherits this region.
- Shrink the outer region into an interior ROI. Erode the contour far enough to clear the wrapper's own dark edge: 2–5% of the part width, which is 2–4 mm on the 78 mm part in the worked example, or 20–40 px at 0.1 mm/px. Bind the reduced domain to its image with a domain-reduction operator (spelled reduce_domain in some libraries). In multi-camera cells, keep each ROI bound to the frame it came from — that failure mode is covered in ROI image source binding.
- Pass 1: equalize, then threshold. Run histogram equalization inside the ROI, then a binary threshold. Equalization re-spreads the gray levels so the band's local contrast survives the shading sweep, and one fixed level finds it across the whole part. Keep the candidate regions whose area sits inside your bounds. On a healthy line this pass catches most parts by itself.
- Pass 2: local-mean threshold on the same ROI. For the parts pass 1 misses, apply the formula above inside the ROI, with the window sized to twice the band width: a 0.4 mm band at 0.1 mm/px is 4 px, so w = 9 px. Start C at 3 gray levels for a dark target and move in steps of 1–2. Because the comparison follows the neighborhood, the band separates at gray 20 and at gray 140 with the same settings. The identical operator carries scratch defect detection with lighting and dynamic thresholding — different feature, same mechanics.
- Validate the inner region before trusting it. Three gates, each measured once on a golden part and kept as bounds: area within about ±20% of the golden value, centroid within ±2 mm of its expected position, and fill ratio of at least 0.8 against a fitted shape. A region that fails any gate counts as not found.
- Two strikes, then NG. If neither pass produces a valid region, flag NG and send the part to the reject lane. Do not loop retries: two passes bound the cycle time. Log which pass won for every part.
Worked example: pixels, window size and exposure budget
The part is a 78 × 52 mm pouch carrying a 0.4 mm dark print band; the band width sets the resolution demand. Reserve 5 mm of handling margin per side, so the field of view is 88 × 62 mm. Require 4 px across the band so the edge transition survives lens softness:
That is 880 × 620 ≈ 0.55 MP. Doubling it to pay for corner falloff and ROI jitter sets a floor near 1.1 MP: a 1.6 MP sensor clears it with margin, and 5–6 MP leaves headroom for a date-code check on the same image. The erosion margin from step 3 falls out of the same numbers — 2–4 mm of the 78 mm part width is 20–40 px at 0.1 mm/px.
Exposure closes the budget. If the line grabs on the fly at 100 mm/s and you tolerate 1 px of blur, exposure ≤ pixel size ÷ belt speed = 0.1 mm ÷ 100 mm/s = 1 ms. Fire a matching 1 ms strobe pulse and size it against the duty rules in LED strobe duty cycle: matching lights and controllers. At 1.6 MP the whole chain — contour, erosion, equalization, two thresholds — runs in tens of milliseconds on a smart camera, so a 2-second cycle budget goes to part presentation and communication, not to thresholding.
Cameras that fit the arithmetic
Decision first: run this on-device when the task is this one check and the line wants pass/fail over digital I/O. Move to a PC plus an area scan camera when several inspections share one image or you need operators a smart camera's toolset does not ship. Both routes need the same 1.1 MP floor from the math above. The smart cameras category covers on-device sensors, area scan cameras covers the PC route, and the camera finder filters both by resolution, shutter and interface.
Three cameras that span the floor-to-headroom range:
- Interface
- GigE
- Resolution
- Up to 3 MP
- Interface
- Fast Ethernet
- Resolution
- 5 MP
- Interface
- GigE
- Resolution
- 6-10 MP
The LS-SMC-0063 sits at the arithmetic's minimum: 1.6 MP, a fixed 16 mm lens and 60 fps over GigE, with an integrated white light that includes polarization for glossy wrappers. The MV-SC3050XC-16M-ATN-H moves up to 5 MP color on a 1/1.45" global-shutter sensor with an M12 mount over Fast Ethernet — the pick when a second inspection rides on the same image. The MV-SC5060XM-08M-WBN is the mono route: 6 MP over GigE, an 8 mm M12-mount lens and a rolling shutter. A 1 ms strobe freezes motion, which makes that rolling shutter harmless on a moving line. Mono pixels carry one gray value each with no demosaicing, so local means are computed on cleaner numbers. For a PC build, start at the configurator's camera step and size a C-mount lens from the field of view above.
Light and strobe: remove the shading you can
Normalization is the second line of defense; even light is the first. A diffuse panel or dome light removes the directional gradient that causes the sweep, and a polarizer kills the specular gloss that makes it worse on shiny wrappers. Hold the light constant with a light controller on constant-current drive or per-part strobe, so illumination geometry is identical in every image. Closed loop lighting control for repeatable vision images shows how to trim LED aging automatically, and the same lighting-first order is worked through for transparent packaging in glass bottle defect detection.
A strobe-capable digital unit keeps every pulse identical — four channels, 200 W, RS232 control and 24 V constant-voltage output:
- Output
- Constant voltage 24 V
- Channels
- 4 channels
- Control
- Digital
- Feature
- Strobe
Drive the panel with the 1 ms pulse from the exposure budget and every part sees the same light. The light controller finder filters units by channels, strobe support and control type.
Verify before release, and keep verifying
Run the release check the same way every time:
- 30 known-good parts spread across the belt — left, right, tilted, near, far. Target: 0 NG. If good parts NG at more than 2%, widen the step-3 margin or fix the light instead of adding a third pass.
- 20 known-bad parts — band missing, shifted or faint. Target: 100% NG catch; a silent pass-through costs more than any hour of tuning.
- A gray-level profile along a line across one part at every belt position: the band must satisfy the local-mean test everywhere on the sweep.
- Corner brightness within about 25% of center — a rule of thumb, because cosine falloff and lens vignetting eat your pixel margin at the field edge first.
- After every lamp change or belt-speed change, rerun the 30-part set: LED output decays with hours, and the profile shows the drift before NG counts rise.
Then force one NG and time the reject signal end to end. If nothing fails loudly, the NG path is still theoretical.
Where to stop: the NG and hardware decisions
A line like this never reaches 100% first-pass detection, and it should not try: the design goal is a deterministic NG path, not a perfect threshold. Two passes, then the reject lane. Count NG causes by pass and by gate, and the shift report tells you whether light, presentation or the band itself moved.
- Single check, stationary part, 1.1 MP floor: a 1.6 MP smart camera running the two-pass recipe, NG bin beside the line. Done — stop tuning.
- Second inspection on the same image: a 5–6 MP sensor; stay on-device while the full chain stays under a few hundred milliseconds, otherwise move to a PC and an area scan camera.
- Good-part NG above 2–3% after both passes: the light is wrong. Diffuse, polarize, strobe — no third threshold pass can fix a directional gradient.
- Glossy foil wrapper: polarizer first, algorithm second; with specular glare gone, expect the equalization pass to do most of the work.
More recipes in the same order — light, geometry, then thresholding — sit on the applications topic page, and dark contaminant detection on dark surfaces is the closest cousin to this task. If the arithmetic above names your camera class, contact us with the part dimensions and cycle time and we will size the rest of the chain with you.
Questions
Why does a global threshold lose the dark band on one side of the part?
Wrapper shading is a smooth illumination gradient: the same band reads gray 20 on the shaded edge and gray 140 on the lit edge of one part. An absolute cutoff at 100 finds one edge and misses the other; a cutoff that catches both floods the shaded wrapper. Run a gray-level profile along a line across the part first: if the band's level crosses your cutoff somewhere on the sweep, shading is the cause and the fix is normalization, not a new cutoff.
How do I size the local-mean threshold window and the offset C?
Set the window to twice the band width in pixels, plus one, and make it odd: a 0.4 mm band at 0.1 mm per pixel is 4 px, so w = 9 px. Start the offset C at 3 gray levels for dark targets and move in steps of 1–2. Too small a window starts tracking the band itself; too large a window re-introduces the shading gradient you were trying to remove.
Can this recipe run inside a 2-second cycle on a smart camera?
Yes. On-device, the full chain — outer contour, domain reduction, equalization and two threshold passes at 1.6–5 MP — costs tens of milliseconds, not seconds. A 2-second budget is usually spent on part presentation, transport stop and communication. Pick a sensor with a fast frame rate so a corrupted first grab can be re-taken within the cycle instead of flagging a false NG.
When should I fix the lighting instead of tuning thresholds?
First, always: a glossy wrapper under a directional light creates the shading you are fighting. Move to a diffuse panel or a dome, add a polarizer for specular gloss, lock exposure and gain to manual, and strobe. If good-part NG counts drop below about 2% with one simple threshold, the optics fixed it; keep the two-pass recipe as insurance for lamp aging and part wander.
What counts as a valid inner region before I accept the part?
Three gates, all measured on a golden part and kept as bounds: area inside the region between A_min and A_max set at roughly ±20% of the golden value, centroid within about ±2 mm of its expected position, and a fill ratio of at least 0.8 against a fitted shape. A region that fails any gate counts as not found, so the part goes to the two-pass NG path honestly.



