Handling Focal Length Conversion To Pixels For Distance Calculation— Android

Before

David Biga
3 min readApr 22, 2019

If you find yourself doing Image Processing, you’ve probably found (or will find) yourself in a situation where you are required to find the distance between two points on an image. You might need this to do further processing of segments of an image or simply provide basic distance information.

This article expects that you are working with Android devices of no specific type.

Introduction

Let’s dive in.

If you’re new to this, you might be asking “Why do I care about the focal length in pixels?” Well in this example, we’ll use this scenario:

You are building an application that will report “too close” when the device gets within 5ft (152cm) of a vehicle. The application will use the vehicle’s taillights to define our two points needed .

With this scenario in mind, it is crucial to find the focal length in pixels before we can derive the metrics in distance. We’ll solve for centimeters.

First, let’s look at what focal length is. It is the distance between the lens and the physical sensor. Depending on the focal length distance, this will determine your FOV (field of view or angle of view).

Now that we have some background, let’s solve for the focal length in pixels.

Solving

double focal_length_px = (size.width * 0.5) / tan(horizontalViewAngle * 0.5 * PI/180);

size derives from the preview size in Android. horizontalViewAngle derives from the camera sensor itself which is also provided by Android.

Important: Here is where things get interesting. From a study I did a few years ago, I found that nearly 20% — 30% of Android OEM’s reported incorrect hardware specs for view angles. Mainly off-brand manufacturers sold in China.

This throws a wrench in the mix. If you are in a situation where you do not have control over the hardware and that you’ll be serving a customer base of all different devices, the best way is to compensate with thresholds. Meaning, don’t look for exact values, but rather a range.

Calculating Distance

Now that we have our focal length in pixels we can solve for our distance. We’ll first get the distance in millimeters using the following:

float calc_distance_mm = _avg_distance_between_taillights_mm * focal_length_px / actual_distance_between_taillights_px;

What is happening here? Well, we use the average distance between taillights in millimeters (let’s say roughly 3ft or 940 mm). We then multiply that by the focal length divided by the actual distance between the two points we had access to in this scenario.

How would we get actual_distance_between_taillights_px ? Well, prior to this step we would have detected the two taillights and found the center point for each. We’d normalize and subtract one another to get the distance in pixels between the two points.

From here it is simple to convert to centimeters. Divide the calc_distance_mm by 10.

So now let’s put it all together!

float calculated_distance_cm = calc_distance_mm / 10;

Now all you’d have to do is check if calculated_distance_cm is less than 152cm and you could report “too close”….or just slam on the breaks….

If you have further questions, please reach out.

--

--

David Biga

Engineer gone founder. Building tech for Real Estate