Reconstruct 3-D Scene from Stereo Image Pair Using Semi-Global Matching
Reconstruct a 3-D scene from a pair of stereo images by using the disparity map computed through semi-global matching.
Load the stereo parameters.
load('webcamsSceneReconstruction.mat');Read in the stereo pair of images.
I1 = imread('sceneReconstructionLeft.jpg'); I2 = imread('sceneReconstructionRight.jpg');
Rectify the images.
[J1, J2, reprojectionMatrix] = rectifyStereoImages(I1,I2,stereoParams);
Display the images after rectification.
figure imshow(cat(3,J1(:,:,1),J2(:,:,2:3)));

Compute the disparity.
disparityMap = disparitySGM(im2gray(J1),im2gray(J2)); figure imshow(disparityMap,[0,64]);

Reconstruct the 3-D world coordinates of points corresponding to each pixel from the disparity map.
xyzPoints = reconstructScene(disparityMap,reprojectionMatrix);
Segment out a person located between 3.2 and 3.7 meters away from the camera.
Z = xyzPoints(:,:,3); mask = repmat(Z > 3200 & Z < 3700,[1,1,3]); J1(~mask) = 0; imshow(J1);
