System designHow-to

Grayscale centroid of a dark circle: invert for sub-pixel

A grayscale centroid weights bright pixels, so a dark circle on white paper centroids the paper frame. Invert the image first and lock sub-pixel accuracy.

LumaSpec Vision11 min read Share on LinkedIn

On this page 9 sections
  1. 01The fix comes first: invert the region
  2. 02Why the weights land on the paper
  3. 03The two biases that set your error budget
  4. 04The procedure: raw frame to sub-pixel center
  5. 05Give the mark enough pixels
  6. 06Worked example: a 10 mm dark circle on white paper, 0.05 mm budget
  7. 07When inversion is not the answer
  8. 08Checks before you ship it
  9. 09Questions

An intensity-weighted centroid multiplies every pixel's coordinates by that pixel's grey level, then divides by the total grey level. Run it on a dark circle printed on white paper and the circle barely counts: its weight is near zero. The result tracks the white frame inside your region of interest, so the reported center wanders with region placement and jitters with the lighting. The cure is one line of processing - invert the image inside the region so the circle becomes the bright object. This guide gives the numbers behind that bias, the full procedure from raw frame to sub-pixel center, the optics that put enough pixels on the mark, and the cases where a blob or an edge fit wins outright.

The fix comes first: invert the region

Before touching exposure, region size or light intensity, invert the pixels inside the region of interest. On 8-bit data, replace every level I with 255 - I: ink at level 30 becomes 225, paper at 240 falls to 15, and the circle now owns about 98% of the total weight. From that point the centroid measures the circle, not its surroundings.

If you control the target rather than the software, the hardware equivalent is to print a white circle on matte black stock, or to put an opaque mask with a circular hole over a backlight. Both deliver bright-on-dark contrast with no transform at all, and both follow the same rule as the software fix: the mark must be the brightest thing in the region.

Why the weights land on the paper

The grayscale centroid is a center of mass:

x̄ = Σ x·I(x, y) / Σ I(x, y) ȳ = Σ y·I(x, y) / Σ I(x, y)

Every pixel votes with its grey level. Ink at 30 casts one vote where paper at 240 casts eight, so a region a few pixels larger than the circle is dominated by its white frame - the annulus plus the four corners of the bounding box. That is the whole failure: with the weights on the frame, you compute the frame's center of mass, and the frame becomes asymmetric the moment the region sits off-center or the light slopes across the paper.

The binary path you probably already run - imaqCalcCoeff(IMAQ_CENTER_MASS_X, Y) on a thresholded particle - weights every particle pixel equally, which is why it beat the grayscale function in the field. The grayscale center of mass that IMAQ Centroid computes uses exactly this intensity weighting, which is why the standing advice is to present a bright object on a dark background.

The two biases that set your error budget

Uneven light does more than look bad; it moves the answer. For a uniform disk under a linear brightness slope, the centroid shifts by:

shift ≈ (ΔI / I across the disk) × R / 8

with R the disk radius in pixels. A 2% brightness fall across a 200 px disk (R = 100) shifts the center 0.02 x 100 / 8 = 0.25 px - more than twice a healthy per-axis jitter budget, from light alone. Flat-field division, not more pixels, removes this term.

The second bias is sampling itself. An intensity-weighted center of mass computed on raw pixel values pulls the result toward pixel centers, and the pull shrinks as the disk covers more pixels; published simulation and laboratory studies of circular-feature location show location accuracy improving steadily with disk diameter in pixels. The practical line: give the mark at least 100 px across for centroid work, and switch to an edge fit below about 30 px.

The procedure: raw frame to sub-pixel center

  1. Freeze exposure and gain. Turn off auto exposure, auto iris and auto gain. Per-frame brightness drift re-creates the gradient bias on every frame; contour position jitter under automatic exposure shows the same mechanism on measured edges.
  2. Set the levels. Expose so the paper sits between 220 and 245 and the ink at or under 40, with nothing clipped at 255 - flat-field division needs that headroom.
  3. Capture a flat field. Put blank white stock where the part sits, same light, same f-number, and store the image.
  4. Flatten, then invert. Inside the region, divide by the normalized flat field, then replace every level I with 255 - I. The circle is now the dominant weight and the residual slope is a few tenths of a percent.
  5. Mask and compute. Keep the region a few pixels larger than the disk - after inversion the margin is harmless - and run the intensity-weighted centroid, imaqCentroid() on the inverted region. Your existing binary path, imaqCalcCoeff(IMAQ_CENTER_MASS_X, Y), stays valid as the uniform-weight cross-check.
  6. Prove it. Log the center over 100 static frames and take the per-axis standard deviation. Below 0.1 px, ship it; at 0.3 px or worse, chase the light or the print, not the algorithm.

