Cameras & sensorsExplainer

Local mean adaptive thresholding for bright-object segmentation

Use local mean adaptive thresholding to segment bright regions, label connected components, and verify the largest candidate is your target.

LumaSpec Vision11 min read Share on LinkedIn

On this page 9 sections
  1. 01Decide whether local contrast matches your task
  2. 02Understand the local mean and offset
  3. 03Choose the window and offset by feature scale
  4. 04Build the segmentation and selection workflow
  5. 05Work through the arithmetic and largest-region trap
  6. 06Diagnose unstable masks before adding complexity
  7. 07Improve acquisition before buying more processing
    1. Camera and image integrity
    2. Directional lighting
    3. Lighting around the viewing axis
  8. 08Validate the decision, not just the overlay
  9. 09Questions

Bright-region segmentation often fails when one side of an image is brighter than the other. A single grayscale threshold then captures background in the bright area while missing the target in shadow. Local mean adaptive thresholding compares each pixel with its neighborhood instead of one image-wide cutoff. This workflow helps you extract candidates, connect neighboring pixels, and select a plausible object. The key qualification is that the largest bright component is not automatically the target.

Decide whether local contrast matches your task

Use this method when the target is brighter than its immediate surroundings and the background illumination changes gradually. It is particularly useful when the target's absolute grayscale value changes across the field but its local contrast remains reasonably consistent.

Do not confuse local brightness with object identity. A reflection, printed mark, or exposed background can have stronger local contrast than the intended part. Connected-component labeling separates regions; it does not determine which region represents a valid object.

Image condition Sensible starting method Main limitation
Uniform background and stable brightness Fixed grayscale threshold Sensitive to illumination drift
Gradually varying background with locally bright features Local mean comparison with a positive offset Window size changes which features survive
Isolated bright noise among useful features Local comparison followed by component filtering Noise can connect into larger regions
Broad, uniformly bright object Fixed threshold or background correction Local comparison may retain only its boundary
Glare brighter than the target Improve acquisition and add candidate checks Largest-area selection can favor glare

For narrow defects rather than filled bright objects, the crack segmentation guide addresses a different geometry. A setting that preserves thin cracks may be unsuitable for extracting an entire part silhouette.

Understand the local mean and offset

Let the grayscale image be P, with higher values representing brighter pixels. Calculate a local mean M over a neighborhood centered on each pixel. Mark a pixel as foreground when it exceeds that local mean by a positive offset.

M(x, y) = sum of neighborhood pixel values / neighborhood pixel count D(x, y) = P(x, y) - M(x, y) B(x, y) = 1 when D(x, y) > delta; otherwise 0

Here, B is the binary mask and delta is the required brightness difference. This article uses a strict greater-than comparison. Check your implementation because equality handling and offset sign conventions can differ.

The difference image D makes debugging easier. Positive values indicate pixels brighter than their neighborhood; negative values indicate darker pixels. Perform subtraction in a signed or floating-point representation. Unsigned subtraction can wrap or clip negative differences, producing misleading masks.

The neighborhood contains both the target and its surroundings. As a bright object occupies more of that neighborhood, it raises its own reference mean. A uniformly bright interior can therefore disappear even though its absolute intensity is high.

Choose the window and offset by feature scale

Window size determines what counts as local background. Start by measuring the width of the feature you need to preserve in pixels, not just its physical dimensions.

For an approximately uniform background, a neighborhood substantially wider than a narrow bright feature includes more surrounding background. That usually increases the feature's difference from the mean. A smaller neighborhood may follow the feature itself and reduce that difference.

However, a larger neighborhood also spans more illumination variation. Across a shadow boundary, it may generate foreground that belongs to the lighting pattern rather than the part. There is no universal window-to-object ratio that solves both problems.

A practical trial might compare square neighborhoods of 15 × 15, 31 × 31, and 61 × 61 pixels. These are test settings, not recommended defaults. Keep the images, offset, and candidate rules unchanged during this comparison.

Tune the offset separately. For an 8-bit image, you could test offsets of 6, 12, and 18 grayscale levels. Increasing the offset usually rejects weak contrast and noise, but it also removes low-contrast target pixels. Examine missed targets as carefully as unwanted detections.

Keep intensity scaling explicit. An offset of 12 on a 0–255 scale corresponds to approximately 12 / 255 = 0.047 on a normalized 0–1 scale. It does not mean the same thing as an offset of 12 in an image with a different numeric range.

If exposure or preprocessing changes brightness, inspect the raw image before retuning segmentation. The guide to brightening grayscale images without losing highlight detail explains why display appearance and retained image information are different concerns.

Build the segmentation and selection workflow

