Skip to content

Instantly share code, notes, and snippets.

@nikolaseu
Created October 29, 2017 17:09
Show Gist options
  • Save nikolaseu/5e310238d0eb84b888c5d7662ae1d7d7 to your computer and use it in GitHub Desktop.
Save nikolaseu/5e310238d0eb84b888c5d7662ae1d7d7 to your computer and use it in GitHub Desktop.
Lens focal length calculator
% required dimensions
standoff = 500; % distance from camera, in [mm]
fovWidth = 320; % width of field of view, in [mm]
fovHeight = 10; % height of FOV, in [mm]
% sensor resolution
sensorXRes = 4096; % widht
sensorYRes = 3072; % height
% pixel physical size
pixelWidth = 5.5 / 1000; % 5.5 [um] but everything has to be in [mm], so /1000
pixelHeight = 5.5 / 1000;
% sensor physical size
sensorWidth = sensorXRes * pixelWidth;
sensorHeight = sensorYRes * pixelHeight;
% diagonal size
sensorDiagSize = sqrt(sensorWidth ^ 2 + sensorHeight ^ 2);
sensorDiagSizeInch = sensorDiagSize / 25.4;
fprintf('Sensor diagonal size: %gmm, %g"\n', ...
sensorDiagSize, ...
sensorDiagSizeInch);
% aspect ratios
sensorAspectRatio = sensorWidth / sensorHeight;
fovAspectRatio = fovWidth / fovHeight;
if fovAspectRatio > sensorAspectRatio,
% limited by width
halfAngle = 180/pi * atan2( (fovWidth/2), standoff);
maxFocalDistance = (sensorWidth / 2) / tand(halfAngle);
else
% limited by height
halfAngle = 180/pi * atan2( (fovHeight/2), standoff);
maxFocalDistance = (sensorHeight / 2) / tand(halfAngle);
end
fprintf('The maximum focal distance to cover the fov is %gmm\nFan angle approx %g∞\n', ...
maxFocalDistance, ...
2*halfAngle)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment