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 – ElendilTheTall May 26 '11 at 10:03
-
2My 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! – ChrisFletcher May 26 '11 at 10:34
-
@ChrisFletcher - wow -Iv never looked for that in the EXIF, that would be cool – Rob May 26 '11 at 10:51
-
2@Chris The focussing distance will probably just say "infinity" which isn't all that useful! – Matt Grum May 26 '11 at 11:27
-
@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 – ChrisFletcher May 26 '11 at 11:43
-
1@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! – Matt Grum May 26 '11 at 12:16
-
@ElendilTheTall working out distance to the horizon is actually more difficult than working out the distance to the object! – Matt Grum May 26 '11 at 12:18
-
1Matt: 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. – Joey May 26 '11 at 16:07
-
If you're only interested in the result, and not the math itself, some cameras record the subject's distance in EXIF information. – Fer Mar 04 '14 at 19:22
5 Answers
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:
$$ \textrm{Distance to object}(mm) = \frac{f(mm)\,\times\, \textrm{real height}(mm)\,\times\, \textrm{image height}(pixels)} {\textrm{object height}(pixels)\,\times\, \textrm{sensor height}(mm)} $$
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).
-
2So, 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 May 26 '11 at 12:14
-
2@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 May 26 '11 at 12:19
-
@matt-grum My point is that we need one of the following: 1) either the real object size; 2) or two or more images. – sastanin May 26 '11 at 13:13
-
@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 – Matt Grum May 26 '11 at 13:23
-
Reminds me of the classic Father Ted line about some cows being small and others being far away – osullic Apr 26 '22 at 20:25
-
Just FYI: this formula will work for windmills and other relatively large objects, but will be inaccurate for macro photography. A more accurate formula would be (ObjectHeight + ProjectionHeight) * FocalLength / ProjectionHeight, where ProjectionHeight is PixelObjectHeight * SensorHeight / PixelImageHeight. This assumes that the object is in focus, otherwise it gets more complex. When ObjectHeight is much larger than ProjectionHeight (in other words, when magnification is small), you can ignore the "+ Projection Height" in the parentheses and the formula simplifies to the one in the answer. – relatively_random Jun 10 '22 at 08:10
As noted @matt-grum, the most simple formula to estimate distance to the object is pinhole projection formula:
$$ \frac{x}{f} = \frac{X}{d} $$
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 respectively (to calculate \$x\$ you'll need to estimate pixel size for your sensor; for example, for Pentax K20D it is \$\textrm{23.4 mm / 4672 px} \approx 5.008\times10^{-3}\,\textrm{mm/px}\$, i.e. an image 100 px long corresponds to \$x = 50.08\,\times10^{-3}\,\textrm{mm}\$).
In the following I assume that the size of the object \$X\$ is unknown, and the only known parameters are image size \$x\$ and focal length \$f\$.
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_1\$, and image size be \$x_1\$:
$$ \frac{x_1}{f} = \frac{X}{d_1} $$
Then if we move the camera \$s\$ meters directly towards the object, then on the second photo we have image size \$x_2\$ slightly bigger than \$x_1\$:
$$ \frac{x_2}{f} = \frac{X}{d_1 - s} $$
Which gives us
$$ d_1 = s\cdot\frac{x_2}{x_2-x_1} $$
Evidently, if \$s\$ is not big enough to affect image size significantly, you cannot estimate distance reliably, and need to use more complicated methods. The bigger the difference \$x_2-x_1\$ is, the better.
-
ahh I get what you meant by your comment now, I was assuming object size was known, otherwise it gets way more complicated, as you need not only at least two cameras, but the cameras must be calibrated – Matt Grum May 26 '11 at 13:22
-
@sastanin I am trying your formula but I couldn't get it to work. I made a page here http://photo.stackexchange.com/questions/80195/calculation-for-getting-dimension-of-object-in-image-not-working – omega Jul 16 '16 at 00:57
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.
http://www.scantips.com/lights/subjectdistance.html
You will still have to know your values to make it work, one of which is approximate real height of the object. Discussed there.
- 12,879
- 1
- 16
- 30
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.
- 163
- 5
-
This isn't quite the nautical rule of thumb. The horizon is 3 miles away at 6' above sea level, not 1'. The rule of thumb is: "multiply your height, in feet, by 1.5. The square root of that number is the distance to the horizon, in miles." (i.e., (6' * 1.5)^0.5 = 3 miles). – Larry OBrien Mar 30 '20 at 21:09
Simple answer - no. You have two variables and only one equation.
- 27
- 1
-
2
-
-
you can assume the this implicit in the question, and the answer requires figuring out extra equation – Error Jun 01 '19 at 10:09