load camera parameters from yml file cpp

asked 2017-03-05 06:10:19 -0600

deathracer99 gravatar image

I saved the CameraParams which is defined in Opencv Stitching Detailed sample. 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 the camera Parameters calculated from the stitching_detailed.cpp using the following code:

  <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();

Here is how the file content looked 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.

How can I now load these camera parameters to initialize CameraParams. This is the read function I was using:

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["ppx"] >> 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()

but this gives Run time error. So what transformation can be done

edit retag flag offensive close merge delete

Comments

an error here

fs["ppx"] >> ppx;
fs["ppx"] >> ppy;

second line must be

 fs["ppy"] >> ppy;

"this gives Run time error." what's error ? CameraTest is an array have you initilized this array ?

camerasTest[i].K() = (Mat)K; ???

LBerger gravatar imageLBerger ( 2017-03-05 09:38:57 -0600 )edit

sorry ppy thing was a typo. Yes I have initialized the array as vector<cameraparams> camerasTest(num_images) //where num_images=8

deathracer99 gravatar imagedeathracer99 ( 2017-03-06 00:40:01 -0600 )edit

and the error ? and camerasTest[i].K() = (Mat)K; ???

LBerger gravatar imageLBerger ( 2017-03-06 01:32:08 -0600 )edit

if it has:

 Mat K() const;

you're not allowed to assign anything to it here

camerasTest[i].K() = (Mat)K;
berak gravatar imageberak ( 2017-03-06 01:58:29 -0600 )edit

Then how can I initialize it ?

deathracer99 gravatar imagedeathracer99 ( 2017-03-06 23:16:39 -0600 )edit

Copy and paste some source code is not enough sometime to learn a language like C++ java...

LBerger gravatar imageLBerger ( 2017-03-07 01:07:27 -0600 )edit

@LBerger I accept that fact totally, but I am short on time that is why this approach. Although side by side I am trying to learn C++. Thanks for the help

deathracer99 gravatar imagedeathracer99 ( 2017-03-07 04:58:06 -0600 )edit

actually, i think, the stitching-detailled example (and maybe the lib itself) is pretty inconsistent wrt. this.

berak gravatar imageberak ( 2017-03-07 05:19:53 -0600 )edit