Is it possible to calculate the distance of an object to the camera with two pictures which are taken a few cm apart? We are able to estimate the distance with our eyes. But is it possible to do it mathematically without knowing anything about the object's height?
Is it possible to calculate the distance of an object to the camera with two pictures which are taken a few cm apart?
It depends. You'd need to know something more than just having the two photos. Examples of helpful information include information about the camera and lens such as the sensor size and focal length, and also exactly how the camera's position changed between the two photos. How far did the camera move? Did it keep exactly the same orientation, or was the camera rotated a bit so the the object in question is in the same position in both photos? It would also help to know about other objects in the photos, such as size and distance from the camera.
We are able to estimate the distance with our eyes. But is it possible to do it mathematically without knowing anything about the object's height?
There's much more going on in human vision than just comparing the mental equivalent of two photographs. What your brain does is more like comparing two live video feeds augmented by a huge database of knowledge about the objects in the scene and past experience. And even with all that extra information (or in part because of it) we are easily fooled when reality doesn't match our assumptions. For example, an Ames room uses forced perspective to trick your brain into thinking that a person walking around is growing and shrinking rather than moving closer or farther away.
A photograph is a two-dimensional projection of a three-dimensional scene, and a lot of information, including size and distance, is lost in creating that image. Put another way: for any given image, there are many scenes that could produce that image.* Consider a photo of a Lego mini figure:
If you've ever seen a Lego mini figure, you know that this one is probably about 4cm tall. But is it really? It could be that this one is more like 4 feet tall. Having a second photo from a slightly different perspective doesn't give you any extra information about the true height of this mini (or maxi) figure unless you know how far the camera moved, etc. In other words, you need some piece of data that lets you establish a scale.
Okay let's assume I know how far the camera is moved and there is no rotation. Also there is a ruler near the camera in a given distance.
Think about the way things change when you look out the side window of a car moving at highway speed: objects on the side of the road whiz by, while faraway objects seem to move (relative to you) much more slowly, and objects very far away (stars, for example) don't seem to move at all. You can use that same principle. I don't have time at the moment to work out a formula for you, but I don't think it'd be a hard problem to solve with a diagram and a little geometry. And keep in mind that things start to get a lot more complicated if you have to account for distortion introduced by the lens; for example, a fisheye lens magnifies the center and compresses the edges.
If you're writing code to do this, the OpenCV library has features that will help, and it can even deal with the lens distortion mentioned above. Take a look at the Stack Overflow question: Distance to the object using stereo camera
* Sculptor Seward Johnson takes great advantage of that fact. When viewed from just the right spot his life-sized "Beyond the Frame" sculptures look like famous paintings, but you can walk around them and look at them from other angles and they often include funny details not visible in the original paintings.
It is very easy if you can align the cameras before the shot so that the target will appear in exactly same point (eg the very center). That's how our eyes work.
Rangefinder cameras (like Leicas) do exactly this: you see 2 two images taken with known distance (the distance between 2 rangefinder lenses), you turn the focusing knob to match the images - and the knob position means distance. Math is very simple, it has been done mechanically.
There is no need to know anything about the target, all you need is to have aligned images and know distance and both angles of your cameras.
When you have 2 images that are NOT aimed on your target it gets harder - but XBOX kinect does this with 2 stationary IR cameras.
If you know an approximate actual size dimension of something in the one picture (and if you know focal length and sensor size), you can estimate its distance using the calculator at http://www.scantips.com/lights/subjectdistance.html
It is just geometry, but easier to reference it than to derive it here. I've been surprised that distance is of interest, but it does seem to be...