Pixel-difference template matching compares a stored template against the image at one angle and one size. Rotate the part 30 degrees or let it sit a few millimetres closer to the lens, and the score collapses — unless the tool also sweeps a range of angles and scales, which multiplies the search time. Image moments take the opposite route: they reduce a binary silhouette to seven numbers that barely move when the shape translates, rotates or changes size. Below are the moment definitions, the normalization that cancels scale, a worked example with the arithmetic, the distance metric and its threshold, and the cases where a gradient matcher still wins.
What image moments measure
A moment of order (p, q) is a weighted sum over every pixel of a region, the weight being the pixel coordinates raised to the powers p and q. On a digital image the double integral of the continuous definition becomes a double sum, with f the indicator function of the binary silhouette — 1 inside the part, 0 outside:
Two quantities drop out immediately. M_00 is the region area in pixels, and the centroid sits at x_bar = M_10 / M_00 and y_bar = M_01 / M_00. Recompute the moments about that centroid and you get the central moments mu_pq, which no longer change when the part drifts across the field of view:
The centroid is also a usable origin for downstream results — picking the wrong reference origin is its own class of matching errors, covered in pattern matching reference coordinate systems.
Normalized central moments and the seven Hu invariants
Central moments removed translation. Scale is next. Enlarge a shape by a factor s and every central moment mu_pq grows by s^(p+q+2): the coordinate terms contribute s^(p+q) and the pixel area element contributes s^2. Divide by mu_00 raised to the power 1 + (p+q)/2 and those factors cancel exactly:
Rotation is the harder one, and it is what the Hu invariants solve. Rotating the axes redistributes eta_20, eta_02 and eta_11 into one another, but certain algebraic combinations of the normalized central moments come out unchanged. The seven Hu invariants, published in 1962, are the standard set; the first two are:
The remaining five, I3 through I7, add terms up to third order and carry the fine discrimination; the full set is tabulated in the image moment reference. One closed-form result is worth memorizing: a filled disc of any radius gives I1 = 1 / (2 * pi) = 0.159, and by symmetry every other invariant of a disc is zero. Scale changes nothing — that 0.159 holds for a 2 mm pin and a 200 mm blank alike.
Worked example: an 18 mm washer at 250 mm with ±6 mm height
Inputs, each with its source:
- Field of view 120 x 90 mm, the window the fixture allows.
- Smallest feature to verify: the 6 mm central hole of an 18 mm washer.
- 20 pixels across the smallest measured feature, the working rule for contour and blob measurement.
- Working distance 250 mm, with part height varying ±6 mm on the conveyor.
The pixel rule first. Pixel size = feature / 20 = 6 mm / 20 px = 0.3 mm/px. The field of view then needs 120 / 0.3 = 400 pixels by 90 / 0.3 = 300 pixels, so 400 x 300 = 120,000 pixels, about 0.12 MP. The 18 mm outer diameter spans 18 / 0.3 = 60 px, and the binary area is pi * (9^2 - 3^2) = pi * 72 = 226 mm^2, which at 0.09 mm^2 per pixel puts M_00 at roughly 2,500 pixels.
Now the scale argument. A part sitting 6 mm high is imaged at s = 250 / 244 = 1.025 times nominal size, a +2.5% band; the low side is 250 / 256 = 0.977, or −2.3%. Raw moments drift badly across that band: mu_00 grows by s^2 = 1.051 and mu_40 by s^6 = 1.160. The normalization denominator mu_00^3 grows by (s^2)^3 = s^6 = 1.160 — the same factor — so eta_40 does not move. One moment comparison per part replaces the angle-and-scale sweep entirely.
Decision: the station needs 0.12 MP, so resolution is not the selection driver. Pick the camera for interface and cycle time and spend the engineering on the backlight and the threshold. The camera resolution calculator runs the same arithmetic for your numbers.
The matching distance and its threshold
The seven values span orders of magnitude: I1 sits near 0.1 for compact shapes while the higher invariants commonly land at 10^-6 and below. A plain sum of differences would be dominated by I1 and blind to the rest, so compare in log space and keep each invariant's sign:
In code, with illustrative names — m is the moments dictionary of one binary region:
hu = cv2.HuMoments(m).flatten() # seven values, hu[0] = I1 ... hu[6] = I7
Sanity-check the pipeline by imaging a golden part rotated through 45-degree steps: the seven values hold nearly constant, and the small residue is pixel resampling, not the mathematics.
Threshold: start at D < 0.5 for accept. The same part under full rotation and the ±2.5% scale band stays well under it, and a different shape lands above 2. Calibrate the number from your own good and bad parts before release — 0.5 is the starting point, not the law.
Hu moments versus gradient shape matching
Gradient shape matching extracts edge points from the template, stores their gradient directions relative to a reference point, and scores candidate positions by how well image gradients align. It is the stronger matcher on detail and partial occlusion; Hu moments are the stronger one on invariance per unit of compute. Pixel-difference matching — the sum of absolute differences between template and image at each offset — is listed for completeness: it assumes unchanged rotation and scale, which is exactly the case you are leaving behind.
| Pixel-difference | Gradient shape matching | Hu moment matching | |
|---|---|---|---|
| Input | Grey template | Edge gradients | Binary silhouette |
| Translation | Searched per offset | Searched per offset | Invariant, centroid-based |
| Rotation | Breaks | Angle sweep or tolerant scoring | Invariant by construction |
| Scale | Breaks | Scale sweep | Invariant by construction |
| Dirt, occlusion | Score degrades fast | Tolerates missing edge points | Every lost pixel shifts all seven |
| Discrimination | Pixel-level | Fine detail | Whole shape only |
| Pose output | Offset only | Position and angle | Centroid only |
Decisions by case:
- The part arrives at any angle and you need match or no-match only: Hu moments, one pass, no sweep.
- A robot needs the grip pose: use a gradient matcher, which returns the angle; a moment match returns only the centroid, and rotationally symmetric parts need extra handling — symmetric part orientation matching covers the tolerance logic.
- The scene carries dirt or partial occlusion: gradient matching over sparse edge points, the approach detailed in edge based pattern matching under lighting changes.
- An angle sweep is eating your cycle time: replacing the sweep with a moment score is one of the fixes in raising the pattern matching update rate.
Where the computation runs
One silhouette at 0.3 mm/px is a few hundred thousand pixels and the moment pass is a single sweep, so compute is never the constraint — platform follows station layout.
Single station, verdict only: do it on the sensor with smart cameras. The MV-SC5060XM-08M-WBN is a 6 MP mono smart camera with an 8 mm M12-mount lens and a GigE interface. At the 0.12 MP this station calls for, any sensor in the class clears it many times over.
- Interface
- GigE
- Resolution
- 6-10 MP
- Sensor format
- 1/2.4"
- Shutter
- Rolling
Your own moment code, still on the device: the LS-SMC-0017, an 8.8 MP smart camera on an x86 platform with a global-shutter 1-inch sensor and a C-mount, is the platform class where you build and run your own contour pipeline next to the sensor.
Several cameras, lights and PLC I/O: a vision controller. The MV-VC3301H-128G66 brings an Intel Core i3 processor, 8 GB of memory, a 128 GB SSD, 5+ LAN ports, four built-in light channels and a VM6200Pro software license. Feed it area scan cameras from the camera finder, such as the MV-CU020-90GC, a 2.5 MP GigE camera with a global shutter, a C-mount and a 1/1.7-inch sensor.
- Sensor format
- 1/1.7"
- Lens mount
- C-mount
- Interface
- USB 2.0
- Resolution
- 6-10 MP
To spec the whole station — camera, lens, backlight, controller — in one pass, start the configurator at the camera step.
When Hu moments are the wrong tool
- Small features on a large body: twenty gear teeth contribute almost nothing next to the disc they sit on, and the seven values read "round part". Count the teeth or raise magnification; the moment score will not carry it.
- Near-symmetric parts: as the disc showed, symmetry collapses the discriminative invariants toward zero, and a washer keeps too few non-zero values to separate look-alikes. Pair I1 with area and hole-count checks.
- Surface defects: a scratch that never crosses the outline is invisible to a silhouette descriptor. That job belongs to adding AI surface defect inspection to existing cameras.
- Natural variation: produce, textiles and castings vary part to part in ways a fixed descriptor punishes; AI defect inspection for products with natural appearance variability is built for that spread. For the wider framing, see AI versus rule-based smart camera inspection.
- Pixel-accurate edge positions: moments summarize, they do not localize. Use edge fitting or a gradient matcher when the output is a measurement.
Checks before you commit
- Backlight the silhouette and lock the threshold — every threshold wobble lands in the moments. Track stability with an ROI measurement as described in measuring light intensity in machine vision.
- Twenty pixels across the smallest feature at your field of view, rechecked whenever the lens or standoff changes.
- Square pixels, or resample first: the scale cancellation assumes uniform scaling in x and y.
- One fill rule for template and live parts — filled contour or outline, never mixed.
- Push golden parts through the full rotation range and both height extremes; every distance must sit under threshold.
- Decide the mirror question before line start: the odd invariants flip sign under reflection.
- Segment with a fixed level or Otsu's method rather than an auto mode that hunts — automatic threshold segmentation in Lab color shows the stable setup.
When the station is specified, start it in the configurator or send the part drawings to applications and we will run the numbers with you.
Questions
Do Hu moments change when a shape is mirrored?
They do, and that is useful. A mirrored part keeps I1 and I2 unchanged, but the odd-order invariants change sign, I7 most reliably. Hu matching therefore separates left-hand from right-hand parts without extra code. If mirrored versions must pass as the same part, compare absolute values or match on I1 and I2 only, and write that choice into the acceptance spec.
Can moments give me the part angle, or only match or no-match?
For a part with one dominant axis, yes: theta = 0.5 * atan2(2 * mu_11, mu_20 - mu_02) returns the principal-axis orientation from the central second moments, computed at the same time as the match. It is modulo 180 degrees, so rotationally symmetric parts stay ambiguous — pair it with a hole or corner feature, or use a gradient matcher when the gripper needs a true angle.
How many of the seven invariants should I compare?
All seven. I1 and I2 carry the bulk shape and are nearly always non-zero; the higher orders are what separate a slotted washer from a plain one and a chamfered corner from a square one. Dropping them costs you the discrimination you chose moments for. Compare them as one distance, in log space with signs kept, against a threshold set from your own parts.
How do I set the accept threshold from samples?
Collect at least 20 known-good and 20 known-bad images spanning the full rotation and height range, compute the distance D for each against the golden template, and plot the two clusters — they separate cleanly in log space. Set the threshold at the midpoint between the worst good and the best bad, then add half the good cluster's spread as margin. The 0.5 starting value only exists so first trials produce verdicts.
Why do two different parts sometimes score as a match?
Because the seven values are a global summary of how area is distributed. A small slot, a shallow notch or a faint flat on a flange contributes almost nothing next to the body of the part, so near-identical silhouettes score alike. Raise magnification until the distinguishing feature spans more pixels, or add a second test — hole count or an area band — on top of the moment distance.
Is one smart camera enough, or do I need a vision controller?
For one station, one camera and a backlight, a smart camera is enough — the moment pass is a single sweep over a few hundred thousand pixels, far cheaper than the acquisition itself. Move to a vision controller when the line runs several cameras, needs light-channel control or PLC communication, or when one host must archive and log every result. Match the platform to the station, not to the algorithm.


