Find the focal length (in pixels) of your smartphone

In the world of Computer Vision(CV), there are many interesting concepts. Deep Convolutional Neural Networks have largely dominated many CV tasks in the past decade. CNNs are able to perform better than humans at things like image classification, object detection and image segmentation in certain domains. The best advantage of CNNs are that they can run at scale, hence putting much of the image data collected by individuals and corporations to good use! Recently, Transformers are also being explored for CV tasks. However, in this post, we will focus on a more ‘old-school’ aspect of Computer Vision: 3D Vision. I am sure 3D vision is something we encounter in our everyday lives — I mean, our eyes basically function using this principle. This post aims to introduce some of the basic concepts behind it!

Image for post

Photo by Nathan Dumlao on Unsplash

One of the key functions of 3D vision is to capture depth information. With a single camera, we only have 2D information about a particular scene, therefore 2 cameras are needed at minimum! (E.g. our eyes!). In this series, I will demonstrate how to do 3 simple tasks that can be done with your smartphone camera:

  • Find the Focal Length of your smartphone camera
  • Recover the depth of an object using 2 photos
  • Find the angle of rotation of your camera when taking 2 pictures of the same scene from the same spot.

Finding focal length

The focal length of a camera is illustrated in the figure below. This is a simplified of the inner workings of cameras and lenses. We can easily see that the focal length can be found by measuring the real height, real distance and image height of an object. These quantities are related by the formula:

Image for post

Image for post

If you remember your high school physics lessons, this probably looks familiar. Now, we will go further than theory, and try to find out the focal length of our smartphones! You can follow the steps and try it out yourself.

  1. Find an object with a known height, H. Use your friends/family or you can just use 2 points on a wall and measure the distance.
  2. Take a photo from a certain distance, **D **away from the object.

Image for post

Photo of my bedroom with 2 beautifully drawn crosses on the wall.

3. Measure the image height, **h **of the object. You can use any common image editing tool or image viewer, I did it in Microsoft Paint. By hovering over the crosses with a brush tool, we can get the image coordinates shown in the bottom left of the image below.

Image for post

4. Calculate the image height from the two points using Pythagoras’ theorem. For my photo, the points were (1849,291), (1782,2954), and the calculated height was 2664 pixels.

5. Calculate f using the equation from above! Make sure that the units of H and D are the same (I used meters like most of the world).

6. I got a focal length of 3566 pixels, using a OnePlus 6T smartphone. Do share your results and phone model by commenting below!

#computer-vision #opencv #deep-learning #data-science #developer

Introduction to 3D Vision
2.05 GEEK