5

In Android, I am currently accessing the camera's focal length by using getFocalLength() in Camera1. Camera2 is not an option.

I am trying to full fill the current calculation: focal_length_pix = focal_length_m * 1 (pix) / pixel_width_m.

Basically this converts the focal length from mm -> px. Now I know the focal_length_m variable but I am currently trying to figure out the pixel_width_m which is is the width of a pixel (on the sensor) in meters.

I am struggling to find a way to calculate the width of a pixel on the sensor. Any suggestions, ideas would be much appreciated.

David Biga
  • 2,763
  • 8
  • 38
  • 61
  • it would make more sense to convert all pixel values into meters... – Piglet Jun 29 '16 at 15:58
  • @piglet for my current needs, it has to be px. – David Biga Jun 29 '16 at 15:59
  • and what is that purpose? I honestly can't think of any use case. do you have any further information on your optics and/or sensor? – Piglet Jun 29 '16 at 16:02
  • 1
    @piglet as defined above, I grab the focal length in mm. I am trying to get the `pixel_width_m`. do you have any helpful information or guidance on doing so? The camera itself will be on any Android device. so each OEM will be different. Currently testing on an S7. – David Biga Jun 29 '16 at 16:04
  • the pixel size cannot be derived from the focal length alone as you do not know the distance to the sensor. the only chance would be to focus into infinity so the distance to the sensor equals the focal lenght (assuming the focal length remains constant) and then do some test pictures of known targets. – Piglet Jun 29 '16 at 16:56
  • @Piglet, I see what you are saying, side note if I use camera2 and have the physical size of the sensor, I can use that correct? – David Biga Jun 29 '16 at 17:07
  • 1
    yes. simply divide the sensor dimensions by the number of pixels per dimension if the pixel dimension is not given directly. – Piglet Jun 29 '16 at 17:13
  • @piglet Do I have access to the pixel dimensions in `android`? – David Biga Jun 29 '16 at 17:19
  • https://developer.android.com/reference/android/hardware/camera2/CameraCharacteristics.html#SENSOR_INFO_PHYSICAL_SIZE – Piglet Jun 29 '16 at 17:53
  • @piglet I actually have might been able to solve using: `image_width` and `HorizontalViewAngle`. `double focal_length_pix = (size.width * 0.5) / tan(horizontalAngleView * 0.5 * PI/180);` would be the equation. – David Biga Jun 29 '16 at 18:04

1 Answers1

8

You are able to calculate the focal length in pixels by the following:

double focal_length_pix = (size.width * 0.5) / tan(horizontalAngleView * 0.5 * PI/180);

size derives from getPreviewSize()

David Biga
  • 2,763
  • 8
  • 38
  • 61
  • For the Camera 1 API : `horizontalAngleView = camera.getParameters().getHorizontalViewAngle()` – WillC Mar 03 '17 at 00:04
  • @WillC you are correct, but through much testing I've discovered that devices, mainly china, report inaccurate view angles. Some report the wrong horizontal angle or vertical. By limiting To one angle, and using appropriate math. You lessen your chances at getting an inaccurate focal length. – David Biga Mar 03 '17 at 00:07
  • Good to know. Would you recommend calibrating the horizontal view angle manually per device (or per model) and then doing some form of lookup in an enum in the code? – WillC Mar 03 '17 at 00:11
  • Well in our case, we needed to get away from "per device/model" configurations to compare against. There are thousands of devices and every day it seems as though new ones are made. The best option is to use percentages. I think that it's hopeful for the future that manufacturers provide proper specs – David Biga Mar 03 '17 at 00:13
  • Agreed that it would be good if manufacturers would do what they are supposed to. How can I find or calculate those percentages you mention, working with the Camera 1 API / Android 19 API? – WillC Mar 03 '17 at 00:16
  • Thanks for your advice - - supplementary question if you have time: how reliable have you found camera.getParameters().getFocalLength() – WillC Mar 03 '17 at 00:29
  • Well you need to create thresholds on your end to allow an acceptable bound. For instance, if you know that the view angles might not always be accurate, account for an average. Generalize it. Per your second question, I can't fully remember, but I want to say 20 - 30% inaccurate. As discussed above, camera sensors contain this value and Android calls a getter from a table that contains the sensor information. It does not, to my knowledge, run the calculation. It would be expected that the manufacturer would of known this and provided the correct information. Hope this helps. – David Biga Mar 03 '17 at 21:25