Keep intermediate images available so you can identify which stage creates an error. The following sequence separates brightness detection from object selection.

  1. Define a valid inspection region. Exclude fixtures and irrelevant background where possible. Record whether legitimate targets may touch its boundary.
  2. Prepare the grayscale image. If the source is color, use a consistent conversion. Confirm that grayscale conversion does not remove the contrast distinguishing the target.
  3. Calculate the neighborhood mean. Use the chosen window and a documented border-handling method. Padding choices can change detections near image edges.
  4. Create the binary mask. Compare the original grayscale image against the local mean plus the positive offset. Save both the difference image and the mask during commissioning.
  5. Apply only justified cleanup. Remove isolated speckles or bridge small gaps only when the operation preserves the smallest required feature.
  6. Label connected components. Choose the connectivity convention deliberately and calculate each component's area, bounding box, centroid, and boundary contact.
  7. Reject implausible candidates. Apply task-specific size, position, and shape checks before ranking the surviving components by area.
  8. Select or report no valid target. Keep the largest valid candidate only if that matches the inspection requirement. Define what happens when no candidate passes or several remain ambiguous.

With 4-connectivity, pixels connect through shared edges. With 8-connectivity, diagonal contact also joins them. A diagonal chain of noise can therefore merge separate regions under one convention but not the other.

Morphology also changes component identity. Closing can join nearby objects; opening can remove narrow target sections. Compare masks before and after cleanup rather than judging only the final overlay.

If your inspection software uses multiple image stages, verify that the mask and measurements reference the intended source. The image source binding guide covers mistakes that can resemble a thresholding failure.

Work through the arithmetic and largest-region trap

Consider an illustrative 8-bit image with uneven background brightness. Use a positive offset of 12 grayscale levels. The following values represent sampled pixels and their corresponding neighborhood means.

Sample location Pixel value P Local mean M Difference P − M Foreground?
Background in the darker area 70 75 −5 No
Target in the darker area 120 85 35 Yes
Background in the brighter area 140 145 −5 No
Target in the brighter area 190 155 35 Yes

Both target samples pass because 35 > 12. A fixed threshold of 150 would miss the darker target sample even though it has useful local contrast.

Now consider a uniform bright target with intensity 170 against background intensity 90. If the target occupies 80% of a particular neighborhood, the approximate mean is:

M = 0.80 × 170 + 0.20 × 90 = 154 D = 170 − 154 = 16 16 > 12, so the pixel passes

If the target occupies 90% of the neighborhood, the mean becomes 162. The difference falls to 170 − 162 = 8, so the pixel fails. This simplified calculation explains why a larger bright area can have a missing interior.

After labeling an illustrative mask, suppose the target component has an area of 1,840 pixels, a reflection covers 3,120 pixels, and a speck covers 23 pixels. Selecting the largest component returns the reflection.

Suppose validation establishes an acceptable target area of 1,500–2,300 pixels for this fixed imaging setup. That gate rejects both the reflection and the speck before ranking. These limits are application assumptions, not general thresholding settings. Derive them from representative parts, including acceptable variation.

Area scales with image resizing. If both image dimensions are reduced by half, an unchanged object's pixel area becomes approximately one quarter: 1,840 × 0.5 × 0.5 = 460 pixels. Reusing the original area limits would reject it.

Diagnose unstable masks before adding complexity

Start with the simplest explanation supported by the intermediate images.

  • Random speckles: Examine noise in uniform background regions. Test a larger offset or mild denoising, then check that weak target features remain visible.
  • A hollow target: Compare the difference image inside the object with its boundary. The neighborhood may be dominated by the target; a larger window or another segmentation approach may be needed.
  • Two parts becoming one region: Look for actual contact, diagonal connections, or a morphology operation bridging the gap. Increasing an area limit does not separate merged objects.
  • A bright strip at the image edge: Check mean-filter padding and whether the target is partly outside the inspection region. Do not remove boundary components if valid targets can cross that boundary.
  • Changing area with exposure: Determine whether pixels are clipping or local contrast is changing. Use controlled exposure during validation where the task permits it.
  • A large reflection winning selection: Add position and shape checks, but first determine whether changing the lighting can remove the reflection.

A median filter may suppress isolated impulse noise, but it can also remove small bright features. A mean filter applied before segmentation changes both noise and edge contrast. Treat either as an additional processing stage requiring validation.

When area is a meaningful acceptance measurement, avoid filling every enclosed hole automatically. A hole might be a segmentation artifact, a genuine feature, or a defect. Establish which interpretation the inspection requires.

Improve acquisition before buying more processing

Controlled images reduce the range of thresholds you need to support. Choose exposure, focus, viewing direction, and lighting before committing to a large parameter search.

Camera and image integrity

