# Estimating real world object distance from photo

I'm not really sure if this is possible but it should be.

I currently have a photo of a sphere, whose radius I've measures physically. Knowing the apparent size of the sphere from the photo and all internal camera parameters is it possible to estimate the distance from the camera to the sphere?

There is an equation, more used in lens imaging that is useful:

object_image_size = (real_object_size * lens_focal_length)/object distance

Problem is my object_image_size is in pixels and I need to convert it to a real world measure, preferable the size that actually showed up on the sensor.

I may be misunderstanding what you're asking, but if you know the sensor size and the number of pixels in the object, then you can calculate the object size. For example, a Canon 7D has an APS-C sensor that's 22.3 x 14.9mm and 5184 x 3456 pixels. That works out to ~0.0043mm/pixel. So if you have an object that's 250 pixels wide, then the projection of the object onto the sensor would have been 250 pixels * 0.0043mm/pixel = 1.075mm wide.

EDIT: So just for fun, I gave this a try. I took a 300mm ruler, put it at 1 meter from the camera and shot it with a 50mm lens on a Canon 7D. According to the ruler tool in Photoshop, it was 3734 pixels wide. That gives me the following equation:

3734 * 0.0043 = (300 * 50) / 1000

or

16.0562 = 15.0

Well, it's close, but not exact. (Over by 7%.) My guess is that the sensor site size is actually slightly smaller than 0.0043mm or the sensor size is slightly bigger than the reported size. In fact, the specs for the camera say 18MP effective size, but 19MP actual sensor size. So I'm not sure what that means, but I'm guessing it explains the difference between my actual result and the calculated result. (Either that or I'm misreading the length in Photoshop and it should actually be 3488 pixels instead of 3734.) But it was fun to try!

2nd EDIT: This question on the Photo Stack Exchange explains it. Camera lenses are often slightly longer or shorter than claimed, but rounded to the nearest nice focal length. So (assuming my calculations and measurements were correct) my lens is actually a ~53.5mm lens.

• Does that estimate the distance of the real world object? Jul 14, 2016 at 14:13
• I'm not sure. I was trying to interpret the equation you put up. But I'll give it a try with a ruler when I get home tonight and post the results here. Jul 14, 2016 at 15:55
• This was exactly what I wanted, a conversion system. A straight forward one from the sensor size is not good enough because cameras usually crop part of the image they receive. Thankfully though most cameras will have something called pixel size (in microns usually) which is exactly the conversion measure needed for this! Jul 14, 2016 at 19:15
• Focal length is also affected by your camera focus. so in the course of shooting the focal length varies slightly because that is how the focus is achieved, by moving the elements. This is why you use lots of measurements and a ransac on the image to estimae the actual values better. So not only is your effective value different from spec, its diffenet in each camera and in each shot. Also your ruler may not be perfectly aligned to camera and the camera.lense not being a perfect pinhole etc. Jul 24, 2016 at 19:44
• Maybe you can perform several experiments on your ruler to calibrate the parameters of your camera. Or just use samples to estimate parameters directly by regression. Jul 26, 2016 at 16:29

Yes, theoretically, for the sphere no for other things in image. If it is possible to augment the situation then it is possible to measure anything you can track much more reliably. First, knowing the scale of a object that you can successfully track is a requirement to getting a scale for the image as the cameras lost all scaling info. But this requirement alone is not enough.

In practice you do not know the other settings of a camera (unless calibrated with stereo geometry first), all kinds of problems arise when you do real camera tracking. Such as real cameras lens not being ideal. By tracking more than 2 features on the scene you can automatically estimate the actual focal distances and camera distortions. More importantly you can get an error estimate on your data.

One way is to have two photos from different angles in a scene with enough depth variation (for a stationary object this is quite easy). You can then use stereo geometry to solve the depth many such tracker apps exist as part special effects packages and making your own is not entirely out of the question (fun link, fundamental matrix song). The second way is to have a known plane, the plane can be solved by knowing 4 points and their relative locations. In reality it is better use more points for error and lens correction estimates. A third way is to use structured light (a cloud of laser light points, or a line for example) to triangulate the point distances. Or you could use a light field camera.