how to get Distance of Object from iPhone camera using image Exif meta data?

23,407

Solution 1

@iphonemaclover. As many people have pointed out there's insufficient information to calculate this purely using trigonometry. However, depending on how accurate you need to be, or what it is you're trying to measure and other data you can expect to recover from an iphone and/or you're willing to make some assumptions, it is possible to make some inroads.

a) +1 to Martin R, if you assume a flat earth, a phone height (which as per the app you quoted) a user could update for calibration purposes, can recover pitch information and know where a point on the ground at the base of your object is then this is simple trig. Once there's a estimate for the distance and assuming the thing you're measuring is close to vertical (or sits at a known angle) then its height can also be calculated.

b) Your exif file contains face region information. If you're interested in people and are happy to make an assumption that they're an adult then you could use an average head size assumption to estimate distance using the method you've outlined already.

c) If you can recover a series of images and camera positions and the camera / object positions vary then I believe that 3d information can be recovered using projective geometry.

Solution 2

If you're wondering how to find distance given the phone/tablet height and pitch up/downward, just use the atan of the angle downwards (0 if it is pointed forward; -30 if it is -30 degrees downwards) times the height.The only problem is, we don't know the phone height off the ground.

I know you want to use the focus of the camera, but that is improbable. You mentioned an android app, while if you'll notice that app requires you to enter phone height off the ground. You could try using GPS altitude minus what a topographic map says, but that is incredibly inaccurate. Or maybe you could take a picture from one location, then move around the target by 45 degrees or so, but not everyone has a compass to measure that or has the ability to run circles around the target. How would you move 45 degrees around a object in front of you while standing on a bridge? Even if you did try to use the camera focus, that is automatically updated and I know nothing about using a variable to find the camera focus. https://developer.apple.com/library/ios/documentation/AudioVideo/Conceptual/AVFoundationPG/Articles/04_MediaCapture.html shows how to set focus mode, but I can't find anything on setting a custom focus or viewing what the camera is focused on. If you want to take a picture and do some sort of custom pixel by pixel analysis to see how it's focused, by all means go ahead. Maybe a jail-broken iPhone can access camera focus, but I'm guessing that your target audience is more then the select group that have gone through the jail-breaking process.

I think the easiest way to find the height is to have them point their phone down at their foot. Then you can ask them shoe size and brand to estimate the width of their shoe, or just make a flat out guess. Just ask them to point at the side of their shoe, perhaps by displaying a red dot in the middle of the screen so they know exactly where they're pointing. Then ask them to point at the other and then, once you have captured both edges of their shoe, use the estimated inch width of their shoe multiplied by the tan of the angle difference from one edge to the other (so if it is 170 degrees when pointed at one edge and 190 when at the other use the tan of 20). That should give you the height in inches off the ground.

    s = shoe width;
    ao = angle when pointed at left shoe edge (y-axis)(radians);
    at = angle when pointed at right shoe edge (y-axis)(radians);
    a = angle of device when pointed at the point on the ground (x-axis)(radians);
    d = distance;
    d = atan(a) * tan(at - ao) * s;

Solution 3

Its not possible way to get distance while varying ground-mobile distance dynamically from each person. Manually user have to set the height of the mobile each time to calculate.

Solution 4

I know this is an old question, but something had me thinking about whether it was possible to use an iPhone's focus sensor to measure distance to an object, and this came up in my searches.

I think that in @iphonemaclover's original question, the "sensor height" does not refer to the camera's height above the ground, but the physical height of the camera sensor inside the phone. After all, if you had the phone on a rotating arm pivoting around the subject, the distance to the subject could remain constant while the phone's vertical position changed. It's the sensor size and focal length that determine what is in its frame.