Use area scan cameras when the task requires a complete image of the inspection region. The camera finder helps narrow acquisition choices, but segmentation alone does not establish the necessary resolution or interface.

The LS-CMR-0166 is a USB3 rolling-shutter camera from the catalog. Evaluate motion behavior and image consistency for your actual capture conditions rather than treating a camera change as a segmentation fix.

Area scan cameras12 MP USB3 area scan camera, IMX226, 1/1.7" rolling shutter, 30 fps, 4000 × 3000 resolution (LS-CMR-0166)LS-CMR-0166
Sensor format
1/1.7"
Interface
USB3
Resolution
12 MP
Shutter
Rolling
$560.00View partBuild a system with it

Retain raw grayscale samples for algorithm comparisons. If your workflow uses higher-bit-depth images, follow the grayscale image saving guide so conversion or file export does not silently change the data range.

Directional lighting

Bar lights let you test illumination from different directions. Changing the direction can separate a useful bright surface response from unwanted reflections, although it may also introduce shadows.

The LS-BAR-1047 is a standard bar light listed in the catalog. Consider it as a directional-lighting candidate, with coverage and contrast established through application trials.

Bar lights290 x 30 mm LED bar light (LS-BAR-1047)LS-BAR-1047
Size
200-400 mm
Type
Standard
Voltage
24 V
$449.00View partBuild a system with it

Lighting around the viewing axis

Ring lights provide illumination around the camera's viewing axis. This geometry can reduce directional shadows, but shiny surfaces may still produce bright reflections that dominate the mask.

The LS-RNG-0143 is a diffused ring light in the catalog. Diffusion is a lighting characteristic, not proof that a particular reflective target will segment correctly.

Ring lights100 mm dia. ring light with diffuser, 30° (LS-RNG-0143)LS-RNG-0143
Size
100-200 mm
Type
Diffused
Angle
30°
Voltage
24 V
$169.00View partBuild a system with it

Compare geometries before selecting dimensions through the lighting finder. The lighting-angle guide explains why a surface feature and an outer silhouette often require different arrangements.

Validate the decision, not just the overlay

Use a held-out image set that was not used to choose the window, offset, or area gates. Include acceptable parts, defects, empty scenes, reflections, partial targets, and multiple objects when those conditions can occur.

Record missed targets, false selections, ambiguous scenes, and processing time separately. A visually attractive mask can still produce the wrong inspection decision.

Before ordering hardware or releasing the workflow, check:

  • Target definition: Is the required output a complete object, a bright marking, or merely a candidate location?
  • Scale: Do window dimensions and area gates still match the final image resolution and field of view?
  • Intensity range: Are grayscale conversion, exposure, numeric type, and offset units consistent?
  • Component rules: Are connectivity, morphology, boundary handling, and tie handling documented?
  • Identity checks: Can a reflection or fixture pass the same area and position limits as the target?
  • Failure behavior: Does the system report no valid target instead of forcing a largest-region answer?
  • Repeatability: Do saved test images produce the same decisions after restarting the application?

When broad interiors disappear or glare remains indistinguishable from the target, stop adding cleanup rules. Evaluate fixed thresholding under controlled illumination, background correction, or a segmentation method based on additional features.

For the next step, document representative images and failed masks, then use the lighting configurator to plan acquisition changes. For application-specific selection, send the inspection requirements through technical support, including the smallest feature, field of view, motion conditions, and examples of false selections.

Questions

What does local mean adaptive thresholding detect?

It compares each pixel with the average brightness of a surrounding neighborhood. A positive offset requires the pixel to be brighter than that average by a chosen amount. This can accommodate gradual background variation, but it detects local contrast rather than guaranteeing a complete object mask.

Why does a bright object appear hollow after thresholding?

A broad bright object raises the neighborhood mean toward its own intensity. Interior pixels then have little difference from that mean and can fail the comparison. Test a larger neighborhood, but also consider controlled illumination with fixed thresholding when a filled silhouette is the actual requirement.

Should I always keep the largest connected component?

Only when the inspection establishes that the intended target is the largest valid component. Reflections, touching parts, and exposed background can produce larger regions. Apply justified size, location, and shape checks first, and allow a no-target or ambiguous outcome rather than always returning something.

How should I choose the neighborhood size and offset?

Compare several windows against the smallest useful feature and the scale of illumination variation. Hold the window fixed while testing offsets in the image's actual intensity units. Select settings using separate validation images that include weak targets, noise, glare, and expected position changes.

When should I change the lighting instead of the algorithm?

Change acquisition when reflections, clipping, shadows, or motion prevent consistent target contrast. More segmentation stages cannot restore detail that was never captured. Compare lighting directions and diffusion with representative parts before deciding that a different camera or a more complex algorithm is necessary.