Contents

reconstructScene

Reconstruct 3-D scene from disparity map

Syntax

  • pointCloud = reconstructScene(disparityMap,stereoParams) example

Description

example

pointCloud = reconstructScene(disparityMap,stereoParams) returns an array of 3-D world point coordinates that reconstruct a scene from a disparity map. The world points correspond to the pixels in the disparityMap input. The 3-D world coordinates are relative to the optical center of camera 1 in the stereo system represented by stereoParams. The stereoParams input must be the same input that you use to rectify the stereo images corresponding to the disparity map.

Examples

expand all

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] = rectifyStereoImages(I1, I2, stereoParams);

Display the images after rectification.

figure; imshow(cat(3, J1(:,:,1), J2(:,:,2:3)), 'InitialMagnification', 50);

Compute the disparity.

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

Reconstruct the 3-D world coordinates of points corresponding to each pixel from the disparity map.

pointCloud = reconstructScene(disparityMap, stereoParams);

Segment out a person located between 3.2 and 3.7 meters away from the camera.

Z = pointCloud(:, :, 3);
mask = repmat(Z > 3200 & Z < 3700, [1, 1, 3]);
J1(~mask) = 0;
imshow(J1, 'InitialMagnification', 50);

Input Arguments

expand all

disparityMap — Disparity image2-D array

Disparity image, specified as a 2-D array of disparity values for pixels in image 1 of a stereo pair. The disparity function provides the input disparity map.

The map can contain invalid values marked by -realmax('single') . These values correspond to pixels in image 1, which the disparity function did not match in image 2. The function sets the world coordinates corresponding to invalid disparity to NaN.

Pixels with zero disparity correspond to world points that are too far away to measure, given the resolution of the camera. The function sets the world coordinates corresponding to zero disparity to Inf.

When you specify the disparityMap input as a double, the function returns the pointCloud as a double. Otherwise, the function returns the coordinates as single.

Data Types: single | double

stereoParams — Stereo camera system parametersstereoParameters object

Stereo camera system parameters, specified as a stereoParameters object.

Data Types: uint8 | uint16 | int16 | single | double

Output Arguments

expand all

pointCloud — Coordinates of world pointsM-by-N-by-3 array

Coordinates of world points, returned as an M-by-N-by-3 array. The array contains the [x, y, z] coordinates of world points that correspond to the pixels in the disparityMap input. pointCloud(:, :, 1) contains the x world coordinates of points corresponding to the pixels in the disparity map. pointCloud(:, :, 2) contains the y world coordinates, and pointCloud(:, :, 3) contains the z world coordinates. The 3-D world coordinates are relative to the optical center of camera 1 in the stereo system. When you specify the disparityMap input as double, the function returns the pointCloud output as double. Otherwise, the function returns it as single.

Data Types: single | double

References

[1] G. Bradski and A. Kaehler, Learning OpenCV : Computer Vision with the OpenCV Library, Sebastopol, CA: O'Reilly, 2008.

Was this topic helpful?