Give the mark enough pixels

The optics decide how much the centroid has to work with. Work from the field of view outward:

m = sensor width / field of view f = WD × m / (1 + m) object-space pixel = pixel size / m

This is the same working distance and field of view formula used for gauging set-ups, and the object-space pixel it produces is what your repeatability budget divides by; object-space pixel size from magnification works it from the other end. For pixels-per-part budgets by task, camera resolution for dimensional inspection sets the floor. Put your own numbers into the lens focal length calculator or the one below.

Lens focal length, field of view and working distance
Units
Solve for
Focal length
– mm
Field of view
– mm
Working distance
– mm
Magnification
– x
Angle of view
– °
Pixel on the part
– mm

Thin-lens model: m = sensor width / field of view, f = WD × m / (1 + m). Accurate to a few percent above about 150 mm; check the lens's minimum object distance.

Find lenses with this focal length Open the full calculator

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.

A 1.1" global-shutter camera with a C-mount pairs with a wide choice of standard lenses, and the camera finder filters on format and interface.

The MV-CH120-11UC is a 12 MP camera with USB3, global shutter and a C-mount, built on a 1.1" sensor format.

Area scan camerasHikrobot MV-CH120-11UC 12 MP Area Scan Camera, USB3, IMX304, High-speed version, ColorMV-CH120-11UC
Sensor format
1.1"
Lens mount
C-mount
Interface
USB3
Resolution
12 MP
$2,339.00 · quotedView and quoteBuild a system with it

The LS-FFL-0567 is a 16 mm C-mount lens that covers 1.1" sensors at 20 MP, with an F2.8-22 iris and distortion under 0.3%.

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

A typical 1.1" sensor at this resolution samples at 3.45 µm, and the worked example below uses exactly that geometry with this 16 mm focal length. If your field of view is already fixed, start at the lens step of the configurator instead.

Worked example: a 10 mm dark circle on white paper, 0.05 mm budget

Inputs, each with its source:

  • Printed circle 10 mm diameter on white paper, ink level 30, paper level 240 - levels from a locked exposure of 4 ms at f/5.6, mid-range of the lens's iris.
  • Center repeatability budget 0.05 mm (50 µm), a typical guidance tolerance.
  • Sensor: 4,096 px across at 3.45 µm = 14.1 mm wide, the 1.1" class above.
  • Lens focal length 16 mm; field of view 60 mm to hold the mark plus context.
  • Region 690 px square, placed 3 px off the disk center - the normal error when the region follows a particle finder.

The arithmetic:

  1. Magnification: m = 14.1 mm / 60 mm = 0.235.
  2. Working distance: WD = f (1 + m) / m = 16 mm x 1.235 / 0.235 = 84 mm.
  3. Object-space pixel: 3.45 µm / 0.235 = 14.7 µm.
  4. Disk size: 10,000 µm / 14.7 µm = 680 px across, so radius R = 340 px.
  5. Frame dominance, raw image. Disk area = pi x 340² = 363,000 px²; frame area = 690² - 363,000 = 113,000 px². A region 3 px off-center puts the frame's own center of mass 476,100 x 3 / 113,000 = 12.6 px from the disk center. With paper at 240 and ink at 30, the frame holds 113,000 x 240 / (113,000 x 240 + 363,000 x 30) = 71% of the weight, so the reported center lands 0.71 x 12.6 = 9.0 px out: 9.0 x 14.7 µm = 132 µm, or 0.13 mm, from a 3-pixel placement error.
  6. After inversion. Ink weighs 225 and paper 15, so the frame share falls to 113,000 x 15 / (113,000 x 15 + 363,000 x 225) = 2.0%, and the same 3 px offset costs 0.020 x 12.6 = 0.26 px = 3.8 µm.
  7. Light slope. Paper darkening 1% across the disk shifts the center 0.01 x 340 / 8 = 0.43 px = 6.3 µm. Flat-field division holding the residual slope to 0.2% cuts that to 0.09 px = 1.3 µm.
Error source Raw image Inverted, flat-fielded
Region 3 px off-center 9.0 px = 132 µm 0.26 px = 3.8 µm
Brightness slope across disk 0.43 px = 6.3 µm at 1% 0.09 px = 1.3 µm at 0.2%
Combined about 0.13 mm about 4 µm

