System designHow-to

Locate image defects with pixel coordinates and calibration

Locate image defects with pixel coordinates, extract connected-region centroids, and validate calibrated positions without confusing pixels and millimeters.

LumaSpec Vision10 min read Share on LinkedIn

On this page 11 sections
  1. 01Define which coordinates you need
  2. 02Prepare the acquisition and coordinate records
  3. 03Align the images before subtracting
  4. 04Build a two-dimensional defect mask
  5. 05Label regions and extract their centroids
  6. 06Preserve coordinates through crops and warps
  7. 07Calibrate pixel positions into physical coordinates
  8. 08Verify position and detection separately
  9. 09Checks before ordering or deploying
  10. 10When this workflow is not the right answer
  11. 11Questions

A difference mask identifies changed pixels, but it does not automatically provide a useful defect position. You must group those pixels, choose a representative location, and define the coordinate system. This workflow is for inspection developers comparing a captured image against a reference. You will extract defect centroids, retain their relationship to the original image, and convert them into calibrated positions on a flat inspection plane.

Define which coordinates you need

Choose the output before writing the measurement code. A defect can have several useful locations, and they are not interchangeable.

Output Meaning Typical use
Pixel centroid Average position of the pixels belonging to one region Overlay markers and associate detections
Bounding box Rectangle enclosing the region Save a defect crop or define a review window
Selected boundary point A specified point on the region outline Locate a tip, edge, or nearest clearance
Calibrated plane position Image location mapped into physical coordinates Measure position on a fixed, flat surface

A centroid is a representative point, not necessarily a defective material point. For a crescent-shaped region, it can lie outside the region itself. Keep the mask or boundary alongside the centroid when shape matters.

If your goal is dimensional inspection rather than an image overlay, use the calibration principles in measuring planar distances with a calibrated camera. A pixel coordinate alone has no millimeter value.

Prepare the acquisition and coordinate records

You need a reference image, an inspection image, a repeatable acquisition setup, and a calibration target when physical coordinates are required. Record image dimensions, pixel format, crop offsets, scaling, exposure settings, and calibration identity.

Choose area scan cameras around sampling, shutter behavior, and acquisition requirements. Use the camera finder to narrow the hardware. A rolling-shutter camera can be considered for stationary captures, but moving parts require a separate assessment of distortion and motion blur.

A camera candidate for evaluation is LS-CMR-0053.

Area scan cameras12 MP GigE area scan camera, IMX226, 1/1.7" rolling shutter, 9 fps (LS-CMR-0053)LS-CMR-0053
Sensor format
1/1.7"
Lens mount
C-mount
Interface
GigE
Resolution
12 MP
$560.00View partBuild a system with it

Select machine vision lenses for sensor coverage, mount compatibility, working distance, and the required field of view. The lens finder helps narrow those choices. A lens candidate is LS-FFL-0567; verify its suitability for your actual geometry before ordering.

Machine vision lenses16 mm, 1.1", 20 MP, C-mount machine vision lens, F2.8-22, <0.3% distortion (LS-FFL-0567)LS-FFL-0567
Type
Machine vision fixed
Sensor format
1.1"
Mount
C-mount
Resolution
16-25 MP
$679.00View partBuild a system with it

For surface changes, bar lights let you adjust illumination direction. Search the lighting finder after testing the angle and coverage on representative parts. One candidate to evaluate is LS-BAR-1047.

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

Treat these as evaluation candidates, not a validated assembly. The catalog information alone does not establish the final field of view, contrast, or measurement accuracy.

Align the images before subtracting

  1. Capture repeatable images and align the inspection image to the reference coordinate system before calculating differences.

Subtraction assumes that corresponding pixels represent the same surface location. A small translation can create paired bright outlines around every existing edge. Rotation, perspective changes, and lens distortion can produce similar false regions.

Use stable part features or fixture references for registration. Avoid aligning primarily on the defect itself, because registration can partially remove the change you want to detect. Save the transformation used for every image.

Inspect the aligned pair before adjusting the threshold. The closest companion workflow, reducing false defects in image subtraction, addresses contour artifacts that can otherwise look like valid detections.

Keep exposure, gain, illumination geometry, and focus consistent. Automatic adjustments can change the difference image even when the part is unchanged. If automatic acquisition controls are necessary, understand which pixels influence exposure and gain.

Build a two-dimensional defect mask

  1. Calculate the difference using a suitable numeric type, apply a documented threshold, and inspect the resulting binary mask.

For a grayscale pair, use the absolute difference at each image location:

D(u, v) = absolute(I_current_aligned(u, v) - I_reference(u, v)) M(u, v) = 1 when D(u, v) >= T; otherwise 0

