Measure distance from an object using iPhone x ir camera

Hello,


Using kinect, you can find the distance between an object and the camera like this:https://pterneas.com/2016/08/11/measuring-distances-kinect/


I can't find any info or way to achieve this in the sdk.

Is it possible to do the same with the iphone x ir camera ?


Thank you in advance for your help.

Replies

I don't think there is a ndirect method so far in API, even if autofocus data could probably give some data.

May be, when PAI of faceID is open, there will be more possibilities.


However, there may be some indirect metrhod, but they require you know the size of the object See:

h ttps://stackoverflow.com/questions/22634518/how-to-get-distance-of-object-from-iphone-camera-using-image-exif-meta-data


I have also thought of a way to measure it with some trigonometry, with no need to know object size, but that may be a bit difficult operationally as it is not a programmatic evaluation.

Here it is, for the fun. Ready to follow me ? Take care, it is a bit long


First, some skematics of the situation.

The object is at distance x.

It's height is h (but we shall not need it at the end).

It appears at an angle a in the camera (which leads to an apparent height hAppA on screen).

If I move forward by a distance d to a new distance y to the object, it will be seen with an angle b, yielding to an apparent height h AppB on screen

Some drawing taking profit of the remarkable graphics capabilities of the editor of the forum.

|

/ / | h

/ a / b |

--------------------------+-------------------------------

<------ d ------------><------------- y------------->

<-----------------------x------------------------------>

We get 2 equations:

1. h = x tg(a)

2. h = y tg(b)

3. x = y + d

The trick would be to move (step forward by distance d) so that b is twice a (object appears twice as large)

Which means

4. b = 2 * a

b can be evaluated as we can find what is the angle for a full screen height.

We shall need trigo formula : cos(b) = cos(2a) = cos2(a) - sin2(a) = 2 * cos2(a) - 1, hence

cos2(a) = (1 + cos(b)) / 2

Noting also that

5. 1 + tg2(a) = 1 + sin2(a) / cos2(a) = (cos2(a) + sin2(a)) / cos2(a) = 1 / cos2(a) = 2 / (1 + cos(b))

Then we get:

2. h = y tg(2a) = y * 2 tg(a) / (1 - tg2(a)) where tg2(a) means square of tg(a), because tg(2a) = 2 tg(a) / (1 - tg2(a)) as described in your trigonometrics lessons

With 1 and 2, we can eliminate h:

x * tg(a) = 2 * y * tg(a) / (1 - tg2(a))

so

(1 - tg2(a)) * x * tg(a) = 2 * y * tg(a)

hence

x * tg(a) - x * tg(a) * tg(a) * tg(a) = 2 * y * tg(a)

and because of 3.

x * tg(a) - x * tg(a) * tg(a) * tg(a) = 2 * (x - d) * tg(a) = 2 * x * tg(a) - 2 * d * tg(a)

get out one x * tg(a) on each side

- x * tg(a) * tg(a) * tg(a) = x * tg(a) - 2 * d * tg(a)

Simplifying by tg(a) on each side:

- x * tg(a) * tg(a) = x - 2 * d

hence

x * (1 + tg2(a)) = 2 * d,

which with 5. leads to

x * 2 / (1 + cos(b)) = 2 * d

And here we are

x = d * (1 + cos(b))

So, operationnaly:

you are at a distance x

move by d so that the object appears twice as large

Evaluate the angle of a full screen image, allows to evaluate b

Then you find x, without needing to know h.

Of course, if you know the height of the object, you can get x directly !