Main Content

cameraIntrinsicsToOpenCV

Convert camera intrinsic parameters fromMATLABto OpenCV

Since R2021b

Description

example

[intrinsicMatrix,distortionCoefficients) = cameraIntrinsicsToOpenCV (intrinsics)converts a MATLAB®cameraIntrinsicsorcameraParametersobject, specified byintrinsics, to OpenCV camera intrinsic 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). ThecameraIntrinsicsToOpenCVfunction compensates for this difference by subtracting 1 from both thexandy-values for the converted principal point.

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

Examples

collapse all

Create a set of calibration images.

images = imageDatastore(fullfile(toolboxdir('vision'),'visiondata',...'calibration','mono')); imageFileNames = images.Files;

Detect the checkerboard calibration pattern in the images.

[imagePoints,boardSize] = detectCheckerboardPoints(imageFileNames);

Generate the world coordinates of the corners of the squares. Square size is in millimeters.

squareSize = 29; worldPoints = generateCheckerboardPoints(boardSize,squareSize);

Calibrate the camera.

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

Convert the intrinsic parameters to OpenCV format

[intrinsicMatrix,distortionCoefficients] = cameraIntrinsicsToOpenCV(params);

Input Arguments

collapse all

Camera intrinsic parameters, specified as acameraIntrinsicsor acameraParametersobject.

Output Arguments

collapse all

Camera intrinsics matrix formatted for OpenCV, returned as a 3-by-3 matrix of the form:

[ f x 0 c x 0 f y c y 0 0 1 ]

wherefxandfyare the focal lengths in thexandy-directions, and (cx,cy) is the principal point in OpenCV.

Camera radial and tangential distortion coefficients, returned as a five-element vector in the form [k1k2p1p2k3]. The values ofk1,k2, andk3describe the radial distortion andp1andp2describe the tangential distortion, specified in OpenCV.

Version History

Introduced in R2021b