With that in mind, I did some quick experiments with my phone which seemed to bear this out (though, I'll admit that I did not perform any real measurements beyond eyeball estimates and a desk ruler.)

I was using an iPhone 5, and with a little Googling, we can learn some key pieces of information about its camera sensor:

  • pixel resolution: 3264 x 2448
  • focal length: 4.10mm
  • sensor size: 4.54 x 3.42 mm

So: the first formula above for "Working Distance" can tell us the distance at which a subject of a given size will fill the sensor's frame. With the camera in landscape orientation, and a subject 2m tall (2000mm), we get:

(3.42 + 2000) * 4.10
--------------------  =  2401.76mm, or about 2.4m
        3.42

So if you stand about 2.4m away from a 2m tall person, they should fill the frame in landscape orientation. For portrait, you'll need to step up to about 1.8m away.

That's all fine and wonderful for filling the frame. But we want to calculate for arbitrary distances, by seeing how much of the frame our subject takes up. That's what the second formula is for. Let's again use our 2m tall subject, and plug numbers in. For this experiment, let's say that we've examined our image, and the subject in the photo is taking up 1800 pixels, vertically. Plugging into the second distance formula, we get:

4.10 * 2000 * 2448
------------------  =  3260.82mm, or ~ 3.26m
    1800 * 3.42

And if the subject took up the same 1800 vertical pixels in portrait mode, then we could guess that the photographer was about 3.28m away.

This all assumes that the formulae given above are correct. And again, I only did rudimentary attempts at verifying the 'full-frame' measurements (once with a subject about 28cm tall, then with a subject about 1.7m tall). But my eyeball estimates looked reasonably close to what the formula predicted.

Share:
23,407
9to5ios
Author by

9to5ios

Welcome Friends!!! My self :- Deepak Singh Rawat Iphone Game and Application Developer India My blog:- http://9to5ios.com Email-me:- [email protected]

Updated on July 09, 2022

Comments

  • 9to5ios
    9to5ios almost 2 years

    Edit: Sorry for late Edit, Without two parameter you cannot calculate it, So first need to fill user Camera height from ground.


    I have check a number of solutions but none of them helpful!

    I know that Working Distance = (Sensor Height + Subject Height) * Focal Length / Sensor Height

    and

    distance to object (mm) = focal length (mm) * real height of the object (mm) * image height (pixels)
                              ----------------------------------------------------------------
                                    object height (pixels) * sensor height (mm)
    

    And I want to get distance from this:

    Image Formation by Lenses and the Eye

    Hello I get the following info using image Exif ALAssetsLibrary

    And I got following meta data :

    Save image metadata.
     {
        DPIHeight = 72;
        DPIWidth = 72;
        FaceRegions =     {
            Regions =         {
                HeightAppliedTo = 2448;
                RegionList =             (
                                    {
                        AngleInfoRoll = 270;
                        AngleInfoYaw = 0;
                        ConfidenceLevel = 376;
                        FaceID = 1;
                        Height = "0.1413399";
                        Timestamp = 5996166864910;
                        Type = Face;
                        Width = "0.1060049";
                        X = "0.3560049";
                        Y = "0.4746732";
                    }
                );
                WidthAppliedTo = 3264;
            };
        };
        Orientation = 6;
        "{Exif}" =     {
            ApertureValue = "2.526068811667587";
            BrightnessValue = "1.291629806962232";
            ColorSpace = 1;
            DateTimeDigitized = "2014:03:25 15:43:36";
            DateTimeOriginal = "2014:03:25 15:43:36";
            ExposureMode = 0;
            ExposureProgram = 2;
            ExposureTime = "0.05";
            FNumber = "2.4";
            Flash = 24;
            FocalLenIn35mmFilm = 33;
            FocalLength = "4.12";
            ISOSpeedRatings =         (
                160
            );
            LensMake = Apple;
            LensModel = "iPhone 5 back camera 4.12mm f/2.4";
            LensSpecification =         (
                "4.12",
                "4.12",
                "2.4",
                "2.4"
            );
            MeteringMode = 5;
            PixelXDimension = 3264;
            PixelYDimension = 2448;
            SceneType = 1;
            SensingMethod = 2;
            ShutterSpeedValue = "4.321956949076723";
            SubjectArea =         (
                1631,
                1223,
                1795,
                1077
            );
            SubsecTimeDigitized = 261;
            SubsecTimeOriginal = 261;
            UserComment = hoge;
            WhiteBalance = 0;
        };
        "{GPS}" =     {
            Altitude = "196.008";
            AltitudeRef = 0;
            DateStamp = "2014:03:25";
            Latitude = "28.61772";
            LatitudeRef = N;
            Longitude = "77.38891";
            LongitudeRef = E;
            TimeStamp = "10:13:37.439000";
        };
        "{MakerApple}" =     {
            1 = 0;
            3 =         {
                epoch = 0;
                flags = 1;
                timescale = 1000000000;
                value = 249840592070541;
            };
            4 = 0;
            5 = 179;
            6 = 139;
            7 = 1;
        };
        "{TIFF}" =     {
            DateTime = "2014:03:25 15:43:36";
            Make = Apple;
            Model = "iPhone 5";
            Software = "7.0.6";
            XResolution = 72;
            YResolution = 72;
        };
    }
    

    I need to calculate the distance of the object from the camera, using the above details; using iphone4s,iphone5, or iphone5s. Is it possible?

    enter image description here

    Modified Need to know formula used by this app any idea:

    Need to know any method

    http://www.youtube.com/watch?v=eCStIagorx8

    how this App working ??? All Help are welcome

  • Jonny Vu
    Jonny Vu about 10 years
    Completely possible to caculate this distance. I dont know how to work in objective C but let think that, how a camera could "auto focus" to a point. Camera have to calculate DOF (Depth of field) depend on 3 indexs(distance, apesture, focus). So, when we focus to one point using camera -> we have had DOF -> we have apesture and focus -> easy to calculate distance following physical knowledge
  • 9to5ios
    9to5ios about 10 years
    can you please provide me some sort of algorithm used there?
  • Dougal Campbell
    Dougal Campbell almost 9 years
    As an added bonus, since the only unknowns are the subject's real height and pixel height in the image, you can simplify the formula down to a constant (focal length * image pixel height / sensor height) times the ratio of the real:pixel heights. For landscape, the constant is about 2935, and for portrait, it's about 2948.
  • Dougal Campbell
    Dougal Campbell almost 9 years
    See my answer below. The key to this is knowing the camera's sensor size. With the sensor size, image pixel resolution, focal length, and known subject size, you can back-calculate (well, estimate) the distance between the camera and subject (assuming that the subject is reasonably parallel to the camera sensor plane). I think people were getting hung up on the simple trig problem, prompted by the illustration. But you can estimate the distance, when the camera sensor stats are known.
  • Dougal Campbell
    Dougal Campbell almost 9 years
    It's probably obvious to most, but also, if you knew the distance, but not the size of an object in the image, you could use the same formulas to estimate that.
  • StuReeks
    StuReeks over 8 years
    Dougal, if we're given the height of the object we're looking at then there are a number of different ways (yours included) of recovering the distance. There are a number of ways of adding information / making assumptions which might help. In my answer, (a) doesn't require us to know the height of the subject (but requires camera height and pitch) (b) assumes that we're finding distances to humans (your method works here, but this one uses the camera's own facial recognition) (c) assumes that the object can be found in the camera's scene, but little else is known other than camera position.
  • Dougal Campbell
    Dougal Campbell over 8 years
    StuReeks, the OP wanted to calculate distance-to-subject using the given formulae and exif data. Assuming we have an available database of sensor details (focal length, width, height, pixels) for most popular cameras, the only unknown is the subject height. In (a) and (b), you have to guess at several variables, increasing the error margin. Using (c) you might be able to reduce the error, but that will depend on a lot of factors. Granted, in the real world, all these methods will be prone to some error.
  • Dougal Campbell
    Dougal Campbell over 8 years
    Based on the exif sample given, it doesn't seem possible to calculate the subject distance without some external data. I guess it's mostly a matter of picking a method that best fits which data you have available.
  • M David
    M David over 7 years
    can you please elaborate your comment which is added as a bonus? What is real:pixel heights
  • Dougal Campbell
    Dougal Campbell over 7 years
    @MDavid, What I mean is that if you were photographing a person or landmark of known height, you could just enter the height, then determine how many vertical pixels they occupy within the image frame (either by algorithmic detection, or by some UI for drawing a box around it). Convert the object height to millimeters, divide by the screen pixels, and multiply by the constant for your camera sensor (for any given sensor, all the other numbers will remain constant).
  • Ameet Dhas
    Ameet Dhas almost 6 years
    Can we get the sensor size programmatically in iOS? I did not find any such key in EXIF Dictionary Keys. Is there any other way?