I have been trying to calculate a distance between an object and one of my cameras (the same model). After calibration, I successfully got the following matrix. The resolution was 600 x 480.
Left Camera Matrix
[[624.65871068 0. 279.196958 ]
[ 0. 637.37379116 267.35689643]
[ 0. 0. 1. ]]
Left Distortion Coefficients
[[ 0.07129149 -0.32551738 0.00165004 0.00582742 0.55830776]]
Right Camera Matrix
[[628.54997755 0. 278.88536288]
[ 0. 638.88299319 262.29519192]
[ 0. 0. 1. ]]
Right Distortion Coefficients
[[ 0.05281363 -0.20836547 0.0015596 0.00694854 -0.18818856]]
Calibrating cameras together...
Rotation Matrix
[[ 9.78166692e-01 -2.92706245e-02 -2.05750220e-01]
[ 2.87961989e-02 9.99571250e-01 -5.30056367e-03]
[ 2.05817156e-01 -7.39989429e-04 9.78590185e-01]]
Translation Vector
[[6.87024126]
[0.33698621]
[0.82946341]]
If I can detect an object from the both cameras, let us say an object found in pixel (a, b) in the left camera and (c, d) in the right camera. Is there any way that I can get the distance between one camera to the object?
Plus, the script I have implements cv2.stereoRectify and cv2.initUndistortRectifyingMap, which can be used to get the fixed frames and then to calculate a depth map using cv2.StereoBM_create(). Well, to be honest, I am not sure whether this can be used to calculate the distance.
Thanks!
You are computing the distance to the camera, you need to know two parameters from your (normally "LEFT") camera matrix: (1) Baseline; (2) focal length.
Usually we use "LEFT" image as the major reference image, because most of the time, we compute the depth map / disparity image based on left image.
After you have the coordinates of the object (x,y) on the left image, you can inverse the formula and compute the Z-distance as the follows:
Reference: OpenCV - Depth Map from Stereo Images