Convert unsigned image values to a suitable signed or floating-point type before subtracting, unless your image operation already handles absolute differences safely. Ordinary unsigned subtraction can wrap or clip negative differences.

For color images, define how channel differences become one binary decision. You might compare a consistently derived grayscale image or take the largest channel difference. These choices detect different changes, so validate them against actual defects. Do not accidentally treat a color array as a spatial volume.

For an illustrative 8-bit image, a threshold of 12 intensity levels is only a starting experiment. It is not a transferable defect specification. Compare difference values from acceptable parts and known defects, including normal variation.

Mask out irrelevant background areas. Use opening, closing, or hole filling only when their effect on the smallest required defect is understood. These operations can erase narrow defects, join separate regions, or shift their centroids.

Label regions and extract their centroids

  1. Label connected foreground pixels, reject regions using validated criteria, and calculate a coordinate record for each retained region.

Choose connectivity explicitly. Four-connected labeling joins pixels sharing an edge. Eight-connected labeling also joins diagonal neighbors. The choice can change both region count and reported position.

For a binary region containing N foreground pixels, the centroid is the arithmetic mean of their coordinates:

u_centroid = sum(u_i) / N v_centroid = sum(v_i) / N area_pixels = N

The following pseudocode uses zero-based indexing. Array access is row first, while the reported point is column first:

regions = label_components(mask, connectivity=8)
detections = []

for region in regions:
    rows, columns = foreground_indices(region)
    area_pixels = count(rows)

    if area_pixels < minimum_area_pixels:
        continue

    detections.append({
        u: mean(columns),
        v: mean(rows),
        area_pixels: area_pixels,
        bounding_box: bounds(columns, rows)
    })

The minimum area must come from detection trials, not convenience. A long, narrow crack and a compact spot may have equal area but different significance. Consider length, shape, or contrast when area alone cannot express the acceptance requirement.

Return an empty detection list when nothing qualifies. Keep fractional centroid coordinates; rounding is appropriate for drawing a marker, not for storing the underlying position.

Preserve coordinates through crops and warps

  1. Convert local coordinates into the intended reporting frame and verify the conversion with a visible overlay.

Use a written convention such as: origin at the center of the upper-left pixel, u increasing right, and v increasing downward. Check your software's indexing convention rather than assuming it matches this one.

A crop changes the origin. If an unscaled crop starts at full-image column 300 and row 120, a local centroid at u = 25.5 and v = 40.5 becomes:

u_full = 300 + 25.5 = 325.5 pixels v_full = 120 + 40.5 = 160.5 pixels

Resizing adds another transformation. Some resampling conventions include half-pixel offsets, so dividing coordinates by a scale factor is not always sufficient. Keep the actual crop and resize transformations rather than reconstructing them later.

Registration creates another distinction: a centroid measured in the aligned image belongs to the reference image frame. To mark its corresponding point in the original inspection image, apply the inverse registration transformation.

The guide to mapping points into a warped image explains this direction-of-mapping problem. Never apply a reference-frame calibration directly to raw-image coordinates without the required conversion.

Calibrate pixel positions into physical coordinates

  1. Calibrate the exact reporting image frame against known target positions on the inspection plane, then verify the mapping independently.

The field of view determines object-plane sampling. A wider view with the same pixel count gives fewer samples across each defect. Keep the calibration target on the surface whose positions you intend to report.

How sensor width, working distance and field of view set the focal length Rays from the edges of the sensor pass through the lens and spread to the edges of the field of view. The ratio of sensor width to field of view is the magnification m.CameraField of view (FOV)Working distance (WD)≈ fsensor width slensm = s / FOVf = WD × m / (1 + m)
How sensor width, working distance and field of view set the focal length. Rays from the edges of the sensor pass through the lens and spread to the edges of the field of view. The ratio of sensor width to field of view is the magnification m.

For a front-facing, distortion-corrected plane with approximately uniform sampling, estimate scale from field width and image width:

s = field_width / image_width s = 80 mm / 2000 pixels = 0.04 mm/pixel samples_across_defect = defect_width / s samples_across_defect = 0.20 mm / 0.04 mm/pixel = 5 pixels

This example describes hypothetical image geometry, not the performance of the listed hardware. Five samples across a feature do not guarantee reliable detection; contrast, focus, noise, and processing still matter.

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

For an axis-aligned example, suppose the calibrated origin is at u₀ = 1000.5 pixels and v₀ = 500.5 pixels. Let physical X increase rightward and physical Y increase upward. A region with centroid u = 1120.5 pixels and v = 640.5 pixels maps as follows:

