Estimating The Object Depth Using The Camera Obscura formulas and lens equations (python)

The goal of this post is to understand how camera devices work and take advantage of that to compute the distance of a given face in an image using the face detection algorithm described in my previous post.

Mahmoud Harmouch
The Startup

--

Pinhole camera

“Light was God’s first creation and one of nature’s most important gifts to mankind…“

Definitions

First, you may ask the question: “What is light?” As a beginning, you should give an operational definition of light: Light is the substance that the normal human eye senses.

Perhaps this is not a profound definition, but it starts us on an exploration of light by giving us a test for the presence of light. When we see an object, it is because the light comes to the eye. If an object emits light, then we say that it is a source of light. Objects that emit light of their own accord are called luminous sources. Other objects, called illuminated sources only emit light when light from luminous sources shines on them(Like the moon).

The term Camera Obscura, which means darkroom in Latin, can be compared to a modern-day camera that doesn’t require a lens or film; the device projects an inverted image onto a wall or canvas.

It the simplest optical device that allows you to get an image of objects on the screen. also Called: pinhole camera.

History

The Chinese first discovered in the 5th century B.C. that if one made a small hole in the wall of a darkened room, an inverted image of the outside is projected from the pinhole to the wall opposite the small hole. No matter the distance to the opposite wall, the image of the outside was projected onto it. Shorten or lengthen the distance to change the size of the projection.

The Arabian scholar Abu Ali al-Hassan ibn al-Haytham(10th Century A.D.), also known by his Latin name Alhazen, described the Camera Obscura in his writings(Book of Optics). Alhazen stressed the significance of the relationship between the size of the aperture and the sharpness of the image.

The first optical equipment for creating artistic paintings was created by the great master Leonardo da Vinci, who lived in 1452–1519. Its description can be read in the “Treatise on Painting”, where the author spoke about the principle of operation of an optical device.

Around 1519, Leonardo da Vinci (1452–1519) wrote:

“When images of illuminated objects … penetrate through a small hole into a very dark room … you will see [on the opposite wall] these objects in their proper form and color, reduced in size … in a reversed position, owing to the intersection of the rays”.

Theory:

Case 1: pinhole camera

Now, our goal is to find a relationship between the focal distance (f) with the Object height (HI = KJ), Image size (LM = EF), and the distance of the object from the camera (d). In addition, if we can find it (at least we can try), we can estimate the distance of the object from the pinhole (Camera), knowing the size of the image (ML = EF), the size of the object (HI = KJ = 18cm; the average height of the human face is something around 18 cm) and the focal distance is unknown.

Focal distance

To compute the focal distance, you can put a 30-centimeter paper in front of your camera, so that it fits completely on the screen, and measure the distance to the camera from the paper(I found 19 cm distance from my camera), you can find out the ratio of the height of the Image of your camera to its focal length by applying the following analogy:

To find out this ratio, you need to investigate a little bit of some basic mathematics. Knowing that the two triangles ABC and CDE are similar, we can write:

They are similar because all three angles are equal to each other. This formula leads to:

so

From this formula

It is seen that if CG increases by a certain factor N, then AB (image height) will decrease N times because f and DE (object height: a piece of paper or face…) are constants.

In addition, this is logically true because if the object moves far away from the camera, then the image would be small on the screen.

Now we need to find out the ratio between the object height and the image height while moving in front of the camera like the following situation:

The object moves from HI to KJ, the image height will decrease from CD to EF. And we know that LM = EF, so we can simplify the Image as follow:

the two triangles GLM and GJK are similar, so this will leads to :

So

Recall from equation (1):

So, finally, we have :

Verification

This equation seems to be logically True because, if you move far away from the camera, your Image in the picture will be small.

Unit is cm -> True

Case-2: camera with a single lens:

we know that the two triangles DFE and FBA are similar, so we can deduce:

but

FG = f + distance from camera lens (f is the focal distance)

DE = height of the image on screen

AB = height of the piece of paper = 30 cm

FF = di –f

so

verification:

If f = 0 will should obtain the same formula in (1) for the pinhole camera:

di in a pinhole camera is the same as focal length (terminology)

after doing simplification to (2) we can deduce:

The standard focal length for a camera is between 35 mm and 70 mm, lets take 50 mm. (5cm = 189 pixels)

From the picture above, moving the object from (A) to (B) will decrease the image size from [c] to (D).

Using the same analogy as discussed before, (Distance = d)

Code

Writing the previous two equations for pinhole and camera with lens, we can find the distance of a certain person in a given picture like the following

Here, we can see that the cameraman is about 1.5 meters away from the actress.

I think 1.5 meters is not really valid because the cameraman can zoom in into the picture, and also he can crop the image while editing (I didn’t take into consideration these two factors).

Conclusion

The pinhole camera was the cornerstone of modern camera devices and it can be considered, regardless of its simplicity, one of the most important discoveries in human history.

For more information about my project, feel free to check out my repo.

--

--

Mahmoud Harmouch
The Startup

Senior Blockchain Rust Enjoyer at GigaDAO - I occasionally write articles about data science, machine learning and Blockchain in Rust - Currently Writing Books