Kronephon Kronephon - 1 month ago 5
C++ Question

Assigning values on a matrix

I'm running into an issue with c++. My idea is to fill an intrinsic matrix with predefined values for camera calibration, except I don't seem to be doing it properly.

Mat intrinsic_Matrix(3,3, CV_64F);
float fx = (4032 * 4)/6.69; //2410.76
float fy = (3024 * 4)/5.55; //2179.45

float cx = 2016.0;
float cy = 1512.0;

cout << intrinsic_Matrix << endl;

cout << endl;

intrinsic_Matrix.at<float>(0,0) = fx;
intrinsic_Matrix.at<float>(0,1) = 0.0;
intrinsic_Matrix.at<float>(0,2) = cx;

intrinsic_Matrix.at<float>(1,0) = 0.0;
intrinsic_Matrix.at<float>(1,1) = fy;
intrinsic_Matrix.at<float>(1,2) = cy;

intrinsic_Matrix.at<float>(2,0) = 0.0;
intrinsic_Matrix.at<float>(2,1) = 0.0;
intrinsic_Matrix.at<float>(2,2) = 1.0;

cout << intrinsic_Matrix << endl;
calibrateCamera(frameWorldPoints, bothImagePoints, cvSize(referimages[0].image.cols, referimages[0].image.rows), intrinsic_Matrix, distortion_coeffs, rvecs, tvecs, CV_CALIB_USE_INTRINSIC_GUESS);


The output on both prints makes little sense. Looks like I'm looking at pointer values or uninitialized values. Also, opencv throws a "intrinsic matrix not correct" exception when I try to calibrate it so I'm sure it's not that I'm printing it wrong.

Answer

You initialize MAT with 64F but use floats. Either change to 32F as follows

Mat intrinsic_Matrix(3,3, CV_32F);
float fx = (4032 * 4)/6.69; //2410.76
float fy = (3024 * 4)/5.55; //2179.45

    float cx = 2016.0;
    float cy = 1512.0;

    cout << intrinsic_Matrix << endl;

    cout << endl;

    intrinsic_Matrix.at<float>(0,0) = fx;
    intrinsic_Matrix.at<float>(0,1) = 0.0;
    intrinsic_Matrix.at<float>(0,2) = cx;

    intrinsic_Matrix.at<float>(1,0) = 0.0;
    intrinsic_Matrix.at<float>(1,1) = fy;
    intrinsic_Matrix.at<float>(1,2) = cy;

    intrinsic_Matrix.at<float>(2,0) = 0.0;
    intrinsic_Matrix.at<float>(2,1) = 0.0;
    intrinsic_Matrix.at<float>(2,2) = 1.0;

    cout << intrinsic_Matrix << endl;
    calibrateCamera(frameWorldPoints, bothImagePoints, cvSize(referimages[0].image.cols, referimages[0].image.rows), intrinsic_Matrix, distortion_coeffs, rvecs, tvecs, CV_CALIB_USE_INTRINSIC_GUESS);

or

change all your variables to double, keeping intrinsic_matrix initialized as 64F

Mat intrinsic_Matrix(3,3, CV_64F);
double fx = (4032 * 4)/6.69; //2410.76
double fy = (3024 * 4)/5.55; //2179.45

double cx = 2016.0;
double cy = 1512.0;

cout << intrinsic_Matrix << endl;

cout << endl;

intrinsic_Matrix.at<double>(0,0) = fx;
intrinsic_Matrix.at<double>(0,1) = 0.0;
intrinsic_Matrix.at<double>(0,2) = cx;

intrinsic_Matrix.at<double>(1,0) = 0.0;
intrinsic_Matrix.at<double>(1,1) = fy;
intrinsic_Matrix.at<double>(1,2) = cy;

intrinsic_Matrix.at<double>(2,0) = 0.0;
intrinsic_Matrix.at<double>(2,1) = 0.0;
intrinsic_Matrix.at<double>(2,2) = 1.0;

cout << intrinsic_Matrix << endl;
Comments