Block Matching Disparity
Implement block matching for stereo disparity estimation, a fundamental technique in stereo vision. This task involves finding the corresponding points between two images of the same scene taken from different viewpoints.
The concept of disparity is crucial here, as it represents the difference in the position of a point between the left and right images, given by . This disparity is inversely proportional to the depth of the point in the scene.
- For each pixel in the left image, find the best matching block in the right image along the epipolar line, which for rectified images, is the same row.
- Calculate the disparity for each pixel based on the position of the best match.
The matching cost is the sum of squared differences (SSD) between the block centred at in the left image and the block centred at in the right image, for each candidate . The disparity is the with the smallest SSD (the smallest wins a tie).
Pixels within of the top, bottom or right edge, and pixels with , are left at disparity .
This technique is widely used in depth estimation and 3D reconstruction applications.
Example
Stereo pair
Disparity map
- Inputs:
leftandrightare a rectified grayscale pair,block_sizeis the square patch side , andmax_dispcounts the candidate shifts . - Worked example: a pair with every row on the left and on the right (scene shifted 2 pixels left), , 3 shifts.
- At the centre-row pixel in column 5, the left block rows are ; compare with the right block centred on column :
- :
- :
- : , a perfect match, so the disparity is .
- Output: a disparity map the input's shape. Columns 4 to 6 of the middle row get ; columns 0 to 3 stay because (the description's rule); column 7 and rows 0 and 2 stay because the block would cross the right, top or bottom edge.
- Notice: larger disparity means a closer object.
Constraints
- Input images: 2D numpy arrays (grayscale) with shape (height, width) and pixel values in [0, 255]
- Block size: odd integer between 3 and 31
- Disparity range: integer values between 0 and width // 2
- Output disparity map: 2D numpy array with shape (height, width) and dtype uint16
- The input images are assumed to be rectified, with the epipolar line being the same row for corresponding pixels