9

I am working on Stereo vision task and I would like to get the distance between stereo vision cameras and the object. I am using Matlab with Computer Vision System Toolbox.
I have calibrated cameras with using "Camera Calibration Toolbox for Matlab" thus I have Intrinsic parameters of left and right camera and Extrinsic parameters (position of right camera wrt left camera). I have also a pair of rectified pictures and thier disparity map. For estimation of disparity I have used Matlab function disparity(). I know the baseline and the focal length of cameras but my results are still wrong.

baseline = 70 mm
focal length = 25 mm
disparity = 60 pixels
---------------------
depth = baseline * focal length / disparity = 70 * 25 / 60 = 29 mm

But I know that the distance is cca 600 mm. Is this formula right? What about the units? mm * mm / pixel != mm. And especially I would like to use Camera matrix (Intrinsic parameters) for calculation but I didn't figure out how. I would be thankful for any hint.

Dima
  • 37,098
  • 13
  • 69
  • 112
PrincAm
  • 317
  • 5
  • 16

1 Answers1

8

Like you said, you have to convert the unit into mm. And for that you need this formulas

z = (b*F) / (d*s)

mm = (mm * mm) / (pixel * (mm/pixel)) 

Where

  • z = depth in mm
  • b = baseline in mm
  • F = focal length in mm
  • d = depth in pixel
  • s = sensor size in mm/pixel. (Normally it provide in um, so do conversion before).

EDIT

Sometime your focal is in pixel so you don't need to use the sensor size. So just use your formula :

z = b*F / d
mm = mm * pixel / pixel
Vuwox
  • 2,293
  • 17
  • 31
  • You can have detail of this formula [here](http://stackoverflow.com/questions/19421003/how-field-of-view-changes-depth-estimation-in-stereo-vision) – Vuwox Nov 13 '13 at 15:11
  • Careful with your bracketing there; do you mean `z = b*F/d*s` or `z = b*F / (d*s)`? – Rody Oldenhuis Nov 13 '13 at 15:12
  • Thanks @Alexandre this part is solved but unfortunately results are not better. Because I am sure about baseline, focal length and pixel size, disparity estimation probably isn't so accurate. And please do you know how to implement Camera matrix into this calculation? – PrincAm Nov 13 '13 at 15:18
  • @PrincAm I think what you are looking for is that : [Homography](http://stackoverflow.com/questions/7836134/get-3d-coord-from-2d-image-pixel-if-we-know-extrinsic-and-intrinsic-parameters), this article explain a little, but try to search for homography for more informations. And maybe you disparity map isn't good. Is it looking well ? – Vuwox Nov 13 '13 at 15:49
  • @Alexandre Please could you help me with the conversion of sensor size? I found in the camera specs parameters Image sensor: 2/3 (type progressive scan IT CCD), Cell size (H x V): 3.45 x 3.45 um - I assume it is "pixel size". And the resolution of picture is 2448 x 2048 pixels. – PrincAm Nov 14 '13 at 14:38
  • @PrincAm You're right. Cell size means the size of a pixel in um. So you have a 0.00345 mm/pixel cell. – Vuwox Nov 14 '13 at 15:08
  • @Alexandre I have two rectified images. I can determine same point on both of them. On the left image the point has coordinates LX: 1472, LY: 742, on the right image RX:1589, RY: 742. I will get Disparity = RX - LX = 1589 - 1472 = 114 pixels. Baseline is 50 mm, Focal length is 12.5 mm (it is written on the lens of camera) and Pixel size is 0.00345 mm/pixel. Z = (baseline * focal length) / (disparity * pixel size) = (50 * 12.5) / (114 * 0.00345) = 1589 mm. But I know that the distance is 800 mm. Am I doing something wrong? – PrincAm Nov 15 '13 at 09:43
  • Nothing. Theory is always good. But practice never giving what we really want. Can you found the focal in pixel ? To use Z = fB/d ? – Vuwox Nov 15 '13 at 12:59
  • 1
    @Alexandre Thanks a lot for your help. At the end I have used different scenario which is more accurate. I calibrated pairs of cameras with Camera Calibration Toolbox for Matlab from Caltech. Inside this Toolbox are functions for image rectification and stereo triangulation. Calculation is based on intrinsics and extrinsic parameters exported from Toolbox then results have higher accuracy. – PrincAm Nov 19 '13 at 09:48
  • Yes, the Focal is in pixel :) I like this toolbox. I'm using it a lot in my work. – Vuwox Nov 19 '13 at 12:54