X = (u - u0) × s X = (1120.5 - 1000.5) × 0.04 = 4.8 mm Y = -(v - v0) × s Y = -(640.5 - 500.5) × 0.04 = -5.6 mm

If that region contains 80 pixels, its estimated area under this uniform square sampling is:

A = N × s² A = 80 × 0.04² = 0.128 mm²

A tilted plane generally requires a planar projective mapping rather than one constant scale. Correct lens distortion consistently with your calibration model. The distinction between sensor sampling and object sampling is explained further in calculating object-space pixel size.

Also distinguish the mapped pixel centroid from a physical area centroid. Under perspective, equal image areas represent unequal physical areas. If physical area or its centroid matters, rectify to a suitable metric plane or integrate using the mapping's local area scale.

Verify position and detection separately

  1. Test localization accuracy and defect detection as separate acceptance requirements, using measurements not used to fit the calibration.

First, place known check points across the inspection plane, including its corners. Compare reported positions with independently known positions. Record maximum errors and spatial patterns, not only the average calibration residual.

Next, capture repeated images without moving the part. This isolates acquisition and segmentation variation. Then reposition the part through the permitted operating range to include fixture and registration effects.

At the illustrative sampling above, a centroid shift of 2 pixels corresponds to 2 × 0.04 = 0.08 mm. That is one error contribution, not the system's total accuracy. Calibration, target uncertainty, part height, and registration can add further error.

Finally, vary the threshold over a justified range. For example, compare thresholds of 10, 12, and 14 intensity levels while holding other settings fixed. Track missed defects, false regions, merged components, and centroid movement. A position that changes substantially with a small threshold adjustment deserves investigation.

Subpixel centroid output is a numerical result, not proof of subpixel physical accuracy. The principles in estimating edge position accuracy help separate sampling from achievable localization performance.

Checks before ordering or deploying

Use this checklist to close the gaps between a working script and a repeatable inspection:

  • Smallest defect: specify width, contrast, and shape, not just total area. These drive sampling and lighting trials.
  • Motion: confirm shutter behavior and exposure are suitable. Blur changes the mask and its centroid.
  • Optical coverage: verify sensor coverage, mount, focus range, and working distance together.
  • Illumination: test acceptable surface variation. Reflections must not dominate the difference mask.
  • Coordinate history: retain crop, scaling, alignment, and calibration identifiers with each detection.
  • Calibration plane: confirm the part surface remains within the validated height range.
  • Verification data: save original images, masks, coordinates, and settings so decisions can be reproduced.
  • Output meaning: label coordinates as pixels or physical units, including origin and axis direction.

When this workflow is not the right answer

Reference subtraction is a poor fit when acceptable parts naturally vary more than the defects. Texture changes, flexible geometry, and uncontrolled reflections can produce many legitimate differences. Feature-based inspection or a different lighting arrangement may be more suitable.

A single planar calibration also cannot recover arbitrary surface height. If the defect lies above or below the calibrated plane, its mapped position can be wrong even when its pixel centroid is correct.

For the next step, use the camera configurator to organize acquisition requirements. If the allowable physical error is already specified, send the field of view, smallest defect, motion conditions, and sample images through application support before choosing the final hardware.

Questions

Is a defect centroid enough to describe its location?

A centroid is useful for marking or tracking a connected region, but it does not describe the complete defect. It can fall outside an irregular region and can move when segmentation changes. Retain the bounding box and mask when the defect's shape or nearest boundary determines the decision.

How do I convert defect pixel coordinates into millimeters?

Use a calibration made for the same image coordinate frame and inspection plane. A constant scale may be sufficient for a validated front-facing setup, while a tilted plane generally needs a projective mapping. Lens distortion, crop offsets, and image resizing must be handled consistently.

Why does the plotted defect marker appear in the wrong place?

Array indexing usually addresses a row before a column, while image plotting commonly expects horizontal position before vertical position. Check indexing origins as well. Crop offsets, resizing, and registration can introduce additional differences between the coordinates you calculate and the image you display.

Should defect coordinates stay fixed when the threshold changes?

No. Thresholding changes which pixels belong to a region, so it can change its size, connectivity, and centroid. Test several justified settings against acceptable parts and known defects. Large coordinate changes can indicate weak contrast, merged regions, uneven illumination, or registration errors.

Will a higher-resolution camera automatically improve position accuracy?

More pixels improve sampling only when the lens, focus, contrast, and motion conditions support the added detail. They do not correct calibration errors or unstable segmentation. Choose resolution from the smallest required feature, then validate the complete setup against the permitted position error.