Disparity calculations and camera centers

I’m learning about the disparity calculations and want to calculate the distance for a known pixel. I have calibrated and rectified images already. Now I just want to calculate Z for some pixel x,y.

the opencv tutorial says: "The depth of a point in a scene is inversely proportional to the difference in distance of corresponding image points and their camera centers.
According to this paper https://www.ri.cmu.edu/pub_files/pub2/willson_reg_1993_1/willson_reg_1993_1.pdf there are a lot of different camera centers.
What kind of camera center should I use?

Should I use the principal point Cx,Cy from the camera matrix? That doesn’t quite make sense since they differ between cameras. If I just use the Center of the frame in pixels, it doesn’t take into account projection center. I’m lost in all these centers. Tutorial makes it look so easy. Anyone has practical experience with the topic?

PS>also why does the right arrow, next to x’ points to the right, as if suggesting that it takes the distance from the left camera center.

Hello @alpi,

In the general case where the principal points don’t have the same image coordinates in the left and right images, the distance Z is calculated as follows: Z = B \cdot f / ((x - c_x) - (x' - c_x')) where c_x and c_x' are the position of the principal point along the x-axis in the left and right images respectively.


Thanks @lpea

Which f would you use? the one from the manufacturer? (focal length in mm)

Is there any way to do this without requiring manufacturer data?

@DoDoM Since you have calibrated your cameras, you should use the focal length given by the stereo calibration function.

Edit: as you mentioned in another thread, stereoCalibrate gives you a value of f_x in pixels and not in millimeters. But it turns out that this is exactly what you need to compute Z. If you look at the formula mentioned in my previous comment:

  • the baseline B is expressed in metric units
  • the disparity (x - c_x) - (x' - c'_x) is in pixels (since x is the pixel coordinate of the point and c_x the coordinate of the principal point).

Therefore, f has to be expressed in pixels for the formula to be consistent. You actually don’t need to know the size of a pixel or the focal length in metric units, but only the ratio between these two quantities (i.e. the focal length in pixels).