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:
- Sensor type: SLSI_S5K2L2_FIMC_IS
- f = 4.3 mm (from CaptureResult.LENS_FOCAL_LENGTH)
- Physical sensor size = 5.645 x 4.234 mm (from CameraCharacteristics.SENSOR_INFO_PHYSICAL_SIZE)
- Pixel array size = 4032 x 3024 px (from CameraCharacteristics.SENSOR_INFO_PIXEL_ARRAY_SIZE)
- Credit card size = 85.6 x 53.98 mm
- Image size = 4032 x 3024 px (note image above is rescaled to include here)
- Credit card size in picture = 1688 x 1059 px
And then for the distance of the card to the camera:
- Real measured distance: 169 mm
- Autofocus distance: 193 mm (from CaptureResult.LENS_FOCUS_DISTANCE)
- Calculated distance: 156 mm
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.