# The problem

I'm trying to determine the size of an object using the focus distance determined by the autofocus of a smartphone camera (Galaxy Note 8). But I don't seem to be able to get an accurate value for the focus distance.

I hope to find the reason(s) why the focus distance cannot be accurately determined and possibly find a solution to make it more accurate.

# What I did so far

When focusing on close objects the focus distance is approximately equal to the object distance. So I took a photo of a know object (credit card sized card) at a known distance to calculate and compare all values. The picture I took is this one:

And these are the relevant values:

And then for the distance of the card to the camera:

Where the calculated distance is acquired using the equation from this topic:

distance = (4.3 * 85.6 * 4032) / (1688 * 5.645) = 156 mm

# Discussion

As seen these values are not the same while they should be. So I'm trying to figure out where this deviations stem from. Possible explanations:

• Focal length is not correct (see here), but since smartphone cameras have a fixed focal length I can't imagine this to be the problem
• The camera deviates too much from a pinhole camera model (camera is calibrated though, from here)
• The calibration of the autofocus distance isn't really accurate to begin with

# Solutions?

One solution I could think of is to calibrate the auto focus distance to a real distance myself. But would this need to be done for every device or will the calibration work across all smartphones of the same kind without recalibrating?

I hope you can help me in my thinking.

# Edit

I've done some more measurements, now with a checkerboard pattern. I've setup the camera at a known distance and measured the size of a checkerboard square and the focus distance given by the camera. These are the results:

As seen in the table, the focus distance given by the camera is actually surprisingly accurate (+/- 4% on distances < 170mm). The focal length given by the camera is however not accurate, I'd say the reason for this is that the camera moves it lens in order to get good focus. By calculating the focal length from the known size of the checkerboard pattern (see results table), it can be seen that the focal length indeed changes and the focal length given by the camera (4.3 mm) is thus highly likely the focal length at focus is infinity. For calculating the size of object this should be taken into account.

• The camera doesn't measure a distance to focus, it just changes the focus to maximize contrast. Given that the short focal-length lens gives it a very deep depth of field, this doesn't require much accuracy. Aug 28, 2018 at 10:33
• @xenoid I'm aware it just changes the focus to maximize contrast. But as this is directly linked to object distance you should be able to find the object distance from the focus distance, which is calibrated to real distances according to the docs. Furthermore when focusing on close objects the depth of field isn't that deep even for short focal-length lenses. From my measurements the accuracy is about 7mm on 15 cm object distance. Aug 29, 2018 at 2:38
• For a start redo your measurements with a high contrast pattern instead of a blank sheet. But still you assume that the camera focus exactly, which isn't likely not true with a contrast focus. It focuses enough. Aug 29, 2018 at 6:50
• I will try to do some more measurements with a checkerboard pattern. The photo isn't of a blank sheet though, there is a card on the corner which is used for the calculations but I blanked out the details for upload purposes. Aug 30, 2018 at 0:18
• Actually, focal length is a measurement taken when the camera is imaging a far distant object. In this condition, the distance lens to sensor is at minimum. As you image objects closer than infinity ∞, the lens is repositioned further away from the sensor. This elongated distance is sometimes incorrectly called the focal length, when in fact, it is now the back focus distance. It is the back focus distance that should substituted for the focal length. Aug 30, 2018 at 13:52

This is actually a somewhat simple ratio problem:

You can trace a triangle of image forming rays. Image triangle dimensions: Find the height of each pixel = 4.234 ÷ 3024 = 0.0014mm. Find the image height = 1059 X 0.0014 = 1.4827mm Image triangle two sides are height = focal length = 4.3mm Image triangle base = 1.4827mm Ratio is 1.4827 ÷ 4.3 = 0.3448

Object triangle height = lens to object = 169mm Height of object is 169 X 0.3448 = 58.27mm

The real height of object as measured is 53.49mm This answer is 92% accurate. The inaccuracy is due to some incorrect data such as focal length or image height.

• You are assuming the camera is accurately reporting the actual focus distance used. That's almost certainly a false assumption. Aug 28, 2018 at 21:13
• Your calculation of the pixel size is correct according to the specs of the sensor. As I don't think the image height is incorrect, it could be that the reported focal length is off. Aug 29, 2018 at 2:30
• See edit, the inaccuracy is indeed to incorrect focal length. Which changes with focus distance in a smartphone camera. Aug 30, 2018 at 7:47

You're using the wrong tool for the job. The camera module is designed to produce photographs, not measurements. To measure distance precisely, you should obtain a sensor made for that purpose.

• I understand it's not the perfect tool for the job, but due to restraints on the application it is not possible to use anything else than a smartphone. Sep 2, 2018 at 23:47