Skip to content
URIAH CLEMMER
$ cat blog/sensor-lens-and-the-space-between.md

Sensor, lens, and the space between

SOFTWARE MACHINE VISION 11 min read

Choosing a lens for a vision system comes down to one relationship, stated three ways. Field of view, working distance and focal length are not three independent things you balance against each other — fix any two and the third is already decided. Once that lands, lens selection stops being a catalogue exercise and becomes arithmetic.

This is the long version of what LensCalc does. The tool will not show you any of this; it just answers.

The terms

  • Sensor format — the physical size of the light-sensitive area, in millimetres. Not the pixel count. A "2/3-inch" sensor is 8.8 × 6.6 mm.
  • Focal length (f) — the distance from the lens's rear principal plane to the sensor when focused at infinity. The number on the barrel.
  • Working distance (WD) — lens to target. Strictly it should be measured from the front principal plane, which is somewhere inside the lens; in practice people measure from the front glass and accept a millimetre or two of error.
  • Field of view (FOV) — how much of the world lands on the sensor, in millimetres at the target plane. Always quote which dimension: width, height or diagonal.
  • Magnification (m) — sensor size divided by field of view. Below 1 the world is being shrunk onto the sensor, which is nearly always the case.
  • Image distance — lens to sensor. Equals the focal length only at infinity focus; closer targets push it further out.
  • Extension — how much further out. This is the number that decides whether you need a spacer ring behind the lens.
  • Pixel pitch — the physical size of one pixel, in micrometres. Sensor width divided by pixel count.
  • Nyquist — the sampling floor. A feature must cover at least two pixels to be detected at all.

One equation, three unknowns

Start with the thin lens equation, which relates the object distance, the image distance and the focal length:

THE THIN LENS EQUATION1f=1s0+1si

Then the geometry. Rays through the centre of a lens carry straight on; rays arriving parallel to the axis bend through the focal point. Draw both from the top of an object and they cross at the top of its image:

FF'objecthimage (sensor)h'fs₀ (working distance)sᵢTHIN LENS GEOMETRYthe object and image triangles are similar — that similarity is the whole calculator

The object triangle and the image triangle share an angle at the lens centre and both have a right angle at the axis, so they are similar. Similar triangles have proportional sides, and that proportion is magnification — which is why it can be written four equivalent ways:

MAGNIFICATION — THREE WAYS TO SAY ITm=h'h=sis0=sensorFOV

The equation the calculator rests on

Substituting the thin lens equation into that ratio and rearranging gives the form that does all the work:

THE ONE THE CALCULATOR RESTS ONm=fWDf

Everything else falls out of it. Think of the sensor as being projected out through the lens onto the target — a cone with its apex at the lens, widening linearly with distance:

sensorlenstarget≈ fWD (working distance)FOVFIELD OF VIEWthe sensor is projected outward through the lens — FOV grows linearly with distance

Since m = sensor ÷ FOV, that single expression rearranges into whichever unknown you have:

SOLVING FOR FIELD OF VIEWFOV=sensor×WDff SOLVING FOR FOCAL LENGTHf=WD×mm+1 SOLVING FOR WORKING DISTANCEWD=f×m+1m

The shorthand that is quietly wrong

Nearly every vendor's quick-reference card gives this instead:

THE SHORTHAND — CLOSE, BUT NOT THE SAME EQUATIONFOVsensor×WDf

That is the real equation with the − f dropped. It is the limit as working distance grows large, and at long range the error is negligible. Up close it is not:

WD / f     shorthand FOV     true FOV      error
100×          880.0 mm      871.2 mm      +1.0%
 20×          176.0 mm      167.2 mm      +5.3%
           70.4 mm       61.6 mm     +14.3%
           35.2 mm       26.4 mm     +33.3%
           17.6 mm        8.8 mm    +100.0%

Fourteen percent is the difference between a part fitting in frame and a part being clipped at the edges. The tool uses the exact form throughout.

Sensor formats are a historical accident

A 1-inch sensor is 12.8 × 9.6 mm, with a diagonal of 16 mm. Not 25.4. The naming survives from vidicon camera tubes, where a tube of one inch outside diameter produced a usable image circle of about 16 mm — the glass envelope took the rest. The tubes are long gone; the names stuck.

Full frame36.0 × 24.043.3 mmAPS-C23.6 × 15.728.3 mm4/3"17.4 × 13.0521.8 mm1"12.8 × 9.616.0 mm2/3"8.8 × 6.611.0 mm1/2"6.4 × 4.88.0 mm1/3"4.8 × 3.66.0 mmFORMATACTIVE AREADIAGSENSOR FORMATS, TO SCALEa 1" sensor is 16 mm on the diagonal, not 25.4 — the names are vidicon tube sizes

