# How do I choose a lens based on object size and distance?

#### bmihura

##### TPF Noob!
How do I choose a C mount camera lens that will fit a 17 mm - long wire into its view, horizontally, at a distance of roughly 70 mm? My image sensor has a 7.564 mm width.

#### weepete

##### TPF Supporters
Supporting Member
What camera are you using? looks like an odd sensor size...

#### Strodav

##### No longer a newbie, moving up!
You have the sensor size, and can calculate the needed Field of View (FOV) given the distance from the subject and size of the subject. You can calculate the focal length from this information. Here is a paper from Dan Carr with the equations you can use to do the calculation: How To Calculate Field of View In Photography

OP
B

#### bmihura

##### TPF Noob!
Perfect. I had to rearrange the equation to solve for focal length; algebra class finally paid off. Here's the Python code in case anybody finds it useful:

import math

sensorWidth = 7.564
angleInDegrees = 6
focalLength = sensorWidth / (2 * math.tan(angleInDegrees * math.pi / 360))
print(f'focal length is {focalLength}')

• jcdeboever and weepete
OP
B

#### bmihura

##### TPF Noob!
Here is Python code that goes in the other direction and does what your article mentioned, plus it calculates the angle for you assuming you know the size of the object and you want to know the camera distance. If you don't have Python on your machine, lots of "online Python" websites let you copy and paste this and it will run.

import math

sensorWidth = 7.564 # millimeters; the sensor in the Raspberry Pi camera
focalLength = 50 # millimeters; largest common focal length-- we want a zoom lens
wireLength = 0.7 # inches
angleInRadians = 2 * math.atan(sensorWidth / (2 * focalLength))
angleInDegrees = angleInRadians * (180 / math.pi)

print(f'camera distance is {cameraDistance:.4} inches, field of view {angleInDegrees:.4} degrees')

• jcdeboever

#### weepete

##### TPF Supporters
Supporting Member
Interesting, I'm glad someone could help.

You've also peaked my curiosity, would you mind me asking what it's for?

If you are using a rasberry pi and programming in python, it looks like you've got some kind of project. Most of us use cameras slightly differently to your use, and though field of view may be possible you may need to use a lens that has a short minimal focal distance in order to obtain focus. Not all lenses can do.

So there may be other considerations for your use case.