All writing
Metrology8 min read

Measuring a part with a camera: pixel-to-millimetre calibration that holds up

Counting pixels is easy. Turning them into millimetres you'd sign a part off on is a metrology problem: distortion, perspective, edge bias, and an uncertainty budget. Here's the full chain in OpenCV, with numbers.

A compact industrial machine-vision camera with its lens mount open, showing the image sensor

Photo: Lg123456 · CC BY-SA 4.0

The first time you measure a part with a webcam and OpenCV, it feels like magic: threshold, find the contour, multiply pixels by a scale factor, and there's your diameter. The second time, you move the part 20 mm toward the edge of the frame and the diameter changes by two tenths. The third time, you lift it onto a 5 mm spacer and it grows. None of that is OpenCV's fault. It's optics, and it's exactly the stuff a metrologist would ask you about before trusting a single number.

This post is the chain from pixels to millimetres you can defend: remove lens distortion, set the scale at the measurement plane, find edges to a fraction of a pixel, and then measure your own uncertainty so you know which tolerances the setup can actually inspect. If you haven't built a basic inspection script yet, start with your first OpenCV part-inspection script. This is the step after.

Start with the resolution budget, not the code

Before buying anything, do one division: field of view ÷ pixels across = millimetres per pixel. Then compare it to the tolerance you need to check, using the old metrology rule that your measurement uncertainty should be about a tenth of the tolerance band (the same logic behind guard-banding).

Field of viewSensor width (px)mm / pxRealistic repeatability*Tolerance band it can check
200 mm19200.104~0.02–0.05 mm±0.25 mm and looser
100 mm40000.025~0.005–0.012 mm±0.06–0.1 mm
40 mm40000.010~0.002–0.005 mm±0.025–0.05 mm
20 mm (telecentric)50000.004~0.001–0.002 mm±0.01–0.02 mm
*Assumes good backlighting and subpixel edge detection at roughly 0.2–0.5 px repeatability. Your own repeatability study (below) replaces these guesses.

The pattern is clear: a camera measures to a small fraction of its field of view. Want to check a ±0.02 mm bore on a part 150 mm across? Don't image the whole part. Image only the feature, or use a different instrument.

Lighting: backlight anything you want to measure

