8

I had searched every where for finding focal length of the camera and its back sensor height. But I didn't get any specific detail.

I am developing one app which will calculate the actual height of an real world object from the height visible in the image captured from an ios camera.

distance to object (mm) =

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

here I am having the values distance to object as static, image height in pixel. Now I need focal length and sensor height to find out the real object height.

Thanks in advance. bskania

BSKANIA
  • 1,317
  • 15
  • 27

2 Answers2

5

EXIF Headers

iOS devices provide the camera's focal-length value in the JPEG EXIF headers. It is visible within iPhoto and just about any other tool.

Options for retrieving it programmatically

The list of EXIF header keys can be found in the CGImageProperties Reference.

NSURL *imageFileURL = [NSURL fileURLWithPath:...];
CGImageSourceRef imageSource = CGImageSourceCreateWithURL((CFURLRef)imageFileURL, NULL);
if (imageSource == NULL) {
    // Error loading image
    ...
    return;
}

NSDictionary *options = @{kCGImageSourceShouldCache, @NO};
CFDictionaryRef imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, (CFDictionaryRef)options);
if (imageProperties) {
    NSNumber *width = (NSNumber *)CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelWidth);
    NSNumber *height = (NSNumber *)CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelHeight);
    NSLog(@"Image dimensions: %@ x %@ px", width, height);
    CFRelease(imageProperties);
}
CFRelease(imageSource);
Cameron Lowell Palmer
  • 21,528
  • 7
  • 125
  • 126
  • CGImageSourceRef is certainly one place to get a dictionary of meta-data from the image. CGImageSourceCopyPropertiesAtIndex is the specific function call. – Cameron Lowell Palmer Dec 19 '14 at 14:37
2

The way to solve this issue.

First take one real world object and its measure i.e. height and width.(example of coke cane) Now take a snap of the object that you want to measure. For the iphone or ipad the focal length is fixed to 25mm. Now frame the object that you want to measure on a snap and re-size the real world coke cane image according to the object you want to measure.

use the equation

object_distance = (focal * real_object_height)/object_height;

here, real_object_height = coke cane height; and object_height = coke cane re-sized height

and accordingly measure the height of object using equation

height_of_frame = ((obj_distance) * measured_object_height_in_mm / 1000.0))/focal;

width_of_frame = ((obj_distance) * measured_object_width_in_mm / 1000.0))/focal;

this way you can find out the object distance and its measure. but you need two static data i.e. focal length of camera which you are using and the one real world object measure to compare.

thanks, bskania.

BSKANIA
  • 1,317
  • 15
  • 27
  • can we measure hight and width of any object in image ? where we nothing found like distance etc – Waseem Shah Nov 12 '14 at 10:12
  • yes we can find. but for that lense focal and one relative object height and width should be as known factor. working application on this formula https://itunes.apple.com/us/app/fishweight/id785343148?mt=8 – BSKANIA Nov 12 '14 at 10:31
  • @BSKANIA real_object_height and object_height pass into which format like pixel, cm or inches. And What is measured_object_height_in_mm? – Dharmraj Vora Jul 27 '21 at 06:05
  • @BSKANIA is this reproducible for different type of devices? or is it for a particular set of devices? In my opinion it won't work with different devices like Android phone, Android tablets, iphone and ipad. What is your opinion? – Aparajit Garg Oct 06 '21 at 05:08