How do I calculate the distance of an object in a photo?
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?
Yes, you probably can with a bit of complex math, but it's beyond me. If it's on the horizon you can bypass all that and just multiply the height of your eyes (in feet, including the height of any land or building you're on) by 1.5 then find the square root of that, which gives you the (approximate) distance to the horizon in miles
My Canon 100mm f/2.8 IS USM (and presumably some other lenses) record the subject distance within the EXIF data, it might be worth investigating if your equipment does this before you enter into any complicated maths!
@Chris The focussing distance will probably just say "infinity" which isn't all that useful!
@Matt Grum I won't pretend to know how it works, I suppose at large enough distances it might struggle to figure it out. But at least with this lens I've got a variety of shots in which the EXIF data specifies distances down to a centimetre precision: http://www.flickr.com/photos/cfletcher86/5744389335/meta/in/photostream
@Chris the camera "knows" the distance from the AF system, and as you suggest it becomes very inaccurate over large distances (but still accurate enough for the image to be in focus!). Also just because the EXIF specifies the distance down to a centimetre, doesn't mean the figure is accurate down to a centimetre!
@ElendilTheTall working out distance to the horizon is actually more difficult than working out the distance to the object!
Matt: As far as I know that information is sent back from the lens (which knows the focusing distance, approximately) for E-TTL2. It only works with Canon lenses with USM, though. Chris: The number being in centimeters tells you nothing about its accuracy.
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).
So, in other words, "no, not without knowing the size of the object in real life". Otherwise, you've got two unknown factors. The windmill could be a model that's closer than you think.
@mattdm exactly, was just about to make that clear in the answer. You could also be photographing a photograph of a windmill etc.
@matt-grum My point is that we need one of the following: 1) either the real object size; 2) or two or more images.
@jetxee yeah I get that now, from your comment it wasn't clear, as I had stated object size to be one of the knowns in my answer