For dimensional work, backlighting (part between camera and a diffuse light panel) is worth more than any algorithm. It produces a black silhouette on white, with edges defined by geometry rather than by surface finish, scratches, or coolant film. Front lighting measures reflections; backlighting measures the part. (Lighting is 80% of the job, and that's doubly true when the output is a number.)

Step 1: calibrate lens distortion with a ChArUco board

Every non-telecentric lens bends straight lines, most visibly near the corners. OpenCV models that with a camera matrix and distortion coefficients, which you estimate by photographing a known pattern 15–25 times at different positions and tilts. Use a ChArUco board (a chessboard with ArUco markers in the white squares): unlike a plain chessboard, it still works when part of the board is out of frame, so you can cover the corners of the image, which is where distortion lives. Print it flat, measure the printed square size with calipers, and use that real number:

import glob
import cv2
import numpy as np

# 7x5 squares; measure YOUR printed square and marker size, in mm
dictionary = cv2.aruco.getPredefinedDictionary(cv2.aruco.DICT_5X5_100)
board = cv2.aruco.CharucoBoard((7, 5), 30.0, 22.0, dictionary)
detector = cv2.aruco.CharucoDetector(board)

obj_points, img_points, size = [], [], None
for path in glob.glob("calib/*.png"):
    gray = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
    size = gray.shape[::-1]
    corners, ids, _, _ = detector.detectBoard(gray)
    if ids is not None and len(ids) >= 6:
        obj, img = board.matchImagePoints(corners, ids)
        obj_points.append(obj)
        img_points.append(img)

rms, K, dist, _, _ = cv2.calibrateCamera(obj_points, img_points, size, None, None)
print(f"reprojection error: {rms:.3f} px from {len(obj_points)} views")
np.savez("camera.npz", K=K, dist=dist)

Aim for a reprojection error well under 0.5 px. If it's higher, the usual culprits are a board that isn't flat (glue it to glass or aluminium), motion blur, or too few views near the image corners. This needs OpenCV 4.7 or newer: the CharucoDetector API replaced the older cv2.aruco.calibrateCameraCharuco functions you'll still see in old tutorials.

Lock the lens before you calibrate

Calibration is only valid for that exact focus and zoom. Set focus, tighten the lens's locking screws (or tape the ring), then calibrate. Bump the focus ring afterwards and every number downstream is wrong.

Step 2: set the scale at the measurement plane

Distortion calibration makes the image geometrically honest; it doesn't tell you millimetres per pixel where the part sits. For that, image a reference of known size, such as a gauge block or a pin gauge, at the same height as the feature you'll measure, run it through the same edge-detection code, and divide:

mm_per_px = certified_size_mm / measured_size_px      # e.g. 25.0000 / 1001.37 = 0.024966

Using the same pipeline for the reference is the subtle, important part. Any systematic edge bias (threshold choice, blooming, slight defocus) affects the reference and the part alike, so it largely cancels in the ratio. That's a big reason a well-run vision gauge can beat its own pixel size.

The perspective trap: height changes size

Here's the error that ruins most DIY setups. A normal (entocentric) lens has perspective: things closer to the camera look bigger. Magnification scales with roughly 1 / working distance, so a feature 1 mm higher than your calibration plane at a 300 mm working distance appears about 1/300 ≈ 0.33% larger. On a 50 mm dimension that's 0.17 mm of error, from one millimetre of height. A chip under the part, a burr, a thicker gasket on the fixture: all of them shift your measurement.

  • Fixture the height. Hard locating surfaces at the calibrated plane, cleaned every time.
  • Increase working distance. At 1,000 mm the same 1 mm height error is 0.1% instead of 0.33%; you pay with a longer lens.
  • Measure features in one plane. Comparing a top edge to a bottom edge mixes two magnifications.
  • Use a telecentric lens when the tolerance demands it. Telecentric optics keep magnification essentially constant over their depth range. They're expensive and their field of view equals their front-lens size, but for precision silhouette gauging they are the real answer.

Step 3: find edges to a fraction of a pixel

A thresholded contour snaps edges to whole pixels. But a backlit edge isn't a step; it's a ramp a few pixels wide. Find where that ramp crosses 50% between dark and light, interpolating between pixels, and you locate the edge to a fraction of a pixel. Here's that on a single scan line across a feature:

def subpixel_crossings(profile, level=None):
    """Positions (in px, fractional) where an intensity profile crosses 'level'."""
    p = profile.astype(float)
    if level is None:
        level = (np.percentile(p, 5) + np.percentile(p, 95)) / 2   # 50% of dark..light
    s = np.sign(p - level)
    idx = np.where(np.diff(s) != 0)[0]            # crossing lies between i and i+1
    return [i + (level - p[i]) / (p[i + 1] - p[i]) for i in idx]

MM_PER_PX = 0.024966                             # from the gauge-block step above
cal = np.load("camera.npz")
img = cv2.undistort(cv2.imread("part.png", cv2.IMREAD_GRAYSCALE), cal["K"], cal["dist"])

row = img.shape[0] // 2
band = img[row - 5: row + 6, :].mean(axis=0)     # average 11 rows to beat noise
edges = subpixel_crossings(band)
width_px = edges[-1] - edges[0]
print(f"width: {width_px:.2f} px = {width_px * MM_PER_PX:.4f} mm")

Averaging several rows along the edge cuts noise dramatically. For round features, collect subpixel edge points all the way around and fit a circle by least squares instead of using one line. The fit averages hundreds of edge points and gives you roundness as a bonus. The Vision Lab on this site lets you play with thresholds and edge finding interactively before writing any of this.

Step 4: prove it with a repeatability study

Your camera is now a gauge, so treat it like one. Measure the same gauge block 25–30 times, removing and replacing it each time (re-placement is where the real variation hides), then do the same with a few real parts and compare them to a CMM or a calibrated micrometer:

  • Repeatability = standard deviation of the 30 readings. Six times that is your measurement spread.
  • Bias = mean reading − certified value. Correct it, or recalibrate the scale.
  • Linearity: repeat with two or three gauge sizes across the field of view. If the error changes with size or position, revisit distortion calibration.
  • Decide what it can check: if the 6σ spread is more than ~10–20% of a tolerance band, that tolerance belongs on a different instrument. The guard-band visualizer shows what that uncertainty does to your accept/reject limits.

Re-check the scale every shift

Temperature, a bumped mount, and lamp ageing all drift. Leave a gauge block in a fixed spot in the field of view and measure it automatically with every part. If its reading drifts beyond a limit, the system stops itself before it passes a bad part.

A camera doesn't measure parts. A calibrated, validated camera system measures parts, and the difference is everything in this post.

If you want the theory behind why that uncertainty budget matters so much, GD&T vs. reality covers it from the tolerancing side, and what FEA taught me about touch-probe error is the same discipline applied to contact probing.

Frequently asked questions

How do I convert pixels to millimetres in OpenCV?+

Correct lens distortion with cv2.calibrateCamera (using a ChArUco or chessboard target), then image a reference of known size at the same height as the part, measure it with the same edge-detection code, and divide the certified size by the measured pixel size to get mm per pixel.

How accurate can camera-based measurement be?+

With backlighting and subpixel edge detection, repeatability of roughly 0.2–0.5 pixel is achievable, so accuracy depends mostly on field of view: around 0.005–0.01 mm with a 100 mm field on a 4,000-pixel sensor. Perspective error from part height often dominates unless you use a telecentric lens.

Do I need a telecentric lens for measurement?+

Not always. A standard lens works if the measured features sit at a fixed, calibrated height. A telecentric lens is needed when part height varies or tolerances are tight, because it keeps magnification constant with distance.

Why use a ChArUco board instead of a chessboard?+

Each ChArUco corner is uniquely identified by its neighbouring ArUco markers, so calibration still works when the board is partly out of frame or partly occluded. That makes it easier to cover the image corners, where lens distortion is strongest.

Muerus Rodrigues

Applications Engineer

Get in touch

Keep reading

Home
Projects
Blog
Tools
Code
Stepsmith
Contact
LinkedIn
Request Résumé