OpenCV
4.7.0dev
Open Source Computer Vision

In this session,
In the last session, we saw basic concepts like epipolar constraints and other related terms. We also saw that if we have two images of same scene, we can get depth information from that in an intuitive way. Below is an image and some simple mathematical formulas which prove that intuition. (Image Courtesy :
The above diagram contains equivalent triangles. Writing their equivalent equations will yield us following result:
\[disparity = x  x' = \frac{Bf}{Z}\]
\(x\) and \(x'\) are the distance between points in image plane corresponding to the scene point 3D and their camera center. \(B\) is the distance between two cameras (which we know) and \(f\) is the focal length of camera (already known). So in short, the above equation says that the depth of a point in a scene is inversely proportional to the difference in distance of corresponding image points and their camera centers. So with this information, we can derive the depth of all pixels in an image.
So it finds corresponding matches between two images. We have already seen how epiline constraint make this operation faster and accurate. Once it finds matches, it finds the disparity. Let's see how we can do it with OpenCV.
Below code snippet shows a simple procedure to create a disparity map.
Below image contains the original image (left) and its disparity map (right). As you can see, the result is contaminated with high degree of noise. By adjusting the values of numDisparities and blockSize, you can get a better result.
There are some parameters when you get familiar with StereoBM, and you may need to fine tune the parameters to get better and smooth results. Parameters:
These parameters are set with dedicated setters and getters after the algoritm initialization, such as setTextureThreshold
, setSpeckleRange
, setUniquenessRatio
, and more. See cv::StereoBM documentation for details.