rectifyStereoImages
Rectify a pair of stereo images
Description
[J1,J2]
= rectifyStereoImages(I1,I2,stereoParams) returns
undistorted and rectified versions of I1 and I2 input
images using the stereo parameters stored in the stereoParams object.
Stereo image rectification projects images onto a common image plane in such a way that the
corresponding points have the same row
coordinates. This image projection makes the image
appear as though the two cameras are parallel. Use
the disparityBM or disparitySGM functions to compute a
disparity map from the rectified images for 3-D
scene reconstruction.
[J1,J2]
= rectifyStereoImages(I1,I2,tform1,tform2) returns
rectified versions of I1 and I2 input
images by applying projective transformations tform1 and tform2.
The projective transformations are returned by the estimateUncalibratedRectification function.
[J1,J2]
= rectifyStereoImages(___,interp) additionally
specifies the interpolation method to use for rectified images. You
can specify the method as 'nearest', 'linear',
or 'cubic'.
[J1,J2]
= rectifyStereoImages(___,Name,Value) uses
additional options specified by one or more Name,Value pair
arguments.
Examples
Rectify Stereo Images
Specify images containing a checkerboard for calibration.
imageDir = fullfile(toolboxdir('vision'),'visiondata', ...
'calibration','stereo');
leftImages = imageDatastore(fullfile(imageDir,'left'));
rightImages = imageDatastore(fullfile(imageDir,'right'));
Detect the checkerboards.
[imagePoints,boardSize] = detectCheckerboardPoints(...
leftImages.Files,rightImages.Files);
Specify world coordinates of checkerboard keypoints.
squareSizeInMillimeters = 108;
worldPoints = generateCheckerboardPoints(boardSize,squareSizeInMillimeters);
Read in the images.
I1 = readimage(leftImages,1);
I2 = readimage(rightImages,1);
imageSize = [size(I1,1),size(I1,2)];
Calibrate the stereo camera system.
stereoParams = estimateCameraParameters(imagePoints,worldPoints, ...
'ImageSize',imageSize);
Rectify the images using 'full' output view.
[J1_full,J2_full] = rectifyStereoImages(I1,I2,stereoParams, ...
'OutputView','full');
Display the result for 'full' output view.
figure;
imshow(stereoAnaglyph(J1_full,J2_full));
Rectify the images using 'valid' output view. This is most suitable for computing disparity.
[J1_valid,J2_valid] = rectifyStereoImages(I1,I2,stereoParams, ...
'OutputView','valid');
Display the result for 'valid' output view.
figure;
imshow(stereoAnaglyph(J1_valid,J2_valid));
Input Arguments
I1 — Input image 1
M-by-N-by-3 truecolor
image | M-by-N 2-D grayscale
image
Input image corresponding to camera 1, specified as an M-by-N-by-3
truecolor image or an M-by-N 2-D
grayscale array. Input images I1 and I2 must
also be real, finite, and nonsparse. The input images must be the
same class.
Data Types:uint8 | uint16 | int16 | single | double | logical
I2 — Input image 2
M-by-N-by-3 truecolor
image | M-by-N 2-D truecolor
image
Input image corresponding to camera 2, specified as an M-by-N-by-3
truecolor image or an M-by-N 2-D
grayscale array. Input images I1 and I2 must
be real, finite, and nonsparse. The input images must also be the
same class.
Data Types:uint8 | uint16 | int16 | single | double | logical
stereoParams — Stereo camera system parameters
stereoParameters object
Stereo camera system parameters, specified as a stereoParameters object.
Data Types:uint8 | uint16 | int16 | single | double
tform1 — Projective transformation
3-by-3 matrix | projective2d object
Projective transformations for image 1, specified as a 3-by-3 matrix returned by the estimateUncalibratedRectification function or a projective2d object.
tform2 — Projective transformation
3-by-3 matrix | projective2d object
Projective transformations for image 2, specified as a 3-by-3 matrix returned by the estimateUncalibratedRectification function or a projective2d object.
interp — Interpolation method
'linear' (default) | 'nearest' | 'cubic'
Interpolation method, specified as either 'linear',
'nearest', or
'cubic'.
Name-Value Pair Arguments
Specify optional
comma-separated pairs of Name,Value arguments. Name is
the argument name and Value is the corresponding value.
Name must appear inside quotes. You can specify several name and value
pair arguments in any order as
Name1,Value1,...,NameN,ValueN.Example:'OutputView', 'valid' sets the
'OutputView' property to
'valid'.
'OutputView' — Size of rectified images
'valid' (default) | 'full'
Size of rectified images, specified as the comma-separated pair consisting of
'OutputView' and either
'full' or
'valid'. When you set this
parameter to 'full', the
rectified images include all pixels from the
original images. When you set this value to
'valid', the output images are
cropped to the size of the largest common
rectangle containing valid pixels.
When there is no overlap between rectified images, set the OutputView to 'full'.
'FillValues' — Output pixel fill values
array of scalar values
Output pixel fill values, specified as the comma-separated pair
consisting of 'FillValues' and an array of one
or more scalar values. When the corresponding inverse-transformed
location in the input image is completely outside the input image
boundaries, use the fill values for output pixels. If I1 and I2 are
2-D grayscale images, then you must set 'FillValues'
to a scalar. If I1 and I2 are
truecolor images, then you can set 'FillValues'
to a scalar or a 3-element vector of RGB values.
Output Arguments
J1 — Undistorted and rectified image 1
M-by-N-by-3 truecolor
image | M-by-N 2-D grayscale
image
Undistorted and rectified version of I1,
returned as an M-by-N-by-3 truecolor
image or as an M-by-N 2-D grayscale
image.
Stereo image rectification projects images onto a common image plane in such a way that the
corresponding points have the same row
coordinates. This image projection makes the image
appear as though the two cameras are parallel. Use
the disparityBM or disparitySGM functions to compute a
disparity map from the rectified images for 3-D
scene reconstruction.
J2 — Undistorted and rectified image 2
M-by-N-by-3 truecolor
image | M-by-N 2-D grayscale
image
Undistorted and rectified version of I2,
returned as an M-by-N-by-3 truecolor
image or as an M-by-N 2-D grayscale
image.
Stereo image rectification projects images onto a common image plane in such a way that the
corresponding points have the same row
coordinates. This image projection makes the image
appear as though the two cameras are parallel. Use
the disparityBM or disparitySGM functions to compute a
disparity map from the rectified images for 3-D
scene reconstruction.
Tips
The
Computer Vision Toolbox™ rectification algorithm requires
that the epipole for each image lie outside of the
image. If the epipole lies within the image, you
can first transform the images into polar
coordinates as described in the rectification
method proposed by Marc Pollefeys, Reinhard Koch,
and Luc Van Gool [2].
References
[1] G. Bradski and A. Kaehler, Learning OpenCV
: Computer Vision with the OpenCV Library. Sebastopol,
CA: O'Reilly, 2008.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
Use in a MATLAB Function block is not supported.
'interp' and 'OutputView' must
be compile-time constants.
To generate code, use the toStruct function to pass stereoParameters to the
rectifyStereoImages
function.
Introduced in R2014a