koh edgar
koh edgar

Reputation: 1

Getting depth map from disparity map

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!

Upvotes: 0

Views: 5135

Answers (2)

dmonkoff
dmonkoff

Reputation: 21

If you calibrated and rectified cameras by yourself, openCV provides a function for disparity-to-depth transform https://docs.opencv.org/3.4/d9/d0c/group__calib3d.html#ga1bc1152bd57d63bc524204f21fde6e02

Upvotes: 2

Madhu Soodhan
Madhu Soodhan

Reputation: 180

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.

Upvotes: 1

Related Questions