The world’s leading publication for data science, AI, and ML professionals.

Introduction to 3D Vision

Find the focal length (in pixels) of your smartphone

Photo by ShareGrid on Unsplash
Photo by ShareGrid on Unsplash

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!

Photo by Nathan Dumlao on Unsplash
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:

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.
Photo of my bedroom with 2 beautifully drawn crosses on the wall.
Photo of my bedroom with 2 beautifully drawn crosses on the wall.
  1. 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.
  1. 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.
  2. 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).
  3. I got a focal length of 3566 pixels, using a OnePlus 6T smartphone. Do share your results and phone model by commenting below!

Conclusion/Discussion

This was a quick post describing how anyone can calculate the focal length of their smartphone just by snapping a photo. This is good information to know as we can cross check the actual focal length with the specifications given by the phone sellers/manufacturers! Also, knowing the focal length in pixels will allow us to do some of the other tasks in this series.

You can also repeat this experiment with different objects at multiple distances, and take multiple photos. Finally, calculate the average f value to reduce the error. Subsequent posts in this series will explain other hands-on projects you can do on your smartphone easily, so stay tuned!


Related Articles