# Is it possible to measure distance to an object by adjusting the focus?

This is maybe not a 100% on topic photography, but I couldn't find a better stackexchange site to post this on and maybe you guys can help me out.

I have a camera that takes pictures of a production process. The objects are all pretty close by, so nothing should be in hyperfocal distance. We have an autofocus that focuses on the object. Is it possible to calculate the distance from the camera by looking at the focus position. How precise would that be. It's a passive focus. I couldn't really find any material on this. Is this done at all? Does anybody know of some references for that problem? Is this a good or bad idea?

-
Interesting question - could you fill in a bit more detail please - e.g. specific camera, lens, etc. – user7226 Dec 7 '11 at 14:48
Right now we use an OV5642 image sensor from OmniVision. But that is not important, we would buy whatever works the best. As you can tell I am not an expert on cameras. I just program the image processing software backend. It was just an idea I had. We use lasers for triangulation, but often have problems with reflective surfaces. So we need some sort of passive way to estimate the distance. The other alternative is of course with two cameras. But I am interested if there are ways with just one by adjusting the focus. – Lucas Dec 7 '11 at 14:54
Are you trying to distinguish between 10 inches away from an object 11 inches away, or an object 2 feet away and an object 10 feet away? – Paul Cezanne Dec 7 '11 at 17:40
If you guys are currently online, it might be more useful to move to chat and discuss the options and tests real time. – jrista Dec 7 '11 at 22:11
Little bit of background reading: en.wikipedia.org/wiki/Rangefinder_camera – user7226 Dec 8 '11 at 8:21

## 2 Answers

It is possible to estimate the distance to the subject using the auto focus. But to do this you need a camera model of your whole camera system, body and lens. You would most likely have to model this yourself, which will take quite some time and effort. The accuracy would likely be very different for different systems. Your model would probably contribute to the (in)accuracy more than anything else. With a very good model it would be the auto focus that dictated the accuracy.

It sounds like you might benefit from looking at the Kinect system from Microsoft, and all the software available for this on line. The hardware is cheap and it is fairly good.

-

There is an EXIF field named "Subject Distance." If you use a camera that populates that field (and can be controlled by your controller software), you could just use the EXIF data from the picture.

I just looked at two images from my old Canon SX10IS that have values in that field, and the picture where the subject was closer had a smaller number: so based on a sample size of two, that feature seems to be available and work. However, I looked at a larger set of photos, taken with the same camera but by someone else, and I can't see any correlation between the Subject Distance value and apparent focus distance.

I imagine you'd need a full camera, not just an image sensor, though. Also, I don't know how to find a list of cameras that populate that field.

-
Thank you for your response. Do you have any idea how the camera is able to populate this field? How do they measure the distance and how precise do they measure the distance? – Lucas Dec 7 '11 at 19:44
I would assume the camera just reads the focus distance setting from the lens (an electronic version of you looking at the distance window markings on the lens). I really have no idea how it works internally or how accurate it is. – drewbenn Dec 7 '11 at 19:54