Main Content

stereoParametersToOpenCV

Convert stereo camera parameters from MATLAB to OpenCV

Since R2021b

Description

[intrinsicMatrix1,distortionCoefficients1,intrinsicMatrix2,distortionCoefficients2,rotationOfCamera2,translationOfCamera2,imageSize] = stereoParametersToOpenCV(stereoParams) converts a MATLAB® stereoParameters object stereoParams to OpenCV stereo parameters.

The OpenCV spatial coordinate system specifies the upper-left pixel center at (0,0), whereas the MATLAB spatial coordinate system specifies the pixel center at (1,1). The stereoParametersToOpenCV function compensates for this difference by subtracting 1 to both of the x and y-values for the converted principal point.

OpenCV stereo parameters do not include the skew of a pinhole camera model. Therefore, only the intrinsics estimated without the skew can be exported to OpenCV.

example

Examples

collapse all

Specify the calibration images.

leftImages = imageDatastore(fullfile(toolboxdir('vision'),'visiondata', ...
   'calibration','stereo','left'));
rightImages = imageDatastore(fullfile(toolboxdir('vision'),'visiondata', ...
   'calibration','stereo','right'));

Detect the checkerboards in the image pairs.

[imagePoints,boardSize] = detectCheckerboardPoints(leftImages.Files,rightImages.Files);

Specify the world coordinates of the checkerboard keypoints in millimeters.

squareSize = 108;
worldPoints = patternWorldPoints('checkerboard',boardSize,squareSize);

Calibrate the stereo camera system. Both cameras have the same resolution.

I = readimage(leftImages,1); 
imageSize = [size(I,1),size(I,2)];
stereoParams = estimateCameraParameters(imagePoints,worldPoints, ...
   ImageSize=imageSize);

Convert the MATLAB stereo parameters object to the OpenCV format.

[intrinsicMatrix1,distortionCoefficients1,intrinsicMatrix2, ...
   distortionCoefficients2,rotationOfCamera2,translationOfCamera2] =... 
   stereoParametersToOpenCV(stereoParams);

Input Arguments

collapse all

Stereo parameters, specified as a stereoParameters object.

Output Arguments

collapse all

Intrinsics matrix of camera 1 formatted for OpenCV, returned as a 3-by-3 matrix of the form:

[fx0cx0fycy001]

where fx and fy are the focal lengths in the x and y-directions, and (cx,cy) is the principal point in specified in the OpenCV input.

Intrinsics matrix of camera 2 formatted for OpenCV, returned as a 3-by-3 matrix of the form:

[fx0cx0fycy001]

where fx and fy are the focal lengths in the x and y-directions, and (cx,cy) is the principal point in specified in the OpenCV input.

Camera 1 radial and tangential distortion coefficients, returned as a five-element vector in the form [k1 k2 p1 p2 k3]. The values of k1, k2, and k3 describe the radial distortion and p1 and p2 describe the tangential distortion, specified in OpenCV.

Camera 2 radial and tangential distortion coefficients, returned as a five-element vector in the form [k1 k2 p1 p2 k3]. The values of k1, k2, and k3 describe the radial distortion and p1 and p2 describe the tangential distortion, specified in OpenCV.

Rotation of camera 2 relative to camera 1 from OpenCV, specified as a 3-by-3 matrix.

Translation of camera 2 relative to camera 1 from OpenCV, specified as a three-element vector.

Image size, specified as a two-element vector in the form [mrows,ncols].

Version History

Introduced in R2021b