24 min read · Machine vision

A vendor offers you a 5-megapixel camera, and you are not sure you need it, or whether a 1.3-megapixel camera would do. The other way it goes: you buy a camera and lens, mount them, and find the visible area does not fit the part, so you move the camera back until the small defect disappears from the image. The answer is not in the megapixel number. It is in the smallest defect you must see, the area you must cover, the distance you can mount at, and the speed of the part.
This article works backward from the defect you must see to the camera and lens. It uses a Python script with only the standard library, calculates three cases, has a test suite, and shows real output from a run. Managers can follow the explanations and tables. Engineers can run the code themselves.
The real problem: a camera that is too big, too small, or blurred when the part moves
Choosing a camera and lens can go wrong in three ways: you pay more than needed, you buy too small and cannot catch the defect, or you pick the right size but the image blurs because the part moves faster than the shutter can follow. Consider three hypothetical situations. They are not BT San customer projects.
- A bottling plant must check that the label is applied and the text is complete. The smallest gap to see is about 0.8 mm over an area of about 90 x 70 mm. A vendor offers a 20-megapixel camera, which costs far more than this job needs.
- A sheet-metal plant must catch scratches 0.1 mm wide on a 100 x 100 mm plate. The team tries a 5-megapixel camera and finds the scratch fades out of the image, because the pixel scale is too coarse.
- A quality control room must measure a part 20 mm wide at a tolerance of ±0.05 mm. The team uses an ordinary lens, and the result changes when the part sits slightly higher or lower, because an ordinary lens sees near objects larger than far ones.
All three can be calculated before buying with a few lines of formulas. But those formulas are approximations, so you need to know what they approximate and where to check the datasheet.
What the documents say, and what this article assumes
| Topic | What the documents say | Effect on the calculation |
|---|---|---|
| Lens formula | vision-doctor uses a single principal-plane model: FOV y = y' x (a / f' - 1) and f' = a / (y / y' + 1), where y' is the sensor size, a is the object distance and f' is the focal length. It states these are approximations. The distance is measured to the principal plane of the lens, not the front edge of the lens, which is usually closer to the part by about 2 to 3 cm. | These formulas are used in focal_length and fov_at. Allow for the real mounting distance. |
| Pixels per defect | vision-doctor states that the smallest feature should be at least 3 to 4 pixels wide to be detected cleanly. With high contrast it can be smaller, but with low contrast the result is uncertain. | Use 3 pixels as the starting value and adjust to the contrast of the real defect. |
| Sensor size | Wikipedia states that the 1/n inch name comes from the size of old television camera tubes and does not match the actual size. Examples in the table: 1/2 inch 6.4 x 4.8 mm, 2/3 inch 8.8 x 6.6 mm, 1 inch 13.2 x 8.8 mm. | Calculate the sensor size from pixel count x pixel size in the datasheet, not from the inch name. |
| Real example camera | The Basler acA2440-75um page lists 2448 x 2048 pixels (default), 3.45 x 3.45 um pixels, a Sony IMX250 sensor in 2/3 inch format, global shutter, 75 fps. | Used as camera A in the example. |
| Depth of field | vision-doctor states that with a fixed camera setup, one aperture setting adjusts the depth of field. Closing the aperture increases depth of field but requires a longer exposure time. Blur becomes visible when the blur circle is larger than one pixel. The formula is an approximation, and lens aberrations can add about one more pixel of blur. | Use the hyperfocal formula in depth_of_field. |
| Telecentric lens | vision-doctor states that it gives constant magnification over a defined range. The image has no perspective error, so it can measure even when the distance changes, but it does not inspect three-dimensional structures such as hole walls. Most are telecentric on the object side only. | Used for dimensional measurement. See Case 3. |
Values set by the author in the example, not product data: camera B (1280 x 1024 pixels, 5.3 um), camera C (5472 x 3648 pixels, 2.4 um), the lens list and the min_pixel_um value stating the smallest pixel each lens supports, and the magnifications of the telecentric lenses. All are sample values to demonstrate the method. The author could not find data for those real models before writing.
Overview of the example
The code is in the folder examples/machine-vision-lens-resolution-fov/. It uses Python 3 with the standard library. There is nothing to install and it does not use the network.
| File | Purpose |
|---|---|
| lens_fov.py | All the calculation functions: FOV, pixel count, focal length, working distance, depth of field, motion blur, and the camera-and-lens combination checker |
| demo.py | Three cases: a bottle label, scratches on sheet metal, and dimensional measurement with a telecentric lens |
| test_lens_fov.py | 6 unittest tests, including the numeric example from the vision-doctor documentation |
| output.txt | Real output of demo.py |
Step 1: Start from the smallest defect, not from the camera
The order of thinking is this. Find the area you must cover (FOV: the width of the scene the camera sees), wider than the part to allow for it sitting off-center. Then work out how many pixels you need across that width so that the smallest defect covers at least 3 pixels. Example from the documentation: a 64 mm part on a 640-pixel sensor gives 0.1 mm per pixel, so a clearly visible defect must be about 0.3 mm, not 0.1 mm.
def required_fov(part: float, margin_each_side: float) -> float:
"""FOV must be larger than the part, to allow for position error."""
return part + 2 * margin_each_side
def required_pixels(fov: float, defect: float, px_per_defect: float) -> int:
"""Pixels needed across the FOV so the smallest defect covers px_per_defect pixels."""
return ceil(fov / defect * px_per_defect)
def focal_length(sensor_len: float, wd: float, fov: float) -> float:
"""f = a / (y/y' + 1)."""
return wd / (fov / sensor_len + 1)
def fov_at(sensor_len: float, f: float, wd: float) -> float:
"""y = y' * (a/f - 1)."""
return sensor_len * (wd / f - 1)
def working_distance(sensor_len: float, f: float, fov: float) -> float:
"""a = f * (y/y' + 1)."""
return f * (fov / sensor_len + 1)
def mm_per_pixel(fov: float, pixels: int) -> float:
return fov / pixels
No camera name appears here yet. The results are the coarsest pixel scale you can accept (mm per pixel) and the minimum number of pixels across the width. These are the criteria to compare against a quotation. If a vendor offers a camera with fewer pixels than this, ask whether you will view a smaller area or use several cameras instead.
Step 2: Focal length and working distance
Once you know the FOV and the distance you can mount the camera at (working distance), the formula gives the focal length you need. Conversely, if you pick standard lenses of 8, 12, 16, 25, 35 and 50 mm, you can calculate the FOV each gives at that distance. It almost never fits exactly. Pick the one whose FOV is no smaller than needed, and adjust the mounting distance slightly.
Remember that as the sensor gets larger, the lens must have an image circle big enough to cover it. Otherwise the corners of the image are dark or blurred. This example does not check that. Look in the lens datasheet (see the section on cautions).
Step 3: The lens must resolve as finely as the pixel
A camera with smaller pixels does not always give a sharper image. If the lens is not fine enough, each pixel just records a blurred image at higher resolution. A sensor with 3.45 um pixels represents about 145 line pairs per mm at the sensor plane (1000 / (2 x 3.45)), and 2.4 um pixels about 208 line pairs per mm. The lens must deliver usable contrast at that frequency. In the code this is represented by the lens's min_pixel_um value, which is a sample value set by the author. For real work, read the MTF chart in the lens datasheet at that frequency and at your working distance.
Step 4: A camera-and-lens combination checker
def check_combo(cam: Camera, lens: Lens, wd: float, fov_w: float, fov_h: float,
defect: float, ppd: float) -> dict:
fw = fov_at(cam.sensor_w, lens.f, wd)
fh = fov_at(cam.sensor_h, lens.f, wd)
mpp = fw / cam.px_w
defect_px = defect / mpp
reasons = []
if fw < fov_w or fh < fov_h:
reasons.append("FOV small")
if defect_px < ppd:
reasons.append("defect px low")
if cam.pixel_um < lens.min_pixel_um:
reasons.append("lens too coarse")
return {"cam": cam.name, "lens": lens.name, "fov_w": fw, "fov_h": fh,
"mm_px": mpp, "defect_px": defect_px, "ok": not reasons, "why": ", ".join(reasons)}This function takes one camera and one lens at a given working distance. It calculates the real FOV on both axes, the pixel scale and the number of pixels the defect gets, and then gives the reason for any failure, such as FOV too small, too few pixels per defect, or a lens coarser than the pixel.
Step 5: Depth of field
Depth of field is the range of distances over which the image stays sharp enough. It increases when you close the aperture (higher f-number), use a shorter focal length, or view from farther away. But closing the aperture needs more light or a longer exposure time, and if you close it too far, diffraction makes the image unsharp by itself. The formula in the file uses a blur circle c equal to two pixels, as the documentation recommends.
def depth_of_field(f: float, n: float, c: float, a: float) -> tuple[float, float, float]:
"""Near, far, total DoF (mm) from the hyperfocal formulas. c = blur circle, often ~2 pixels."""
h = f * f / (n * c) + f
near = a * (h - f) / (h + a - 2 * f)
far = a * (h - f) / (h - a) if a < h else float("inf")
return near, far, far - nearIf parts vary in height, or sit on a belt that wobbles, the depth of field must cover that range. If it does not, close the aperture and add light, or switch to a telecentric lens for measurement work.
Step 6: Motion blur from the conveyor
While the shutter is open, the part moves a distance equal to its speed times the exposure time. If that distance is more than one pixel, the image blurs. This is plain geometry. The author derived this formula and does not cite any web page for it. For global versus rolling shutters, see the article on global shutters and conveyors.
def motion_blur_px(speed: float, exposure: float, mm_px: float) -> float:
"""Distance travelled during exposure, expressed in pixels."""
return speed * exposure / mm_px
def max_exposure(speed: float, mm_px: float, max_blur_px: float = 1.0) -> float:
return max_blur_px * mm_px / speed
A shorter exposure time needs more light, which is why strobes or very bright lighting matter for conveyor work.
Step 7: Telecentric lenses for measurement
An ordinary lens sees near objects larger than far ones. Parts whose heights differ by 1 mm therefore measure differently. A telecentric lens has constant magnification over a range of distances and no perspective error, so it suits dimensional measurement. The FOV is fixed at the sensor size divided by the magnification and does not depend on distance. But the lens is large and heavy, and its FOV is limited by the size of the lens body, not by distance.
def telecentric_fov(sensor_len: float, magnification: float) -> float:
"""A telecentric lens has fixed magnification: FOV does not depend on distance."""
return sensor_len / magnification
A real run: three cases
demo.py calls the functions above on three cases. The output below is from a real run on 2026-11-21.
def case(title, part_w, part_h, margin, defect, ppd, wd, speed, exposure):
fw, fh = required_fov(part_w, margin), required_fov(part_h, margin)
px = required_pixels(fw, defect, ppd)
mpp = fw / px
print(f"== {title}")
print(f"FOV needed {fw:.1f} x {fh:.1f} mm, defect {defect} mm x {ppd} px -> {px} px across, <= {fw / px:.4f} mm/px")
for cam in CAMS:
f = focal_length(cam.sensor_w, wd, fw)
print(f" {cam.name}: f = {f:.1f} mm at WD {wd} mm, sensor {cam.sensor_w:.2f} mm, Nyquist {nyquist_lp_mm(cam.pixel_um):.0f} lp/mm")
for cam in CAMS:
for lens in LENSES:
r = check_combo(cam, lens, wd, fw, fh, defect, ppd)
if r["ok"]:
print(f" OK {r['cam'][:2]}+{r['lens']}: FOV {r['fov_w']:.1f}x{r['fov_h']:.1f}, {r['mm_px']:.4f} mm/px, defect {r['defect_px']:.1f} px")
oks = [(check_combo(c, l, wd, fw, fh, defect, ppd), c, l) for c in CAMS for l in LENSES]
oks = [t for t in oks if t[0]["ok"]]
if not oks:
print(" no combo passes: use more pixels (line scan / several cameras) or a smaller FOV\n")
return
r, cam, lens = max(oks, key=lambda t: t[0]["mm_px"]) # least demanding passing combo
mp = r["mm_px"]
print(f" chosen for the next checks: {r['cam'][:2]}+{lens.name}, {mp:.4f} mm/px")
print(f" motion: {speed} mm/s, exposure {exposure * 1e6:.0f} us -> blur {motion_blur_px(speed, exposure, mp):.2f} px; "
f"exposure for <=1 px: {max_exposure(speed, mp) * 1e6:.0f} us")
n, c = 4.0, 2 * cam.pixel_um / 1000
near, far, tot = depth_of_field(lens.f, n, c, wd)
print(f" DoF with {lens.f:g} mm lens f/{n:.0f}, blur circle {c * 1000:.1f} um: {near:.0f}-{far:.0f} mm, total {tot:.1f} mm\n")
== 1 bottle label check
FOV needed 90.0 x 70.0 mm, defect 0.8 mm x 3 px -> 338 px across, <= 0.2663 mm/px
A 2448x2048 3.45um (Basler acA2440-75um class): f = 34.3 mm at WD 400 mm, sensor 8.45 mm, Nyquist 145 lp/mm
B 1280x1024 5.3um (example): f = 28.0 mm at WD 400 mm, sensor 6.78 mm, Nyquist 94 lp/mm
C 5472x3648 2.4um (example 20MP): f = 50.9 mm at WD 400 mm, sensor 13.13 mm, Nyquist 208 lp/mm
OK A +12mm (rated >=3.45um): FOV 273.1x228.5, 0.1116 mm/px, defect 7.2 px
OK A +16mm (rated >=3.45um): FOV 202.7x169.6, 0.0828 mm/px, defect 9.7 px
OK A +25mm (rated >=3.45um): FOV 126.7x106.0, 0.0518 mm/px, defect 15.5 px
OK B +8mm (rated >=5.0um): FOV 332.4x265.9, 0.2597 mm/px, defect 3.1 px
OK B +12mm (rated >=3.45um): FOV 219.3x175.5, 0.1714 mm/px, defect 4.7 px
OK B +16mm (rated >=3.45um): FOV 162.8x130.3, 0.1272 mm/px, defect 6.3 px
OK B +25mm (rated >=3.45um): FOV 101.8x81.4, 0.0795 mm/px, defect 10.1 px
OK C +35mm (rated >=2.2um): FOV 137.0x91.3, 0.0250 mm/px, defect 32.0 px
chosen for the next checks: B +8mm (rated >=5.0um), 0.2597 mm/px
motion: 500 mm/s, exposure 200 us -> blur 0.39 px; exposure for <=1 px: 519 us
DoF with 8 mm lens f/4, blur circle 10.6 um: 318-540 mm, total 222.8 mm
== 2 scratch on metal plate
FOV needed 106.0 x 106.0 mm, defect 0.1 mm x 3 px -> 3180 px across, <= 0.0333 mm/px
A 2448x2048 3.45um (Basler acA2440-75um class): f = 36.9 mm at WD 500 mm, sensor 8.45 mm, Nyquist 145 lp/mm
B 1280x1024 5.3um (example): f = 30.1 mm at WD 500 mm, sensor 6.78 mm, Nyquist 94 lp/mm
C 5472x3648 2.4um (example 20MP): f = 55.1 mm at WD 500 mm, sensor 13.13 mm, Nyquist 208 lp/mm
OK C +35mm (rated >=2.2um): FOV 174.5x116.3, 0.0319 mm/px, defect 3.1 px
chosen for the next checks: C +35mm (rated >=2.2um), 0.0319 mm/px
motion: 200 mm/s, exposure 100 us -> blur 0.63 px; exposure for <=1 px: 159 us
DoF with 35 mm lens f/4, blur circle 4.8 um: 496-504 mm, total 7.3 mm
== 3 dimension check, telecentric lens
tolerance band 0.1 mm -> step 0.01 mm -> with sub-pixel 0.25 px, need <= 0.040 mm/px
0.25x on camera A: FOV 33.8 mm wide, 0.0138 mm/px, OK for a 20 mm part + margin 2 mm each side
0.3x on camera A: FOV 28.2 mm wide, 0.0115 mm/px, OK for a 20 mm part + margin 2 mm each side
0.5x on camera A: FOV 16.9 mm wide, 0.0069 mm/px, no for a 20 mm part + margin 2 mm each side
1.0x on camera A: FOV 8.4 mm wide, 0.0035 mm/px, no for a 20 mm part + margin 2 mm each sideReading the results case by case
- Bottle label: the pixel scale must be no coarser than 0.27 mm per pixel. A small camera passes (camera B + 8 mm lens gives a defect of 3.1 pixels, just at the limit). Camera A or C gives a safer margin, and a 20-megapixel camera is not needed for this job. Motion blur from a 500 mm/s conveyor at 200 us is 0.39 pixels in the chosen setup.
- Sheet metal: a 0.1 mm defect x 3 pixels needs 3180 pixels across 106 mm. Camera A (2448 pixels) and B are not enough. Only camera C (20 megapixels) with the 35 mm lens passes, and only narrowly (3.1 pixels). The options are two cameras, line scanning, or viewing a smaller area and moving the part. Depth of field is only 7.3 mm at f/4, so the plate must be very flat.
- Dimensional measurement: a tolerance band of 0.1 mm divided by ten gives a step of 0.01 mm. If you believe you can locate the edge to 0.25 pixel (which must be proven by repeated measurement), the scale must be no coarser than 0.04 mm per pixel. Telecentric lenses of 0.25x and 0.3x pass on both scale and FOV. The 0.5x and 1.0x lenses have a very good scale, but the FOV is too narrow for a 20 mm part plus margin.
The ten-to-one rule (measurement resolution should be about ten times finer than the tolerance band) and the 0.25-pixel value are assumptions by the author, not confirmed from the cited web pages. The real value must come from repeated measurements on real parts (gauge R&R).
Test suite
test_lens_fov.py runs with python3 -m unittest test_lens_fov. It has 6 tests, and all pass. The first compares against the numeric example on the vision-doctor page (focal length 12.3 mm and working distance 350 mm). The others check that the FOV and focal length formulas are inverses of each other, the pixel count, motion blur, depth of field increasing as the f-number rises, and the FOV of a telecentric lens. If someone edits a formula and breaks it, the tests fail.
Practical guidelines
- Lighting matters more than the megapixel count. A low-contrast defect is not helped by more pixels. The article on choosing lighting explains this, and you should choose lighting before deciding on resolution.
- Use a calibration target (for example a dot plate or a checkerboard of known size) to measure the real mm-per-pixel scale. Do not trust the formula alone, because lenses have distortion and the distance is measured from the principal plane, not the lens edge.
- Control vibration: a camera stand that vibrates slightly blurs the image as much as a moving part. Check with a still image at a short exposure compared with a long one.
- Test with real parts and the smallest defect you will accept, including parts that are somewhat poor, before ordering the whole set. Borrow a trial camera and lens from the vendor if you can.
- Capture real images at the real mounting distance and measure the number of pixels across the defect. Do not trust the numbers in the quotation alone.
- If you must measure, set the scale from the tolerance band, not from the defect size.
Hypothetical examples: applying this to three businesses
Food or beverage plant: checking labels and caps
The inspection area is large, the defects are not very small, and the conveyor is fast. These jobs usually end up with a mid-size camera, a lens of medium focal length, and a short strobe. Check the depth of field, because a round bottle puts the label surface at different distances from the camera.
Metal or injection-molded plastic plant: checking surface defects
Small defects over a large area need many pixels. Consider splitting the view into several images, using a line-scan camera over the conveyor, or capturing only the risky regions. That costs less than buying the camera with the most pixels.
Dimensional measurement of high-precision parts
A telecentric lens with backlight is the standard set for silhouette measurement, because the edges are sharp and do not depend on distance. But you need a calibration procedure and a repeatability study before using it to reject parts.
Common mistakes and a checklist
- Choosing a camera by megapixel count without calculating from the defect and the FOV.
- Setting the FOV to fit the part exactly, with no allowance for off-center placement.
- Thinking one pixel per defect is enough, when the documentation recommends at least 3 to 4.
- Buying a camera with very small pixels and a lens that cannot resolve that detail.
- Measuring the working distance from the front edge of the lens, when the formula counts from the principal plane of the lens.
- Using an ordinary lens to measure objects of different heights.
- Not calculating motion blur from the conveyor speed and the exposure time.
- Not checking that the lens image circle covers the sensor.
- Write down the smallest defect to catch and the area to cover, with an allowance for off-center placement.
- Calculate the minimum pixels and the maximum mm-per-pixel scale.
- Choose a mounting distance you can really achieve, then find the focal length.
- Check the datasheet: sensor size, image circle and the lens MTF at the pixel frequency.
- Work out the depth of field you need, then check it against the f-number you can use.
- Work out motion blur from the conveyor speed, set the maximum exposure time and provide enough light.
- Capture real images with a calibration target, compare them with the calculation, and then test with real parts.
Cautions for this example: it uses a thin-lens approximation, ignores lens distortion, does not check the lens image circle against the sensor, and ignores diffraction at small apertures. Camera B and C, every lens and the telecentric magnifications are sample values. Use it as a learning exercise, not a purchasing tool. Check against datasheets and test with real parts before deciding.
References
- vision-doctor: Optic basics, working distance, field of view, magnification (formulas, examples) (accessed 2026-11-21)
- vision-doctor: Resolution of sensors (3 to 4 pixels per smallest feature) (accessed 2026-11-21)
- vision-doctor: Image sharpness, depth of field (formulas, aperture, blur circle) (accessed 2026-11-21)
- vision-doctor: Telecentric lenses (constant magnification, no perspective error, limits) (accessed 2026-11-21)
- Basler docs: acA2440-75um (2448 x 2048, 3.45 um, 2/3 inch, global shutter) (accessed 2026-11-21)
- Wikipedia: Image sensor format (optical format vs actual size in mm) (accessed 2026-11-21)
- Wikipedia: Hyperfocal distance (H = f^2/(N c) + f, near and far limits) (accessed 2026-11-21)