Decision: the raw chain misses a 50 µm budget by nearly 3x on region placement alone. Inverted and flat-fielded it lands near 4 µm - a 12x margin (50 / 4 = 12.5). Keep the black-on-white target already in the field, invert in software, and spend the margin on mechanical tolerance instead of a reprint.

When inversion is not the answer

If you own the target design, change the physics before the software. A white circle on matte black stock carries all the weight natively and tolerates dirty or aged paper better than inverted black print. The cleanest bright-on-dark source is an opaque mask with a circular hole over a backlight: the hole glows, the mask stays dark, and no paper grain enters the region. Collimated units keep the hole's edge sharp, and the collimated versus diffuse backlight choice for edge gauging weighs that trade-off; the backlights category spans collimated, flat and high-power units.

The LS-BKL-0830 is a 120 x 120 mm collimated backlight that runs on 24 V.

Backlights120 x 120 mm Collimated back light (LS-BKL-0830)LS-BKL-0830
Size
100-200 mm
Type
Collimated
Voltage
24 V
$309.00View partBuild a system with it

When the print itself is the problem - uneven ink density, partial shading, a scuffed mark - grey weighting is the wrong tool even inverted. Threshold and use uniform-weight particle analysis, the binary path that was already winning, or extract sub-pixel edge points and fit. Detecting a circle in a binary image with contour analysis covers the binary route; the Find Circular Edge tool exists for exactly this shape, so feed it a generous search annulus; circle detection with a Hough transform handles cluttered scenes where the mark is only partly visible.

And since a circle off the optical axis images as an ellipse, fit the ellipse and take its center. The ellipse center equals the projected circle center exactly only on-axis; the difference grows with off-axis angle and with the disk's angular size, so calibrate wide-angle set-ups rather than assuming the offset away. If the mark also carries color, circle-center color measurement adds that channel once the geometry is stable.

Checks before you ship it

  • Grey levels locked: paper 220-245, ink at or under 40, no clipped pixel inside the region.
  • Exposure, gain and iris frozen, with identical settings across the whole run.
  • Flat field captured with the production lens and light at the same f-number.
  • 100-frame static test passes at or under 0.1 px per axis.
  • Inverted centroid and a least-squares circle fit on the same frames agree within 0.3 px.
  • Region margin of several pixels confirmed harmless after inversion, frame weight near 2%.

Next step: put camera, lens and light together in the system configurator, or bring the numbers to the contact page and our applications team will check the budget with you.

Questions

Why does the grayscale centroid jitter more than my blob analysis?

Because the weights sit on the bright frame around the circle, not on the circle. Any frame-to-frame brightness change moves them: a 1% drift in the slope across a 340 px disk alone shifts the centroid 0.43 px, and auto exposure produces exactly that kind of drift. Invert the region, freeze exposure and gain, and the jitter falls into the 0.05-0.2 px class.

Should I invert in software or reprint the target white-on-black?

Invert in software when black-on-white targets are already deployed: it is free, reversible, and with flat-field correction good for roughly 4 µm on a 10 mm mark imaged at 680 px. Reprint white-on-black when you control the target, since native contrast is higher, it tolerates dirty or aging paper, and no processing step can be forgotten. A mask over a backlight beats both.

How many pixels do I need across the circle?

At least 100 across for an intensity-weighted centroid, and more is better: the pull of sampled centroids toward pixel centers shrinks roughly in proportion to disk diameter. Below about 30 px, stop centroiding and fit sub-pixel edge points instead, because the sampling bias dominates everything else at that size. Size the optics first: pixels across = object diameter x m / pixel size.

My circle images as an ellipse - does the center method still work?

Yes. Fit the ellipse and take its center. On-axis, the ellipse center is the projected circle center exactly; off-axis the two separate by an amount that grows with the off-axis angle and with the disk's angular size relative to the working distance. Keep the mark near the optical axis for guidance work, or calibrate the mapping when the camera views the scene wide.

Is a fitted edge better than a centroid for circle center?

Usually yes, once you care about the last tenth of a pixel. A least-squares circle or ellipse fit on sub-pixel edge points reaches a few hundredths of a pixel repeatability and survives uneven print density and partial shading that shift any grey weighting. Keep the inverted centroid as the fast path, cross-check the two, and investigate whenever they differ by more than 0.3 px.