OpenCV - 将相机矩阵和畸变系数存储为 Mat

OpenCV - store camera matrix and distortion coefficients as Mat

我已经使用示例 OpenCV 程序计算了我相机的相机矩阵和畸变系数,并生成了一个包含相关数据的 xml 文件。

我正在尝试通过 undistort 函数使用它,但我不确定如何将这些值存储为 Mat

Mat cameraMatrix;
Mat distortionCoefficients;
undistort(image, newImage, cameraMatrix, distortionCoefficients);

我试过:

Mat cameraMatrix = 1.7514028018776246e+03 0. 1.2635000000000000e+03 0. 1.7514028018776246e+03 9.2750000000000000e+02 0. 0. 1.;
Mat distortionCoefficients = 1.3287735059062630e-01 -6.8376776294978103e-01 0. 0. 8.1215478360827675e-01;

我是否需要尝试为 Mat var 指定一系列行和列,然后为每个值分配一个索引?

您可以在 undistort 的 OpenCV 文档中看到:

相机矩阵是一个3x3矩阵:

 fx   0  cx
  0  fy  cy
  0   0   1 

您可以创建为:

Mat cameraMatrix = (Mat1d(3, 3) << fx, 0, cx, 0, fy, cy, 0, 0, 1);

distortionCoefficients 是向量或4、5或8个参数:

k1, k2, p1, p2 [, k3 [, k4, k5, k6]]

您可以创建为:

Mat distortionCoefficients = (Mat1d(1, 4) << k1, k2, p1, p2);
Mat distortionCoefficients = (Mat1d(1, 5) << k1, k2, p1, p2, k3);
Mat distortionCoefficients = (Mat1d(1, 8) << k1, k2, p1, p2, k3, k4, k5, k6);

你可以在OpenCV documentation

上找到参数的含义