I want to estimate the distance of an object to my camera. This must be using Opencv. I read that I have to use 2 cameras in stead of one and I found some code with Matlab, but I don't have any experience in it. Any help will be appreciated.
-
Do you know the object's and the sensor's dimensions? – Niko Apr 27 '13 at 09:06
-
I know object dimension only. What do you mean by sensor? I have camera – Houssam Badri Apr 27 '13 at 09:25
-
You will definitely need to known the size of a pixel on your camera's sensor in a world-unit, e.g., mm. If you don't have this information but know the focal length of the camera, you can determine the pixel size via calibration. – Niko Apr 27 '13 at 09:37
3 Answers
Yes, It is definitely possible to calculate depth with two cameras using a simple pinhole camera model:
depth = bcm*fcm/(sensorWidthCm*disparity/(float)horizontalResolution);
Here, in order to find average disparity (horizontal pixel location difference of keypoints) you can use SurfDescriptorExtractor
and to find match pairs, you can use BFMatcher
as in the link:
http://docs.opencv.org/doc/tutorials/features2d/feature_description/feature_description.html
fcm, bcm, sensorWidthCm, and horizontalResolution are all camera parameters: fcm is camera focal length, bcm is the separation of cameras, sensorWidthCm is camera sensor width, and horizontalResolution is pixel number in horizontal direction. In fact you do not separately need all these parameters. You just need to experiment with some known depth to find the ratio:bcm*fcm/sensorWidthCm
Horizontal image resolution is already known during image capture.

- 7,789
- 1
- 26
- 48
-
Thanks, but what do you mean by camera sensor width? and how can I get such parameter? – Houssam Badri Apr 27 '13 at 09:34
-
It is the width of image plane of the camera and real size of the image projected onto the plane. It is not possible to directly calculate this parameter, therefore you need to calibrate with an object with a known depth to extract bcm*fcm/sensorWidthCm ratio using the equation above. – fatihk Apr 27 '13 at 09:38
see this answer:
distance to object (mm) = focal length (mm) * real height of the object (mm) * image height (pixels) --------------------------------------------------------------------------- object height (pixels) * sensor height (mm)