Main Content


Convert camera intrinsic parameters from MATLAB to OpenCV



[intrinsicMatrix,distortionCoefficients] = cameraIntrinsicsToOpenCV(intrinsics) converts a MATLAB® cameraIntrinsics or cameraParameters object, specified by intrinsics, 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). The cameraIntrinsicsToOpenCV function compensates for this difference by subtracting 1 from both the x and y-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.


collapse all

Create a set of calibration images.

images = imageDatastore(fullfile(toolboxdir('vision'),'visiondata', ...
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, ...

Convert the intrinsic parameters to OpenCV format

[intrinsicMatrix,distortionCoefficients] = cameraIntrinsicsToOpenCV(params);

Input Arguments

collapse all

Camera intrinsic parameters, specified as a cameraIntrinsics or a cameraParameters object.

Output Arguments

collapse all

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


where fx and fy are the focal lengths in the x and y-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 [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.

Introduced in R2021b