What matters practically is that a lens is rated for an image circle, and it must cover the sensor's diagonal. Put a 2/3" lens on a 1" sensor and the corners go dark — vignetting, because the image circle ran out before the silicon did. The other direction is fine but wasteful: you paid for glass you are cropping away.

Angle of view

Datasheets quote an angle rather than a field size, because the angle is a property of the lens and sensor alone:

ANGLE OF VIEW, AS A DATASHEET QUOTES ITAFOV=2 arctansensor2f

Note the assumption baked in: that formula puts the sensor exactly one focal length behind the lens, which is only true at infinity focus. At a finite working distance the sensor sits further back and the real angle is narrower than the datasheet's. LensCalc reports both, because the discrepancy surprises people who measure the field and find it smaller than the specification promised.

Pixels are not resolution

The most common mistake in specifying a vision system is treating megapixels as resolution. Resolution is millimetres per pixel at the target, and it depends on the field of view as much as the sensor:

SCALE AT THE TARGETmm/px=FOVpixels across

A 5 MP camera looking at a 60 mm field resolves roughly 25 microns per pixel. The same camera looking at a 600 mm field resolves 250 microns. Same sensor, ten times worse — because resolution is a property of the whole optical arrangement, not of the camera you bought.

Then the sampling question: how many pixels does a feature need? Nyquist gives the floor of two, but two pixels only tells you something is there.

1 px acrossALIASED2 px across — NyquistDETECTABLE4 px acrossMEASURABLESAMPLING A FEATUREthe circle is the same size in all three — only the pixel grid changestwo pixels says something is there; it takes four before you can measure it

The working rules, in the order they matter:

detect a feature exists        2 px   (Nyquist floor)
locate its edge reliably       3 px
measure it to a tolerance      4 px or more
read a character (OCR)         ~20 px tall

So the specification question is never "how many megapixels" — it is "what is the smallest thing I must resolve, how many pixels does it need, and what field of view must I cover?" Multiply those out and the required sensor falls out of the arithmetic.

A worked example

A 2/3" sensor, a 50 mm lens, a target 400 mm away:

2/3" sensor8.8 × 6.6 mm50 mmWD = 400 mm61.6× 46.2 mmWORKED EXAMPLE2/3" sensor · 50 mm lens · 400 mm working distance · m = 0.1429the shorthand FOV = sensor × WD / f would have said 70.4 mm — 14.3% too wide
m   = f / (WD - f)      = 50 / (400 - 50)   = 0.1429
FOV = sensor / m        = 8.8 / 0.1429      = 61.6 mm wide
                        = 6.6 / 0.1429      = 46.2 mm tall
si  = f x (1 + m)       = 50 x 1.1429       = 57.14 mm
ext = f x m             = 50 x 0.1429       = 7.14 mm  <- needs a spacer

with a 2448 x 2048 sensor:
pitch  = 8.8 / 2448 x 1000                  = 3.59 um
mm/px  = 61.6 / 2448                        = 0.0252 mm
3 px feature                                = 0.076 mm minimum

That extension figure is the one people trip over. A C-mount lens focused at infinity sits 17.526 mm from the sensor; at this working distance it needs to be 7.14 mm further out, which is beyond what most lens focus rings travel. Hence spacer rings — and hence the fact that fitting one changes your working distance, which changes your field of view, which is where the circular arguments in vision projects come from.

What the code does

The whole calculator is that one relationship, branched three ways:

if ($focalLength === null) {
    $magnification = $sensor->width / $fieldWidth;
    $focalLength   = $workingDistance * $magnification / ($magnification + 1);
} elseif ($workingDistance === null) {
    $magnification   = $sensor->width / $fieldWidth;
    $workingDistance = $focalLength * ($magnification + 1) / $magnification;
} else {
    $magnification = $focalLength / ($workingDistance - $focalLength);
    $fieldWidth    = $sensor->width / $magnification;
}

Thirty lines of arithmetic and a sensor table. The rest of the work was deciding which conventions to follow and testing the edges — that a /31-style special case exists here too, in the form of 1:1 macro geometry where the object sits at exactly twice the focal length and the extension equals a full focal length.

Caveats worth knowing

  • This is thin-lens maths. Real lenses have two principal planes separated by a distance the manufacturer rarely publishes, so working distance carries a few millimetres of uncertainty. For selecting a lens that is fine; for metrology, calibrate.
  • Telecentric lenses do not obey any of this. Their whole point is that magnification does not change with distance, which is why they cost what they cost.
  • Distortion is not modelled. A wide-angle lens will not map the field linearly to the sensor, and the error grows toward the corners.
  • Depth of field is a separate calculation and depends on aperture, which none of the above touches.

None of that stops the geometry being the right starting point. Get the field of view and the sampling right first; the rest is refinement.

LensCalc is here. Fill in any two.

#machine-vision #optics #sensors #lenscalc
CONTINUE? Questions about this one? Say hello →