Main Content

Reconstruct 3-D Scene from Disparity Map

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)),'InitialMagnification',50);

Figure contains an axes object. The hidden axes object contains an object of type image.

Compute the disparity.

disparityMap = disparitySGM(im2gray(J1),im2gray(J2));
figure 
imshow(disparityMap,[0,64],'InitialMagnification',50);

Figure contains an axes object. The hidden axes object contains an object of type image.

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,'InitialMagnification',50);

Figure contains an axes object. The hidden axes object contains an object of type image.