Main Content

cameraIntrinsicsFromOpenCV

Convert camera intrinsic parameters from OpenCV toMATLAB

Description

example

intrinsics= cameraIntrinsicsFromOpenCV(intrinsicMatrix,distortionCoefficients,imageSize)converts the OpenCV intrinsics, specified by the input arguments, into a MATLAB®cameraIntrinsicsobjectintrinsics.

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). ThecameraIntrinsicsFromOpenCVfunction compensates for this difference by adding 1 to both of thexandy-values for the converted principal point.

OpenCV intrinsics cannot be converted to a MATLABcameraIntrinsicsobject when:

  • The OpenCV pinhole camera model uses more than five distortion coefficients.

  • An OpenCV fisheye model is used.

In these cases, you can recalibrate the stereo camera using theCamera Calibrator应用程序。

Examples

collapse all

Define OpenCV camera intrinsic parameters in the workspace.

intrinsicMatrix = [729.4644 0 570.6455; 0 728.8196 346.0108; 0 0 1 ]; distortionCoefficients = [-0.4262 0.5460 0.0038 -0.0051 -0.6176];

Define the image size returned by the camera.

imageSize = [712 1072];

Convert the intrinsic parameters from OpenCV to MATLAB format.

intrinsics = cameraIntrinsicsFromOpenCV(intrinsicMatrix,...distortionCoefficients,imageSize);

Load the image to undistort.

filename = fullfile(toolboxdir("vision"),"visiondata","calibration",..."mono","image01.jpg"); I = imread(filename);

Undistort the image and diplay the results.

J = undistortImage(I,intrinsics); imshowpair(I,J,“蒙太奇”); title("Original Image (left) vs. Corrected Image (right)");

Figure contains an axes object. The axes object with title Original Image (left) vs. Corrected Image (right) contains an object of type image.

TheROS camera calibration packageestimates camera intrinsic parameters using the OpenCV camera calibration tools [1]. After calibrating a camera in ROS, you can import its intrinsic parameters to a YAML file using thecamera calibration parserin ROS. To use the calibrated camera with Computer Vision Toolbox™ functions, such asundistortImage, you must read the camera parameters from the YAML file and then convert them into acameraIntrinsicsobject usingcameraIntrinsicsFromOpenCV.

Note:ThecameraIntrinsicsFromOpenCVfunction supports importing camera intrinsic parameters for only those pinhole camera models that use theROS plumb-bobdistortion model.

Read Camera Intrinsic Parameters from a ROS YAML File

Read the camera parameters stored incameraParams.yamlusing the helper functionhelperReadYAML.

intrinsicsParams = helperReadYAML('cameraParams.yaml');

CreatecameraIntrinsicsObject UsingcameraIntrinsicsFromOpenCV

Use thecameraIntrinsicsFromOpenCVfunction to create acameraIntrinsicsobject from the camera matrix and the distortion coefficients.

imageSize = [intrinsicsParams.image_height intrinsicsParams.image_width]; intrinsicMatrix = intrinsicsParams.camera_matrix; distortionCoefficients = intrinsicsParams.distortion_coefficients; intrinsicsObj = cameraIntrinsicsFromOpenCV(intrinsicMatrix,distortionCoefficients,imageSize);

Undistort Image

使用进口相机intrinsicundistortImageto undistort an image captured using the calibrated camera.

% Load the captured image.imageName = fullfile(toolboxdir('vision'),'visiondata','calibration','stereo','left','left01.png'); I = imread(imageName);% Undistort the image.J = undistortImage(I,intrinsicsObj,'OutputView','full');% Display the result.figure montage({I,J})

Figure contains an axes object. The axes object contains an object of type image.

Supporting Functions

helperReadYAML

ThehelperReadYAMLfunction reads the monocular camera parameters from the input YAML file that was exported from ROS.

functioncameraParams = helperReadYAML(filename)% helperReadYAML reads a ROS YAML file, filename, and returns a structure% with these fields: image_width, image_height, camera_name,% camera_matrix, distortion_model, distortion_coefficients,% rectification_matrix, and projection_matrix. These fields are stored% in the YAML file colon separated from their values in different lines.f = fopen(filename,'r'); stringFields = {'camera_name','distortion_model'};while~feof(f) [name,value,isEmptyLine] = helperReadYAMLLine(f);ifisEmptyLinecontinueendif~isempty(value)% Convert all values to numbers except for known string% fields.if~any(contains(name, stringFields)) value = str2num(value);%#okendelse% An empty value in ROS YAML files indicates a matrix in% upcoming lines. Read the matrix from the upcoming lines.value = helperReadYAMLMatrix(f);end% Store post-processed value.cameraParams.(name) = value;endfclose(f);end

helperReadYAMLMatrix

ThehelperReadYAMLMatrixfunction reads the rows, columns and data fields of a matrix in the ROS YAML file.

functionmatrix = helperReadYAMLMatrix(f)% helperReadYAMLMatrix reads a matrix from the ROS YAML file. A matrix in% a ROS YAML file has three fields: rows, columns and data. rows and col% describe the matrix size. data is a continguous array of the matrix% elements in row-major order. This helper function assumes the presence% of all three fields of a matrix to return the correct matrix.numRows = 0; numCols = 0; data = [];% Read numRows, numCols and matrix data.while~feof(f) [name,value,isEmptyLine] = helperReadYAMLLine(f);ifisEmptyLinecontinueendswitchnamecase'rows'numRows = str2num(value);%#okcase'cols'numCols = str2num(value);%#okcase'data'data = str2num(value);%#ok% Terminate the while loop as data is the last% field of a matrix in the ROS YAML file.breakotherwise% Terminate the while loop if any other field is% encountered.breakendendifnumel(data) == numRows*numCols% Reshape the matrix using row-major order.matrix = reshape(data,[numCols numRows])';endend

helperReadYAMLLine

ThehelperReadYAMLLinefunction reads a line of a ROS YAML file.

function[name,value,isEmptyLine] = helperReadYAMLLine(f)% Read line from file.line = fgetl(f);% Trim leading and trailing whitespaces.line = strtrim(line);ifisempty(line) || line(1)=='//www.tatmou.com/kr/kr/help/vision/ref/#'% Empty line or comment.name =''; value =''; isEmptyLine = true;else% Split the line to get name and value.c = strsplit(line,':'); assert(length(c)==2,'Unexpected file format') name = c{1}; value = strtrim(c{2});% Trim leading whitespace.isEmptyLine = false;endend

References

[1]http://wiki.ros.org/camera_calibration

Input Arguments

collapse all

Camera intrinsic matrix from OpenCV, specified 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 from OpenCV, specified as a five-element vector of the form [k1k2p1p2k3]. The values ofk1,k2, andk3describe the radial distortion andp1andp2describe the tangential distortion, specified in OpenCV.

图像size, specified as a 2-element vector in the form [mrows,ncols].

Output Arguments

collapse all

Camera intrinsic parameters, returned as acameraIntrinsicsobject.

Version History

Introduced in R2021b