If I took a picture of a windmill on the horizon — given that I know the sensor size and the focal length of the lens and other factors to do with the shot — could I calculate how far away an object is from the photographer?
The only other factor you need is the height of the object in real life (otherwise you could be photographing a model which is much closer to the camera).
The maths isn't actually that complex, the ratio of the size of the object on the sensor and the size of the object in real life is the same as the ratio between the focal length and distance to the object.
To work out the size of the object on the sensor, work out it's height in pixels, divide by the image height in pixels and multiply by the physical height of the sensor.
So the whole sum is:
Let's sanity check this equation.
If we keep everything else constant and increase the focal length then the distance increases (as focal length is on the numerator). This is what you would expect, if you have to zoom your lens to make one object the size another equally sized object used to be, the first object must be further away.
If we keep everything else constant and increase the real height of the object then again the distance increases as if two objects of different real heights appear the same height in the image the taller one must be further away.
If we keep everything else constant and increase the image height, then the distance increases, as if two objects (of the same size, remember we're keeping everything else constant) appear the same pixel size in a cropped and uncropped image then the object in the uncropped image must be further away.
If we keep everything else constant and increase the object height in pixels then the distance decreases (we're on the denominator now): two equally sized objects, one takes up more pixels, it must be closer.
Finally if we keep everything else constant and increase sensor size, then distance decreases: two equally sized objects have the same height in pixels when shot with a compact (small sensor, where 20mm is a long lens) and shot with a DSLR (large sensor where 20mm is a wide lens), then the object in the DSLR image must be further away (because it appeared the same size but with a wide lens).
As noted @matt-grum, the most simple formula to estimate distance to the object is pinhole projection formula:
where x is the size of the object on the sensor, f is focal length of the lens, X is the size of the object, and d is distance from nodal point to the object. x and f, and X and d are measured in the same units, e.g. mm and m respetively (to calculate x you'll need to estimate pixel size for your sensor; for example, for Pentax K20D it is 23.4 mm/4672 px ≈ 5.008e-3 mm/px, i.e. an image 100 px long corresponds to x = 50.08e-3 mm).
In the following I assume that the size of the object (X) is unknown, and the only known parameters are x (image size) and f (focal length).
The problem is that we cannot tell from one photo if is a small object very close to the camera or a big object far away, because the depth of field in landscape shots is usually very big (and that's why pinhole formula is applicable).
To solve this problem we may use two or more images to measure the distance. Provided you can measure all angles and distance between two camera positions, you can also calculate distance to the remote object. But measuring all angles is not an easy task.
An easier approach is to take two photos which stay on the same line with the object, with object in the center of the image. Let distance to the object on the first photo be d₁, and image size be x₁:
Then if we move the camera s meters directly towards the object, then on the second photo we have image size x₂ slightly bigger than x₁:
Which gives us
Evidently, if s is not big enogh to affect image size significantly, you cannot estimate distance reliably, and need to use more complicated methods. The bigger is difference x₂ - x₁, the better.
Rather than trying to use formulas if you investigate the nautical methods of estimating distances which involve some basic " rules of thumb" for example if you are standing 1 foot above water hight you are 3 nautical miles from the horizon if you hold up your thumb at arms length them the object you look at is covered it is 100 ft high ( I think ) I have forgotten most of these these as I no longer use them but they do work and once learned and used regularly are remarkably accurate.
I know it's an old thread, but this question seems to come up now and then. FWIW, I added a calculator to compute an object distance in an image.
You will still have to know your values to make it work, one of which is approximate real height of the object. Discussed there.
protected by John Cavan♦ Feb 15 at 18:51
Thank you for your interest in this question.
Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.
Would you like to answer one of these unanswered questions instead?