Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

  1. Is it possible to measure distance to object with camera?

  2. 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)
    

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top