Reputation: 2509
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.
Upvotes: 3
Views: 8092
Reputation: 2332
You can use solvePnP function, if You know the precise dimensions of the object, and it Is easy to detect. You will also need to run camera calibration to set the unit measure in camera's coordinates.
Upvotes: 1
Reputation: 7919
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.
Upvotes: 3