Documentation Center

  • Trial Software
  • Product Updates

reconstructScene

Reconstruct 3-D scene from disparity map

Syntax

  • pointCloud = reconstructScene(disparityMap, stereoParams) example

Description

example

pointCloud = reconstructScene(disparityMap, stereoParams) returns an M-by-N-by-3 array of [x, y, z] coordinates of world points. 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 a 3-D Scene From a Disparity Map

% Load stereoParams.
    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 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 map, 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 reconstructScene function sets the world coordinates corresponding to invalid disparity to NaN.

Pixels with zero disparity correspond to world points which are too far to measure given the resolution of the camera. The reconstructScene 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 it 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, which 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 a double, the function returns the pointCloud as a double. Otherwise, the function returns them as single.

Data Types: single | double

References

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

See Also

| | | | | |

Was this topic helpful?