What does the map returned by stereo.compute()
indicate?
The definition of disparity is the distance between two comparable pixels in the left and right images.
However, by running the following code, I obtained a map with the same size as the input photos with values ranging from -16
to 211
. I got confused when it comes with some negative numbers. If these values refer to distance, why would a distance of -16
be possible? (In fact, it has plenty of -16
in the map).
What precisely do these values indicate?
Any help is greatly appreciated.
code:
import cv2 as cv
from matplotlib import pyplot as plt
imgL = cv.imread("data/stereo-corridor_l.png", 0)
print(imgL.shape)
imgR = cv.imread("data/stereo-corridor_r.png", 0)
stereo = cv.StereoBM_create(numDisparities=16, blockSize=17)
disparity = stereo.compute(imgL, imgR)
plt.imshow(disparity, "gray")
plt.show()
two images used:
stereo right
stereo left
CodePudding user response:
As per its documentation, stereo.compute()
computes 16-bit fixed-point disparity map (where each disparity value has 4 fractional bits), whereas other algorithms output 32-bit floating-point disparity map.
In practice this means that the numbers your are having are integers that must be converted to floating point values.
With some little changes, I did:
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
imgL = cv.imread("data/stereo-corridor_l.png", 0)
print(imgL.shape)
imgR = cv.imread("data/stereo-corridor_r.png", 0)
stereo = cv.StereoBM_create(numDisparities=16, blockSize=17)
disparity = stereo.compute(imgL, imgR).astype(np.float32)/16
print(f"Range: {np.min(disparity)} <-> {np.max(disparity)}")
plt.imshow(disparity, "gray")
plt.show()
You can see that the disparity range is -1.0 <-> 12.5625
, where -1 simply means that the matching algorithm couldn't find a matching correspondence.
Instead, without the conversion astype(np.float32)/16
, you would get a fake range of -16 <-> 201
, which is wrong and also 201 is a too high value (you don't have objects so close).
For a full example you may refer to my code here, as part of the SimpleStereo library.