5

I've used the sample OpenCV program to calculate the camera matrix and distortion coefficients from my camera and produced an xml file with the relevant data.

I'm trying to use it through the undistort function but I'm not sure how to store the values as a Mat.

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

I've tried:

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;

Do I need to try and specify a series of rows and columns to the Mat var and then assign each value an index?

Colin747
  • 4,955
  • 18
  • 70
  • 118
  • So you need to know how to create a matrix with some values? Or how to load a matrix from the xml? – Miki Dec 01 '15 at 15:06
  • I just need to know how to apply these values to their respective variables to use in the `undistort` function. The first method would suffice. – Colin747 Dec 01 '15 at 15:07
  • Check [here](http://docs.opencv.org/2.4/modules/imgproc/doc/geometric_transformations.html#undistort) and [here](http://docs.opencv.org/2.4/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.html) for the order of parameters (if you still haven't). Then you can create a matrix like: `Mat cameraMatrix = (Mat1d(3, 3) << fx, 0, cx, 0, fx, cx, 0, 0, 1);` – Miki Dec 01 '15 at 15:11
  • Thanks, what parameters in place of `(3,3)`do I need to provide for the `distortionCoefficients`? – Colin747 Dec 01 '15 at 15:24
  • `(1, N)`, where `N` is 4, 5, or 8 – Miki Dec 01 '15 at 15:26
  • Thanks, if you like to put your comments as an answer I'd accept. – Colin747 Dec 01 '15 at 15:26

1 Answers1

13

You can see on OpenCV documentation for undistort that:

Camera matrix is a 3x3 matrix:

 fx   0  cx
  0  fy  cy
  0   0   1 

that you can create as:

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

distortionCoefficients is a vector or 4, 5, or 8 parameters:

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

that you can create as:

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

You can find the meaning of the parameters on OpenCV documentation

Miki
  • 40,887
  • 13
  • 123
  • 202