简体   繁体   English

从yml文件opencv C ++初始化相机参数

[英]initialize Camera parameters from yml file opencv C++

I saved the CameraParams which is defined in https://github.com/opencv/opencv/blob/master/samples/cpp/stitching_detailed.cpp . 我保存了在https://github.com/opencv/opencv/blob/master/samples/cpp/stitching_detailed.cpp中定义的CameraParams。 Here is the how camera parameter structure looks like: 相机参数结构如下所示:

struct CV_EXPORTS CameraParams
{
    CameraParams();
    CameraParams(const CameraParams& other);
    const CameraParams& operator =(const CameraParams& other);
    Mat K() const;

    double focal; // Focal length
    double aspect; // Aspect ratio
    double ppx; // Principal point X
    double ppy; // Principal point Y
    Mat R; // Rotation
    Mat t; // Translation
};

I wrote this camera parameter to yml file using the following script: 我使用以下脚本将此相机参数写入yml文件:

<cameraParams> cameras;
FileStorage fs(fileName, FileStorage::WRITE);
fs << "K" << cameras.K();
fs << "R" << cameras.R;
fs << "t" << cameras.t;
fs << "ppx" << cameras.ppx;
fs << "ppy" << cameras.ppy;
fs << "focal" << cameras.focal;
fs << "aspect" << cameras.aspect;
fs.release();

and here is the how the file content look like: 这是文件内容的样子:

%YAML:1.0
---
K: !!opencv-matrix
  rows: 3
  cols: 3
  dt: d
  data: [ 2.4125938056164614e+003, 0., 447., 0.,
      2.4125938056164614e+003, 3.3550000000000000e+002, 0., 0., 1. ]
R: !!opencv-matrix
  rows: 3
  cols: 3
  dt: f
  data: [ -9.67408061e-001, 7.91518241e-002, -2.40534484e-001,
      -4.17553373e-002, -9.86752093e-001, -1.56770796e-001,
      -2.49756604e-001, -1.41617730e-001, 9.57896829e-001 ]
t: !!opencv-matrix
  rows: 3
  cols: 1
  dt: d
  data: [ 0., 0., 0. ]
ppx: 447.
ppy: 3.3550000000000000e+002
focal: 2.4125938056164614e+003
aspect: 1.

Now I want to use these same parameters and read them back in but this is not working (gives a run time error). 现在,我想使用这些相同的参数并读回它们,但这不起作用(产生运行时错误)。 Here is the read function used: 这是使用的读取功能:

    Mat K, R, t;
    double ppx, ppy, focal, aspect;
    FileStorage fs(fileName, FileStorage::READ);
    fs["K"] >> K;
    fs["R"] >> R;
    fs["t"] >> t;
    fs["ppx"] >> ppx;
    fs["ppy"] >> ppy;
    fs["focal"] >> focal;
    fs["aspect"] >> aspect;
    camerasTest[i].K() = (Mat)K;
    camerasTest[i].R = R;
    camerasTest[i].t = t;
    camerasTest[i].ppx = (double)ppx;
    camerasTest[i].ppy = (double)ppy;
    camerasTest[i].focal = (double)focal;
    camerasTest[i].aspect = (double)aspect;
    fs.release()

How can I solve this? 我该如何解决?

There was an error somewhere in initialization of 'K'. 初始化“ K”时某处出错。 I made this change and it started working. 我进行了更改,它开始起作用。

for (int i = 0; i < num_images; ++i)
{
    Mat_<float> K;
    cameras[i].K().convertTo(K, CV_32F);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM