0
votes

From my understanding, we can use the following formula to obtain the real distance from depth map. The focal length of a pi camera in terms of pixel is 2571.4, while the distance betweeen my two cameras is 0.065m. so using this formula based on the triangulation method,

Distance = (focal length * distance between 2 camera)/disparity map.

I also generated a disparity map with the following code:

stereo = cv2.StereoBM_create(numDisparities=48, blockSize=25)
disparity = steeo.compute(rectified_pair[0], rectified_pair[1])
norm_image = cv2.normalize(disparity, None, alpha = 0, beta =1, norm_type=cv2.NORM_MINMAX, dtype=cv.CV_32F)

f = 2571.4 
b = 0.065
distance = (b*f)/disparity
cv2.imshow(norm_image)

I realize my 'disparity' image range starts from -16. I am getting negative values for both set of codes and I am unsure if this is a correct method to calculate distance. Also, I have calibrated my cameras. Thank you!

1

1 Answers

0
votes

Firstly, you should read the values of focal length and baseline from the calibration file and not hardcode it. If it is done properly, the way I used to calculate depth is going pixel by pixel. So I would apply the same formula pixel by pixel like this :

depthmap.at(i,j) = (b*f) / disparitymap.at(i,j).

You can maybe try this. Mostly there would be any negative values in the disparity map that is return by StereoBM compute. So there might a problem only after that process.