title: Use MATLAB cameraParams in OpenCV program
body: <p>I have a MATLAB program that loads two images and returns two camera matrices and a cameraParams object with distortion coefficients, etc. I would now like to use this exact configuration to undistort points and so on, in an OpenCV program that triangulates points given their 2D locations in two different videos.</p>
  
  <pre><code>function [cameraMatrix1, cameraMatrix2, cameraParams] = setupCameraCalibration(leftImageFile, rightImageFile, squareSize)
  % Auto-generated by cameraCalibrator app on 20-Feb-2015
  </code></pre>
  
  <p>The thing is, the output of undistortPoints is different in MATLAB and OpenCV even though both use the same arguments.</p>
  
  <p>As an example:</p>
  
  <pre><code>&gt;&gt; undistortPoints([485, 502], defaultCameraParams)
  ans = 485   502
  </code></pre>
  
  <p>In Java, the following test mimics the above (it passes).</p>
  
  <pre><code>public void testUnDistortPoints() {
      Mat srcMat = new Mat(2, 1, CvType.CV_32FC2);
      Mat dstMat = new Mat(2, 1, CvType.CV_32FC2);
      srcMat.put(0, 0, new float[] { 485, 502 } );
  
      MatOfPoint2f src = new MatOfPoint2f(srcMat);
      MatOfPoint2f dst = new MatOfPoint2f(dstMat);
  
      Mat defaultCameraMatrix = Mat.eye(3, 3, CvType.CV_32F);
      Mat defaultDistCoefficientMatrix = new Mat(1, 4, CvType.CV_32F);
  
      Imgproc.undistortPoints(
              src,
              dst,
              defaultCameraMatrix,
              defaultDistCoefficientMatrix
      );
  
      System.out.println(dst.dump());
  
      assertEquals(dst.get(0, 0)[0], 485d);
      assertEquals(dst.get(0, 0)[1], 502d);
  }
  </code></pre>
  
  <p>However, say I change the first distortion coefficient (k1). In MATLAB:</p>
  
  <pre><code>changedDist = cameraParameters('RadialDistortion', [2 0 0])
  &gt;&gt; undistortPoints([485, 502], changedDist)
  ans = 4.8756    5.0465
  </code></pre>
  
  <p>In Java:</p>
  
  <pre><code>public void testUnDistortPointsChangedDistortion() {
      Mat srcMat = new Mat(2, 1, CvType.CV_32FC2);
      Mat dstMat = new Mat(2, 1, CvType.CV_32FC2);
      srcMat.put(0, 0, new float[] { 485, 502 } );
  
      MatOfPoint2f src = new MatOfPoint2f(srcMat);
      MatOfPoint2f dst = new MatOfPoint2f(dstMat);
  
      Mat defaultCameraMatrix = Mat.eye(3, 3, CvType.CV_32F);
      Mat distCoefficientMatrix = new Mat(1, 4, CvType.CV_32F);
      distCoefficientMatrix.put(0, 0, 2f); // updated
  
      Imgproc.undistortPoints(
              src,
              dst,
              defaultCameraMatrix,
              distCoefficientMatrix
      );
  
      System.out.println(dst.dump());
  
      assertEquals(4.8756, dst.get(0, 0)[0]);
      assertEquals(5.0465, dst.get(0, 0)[1]);
  }
  </code></pre>
  
  <p>It fails with the following output:</p>
  
  <pre><code>[0.0004977131, 0.0005151587]
  
  junit.framework.AssertionFailedError: 
  Expected :4.8756
  Actual   :4.977131029590964E-4
  </code></pre>
  
  <p>Why are the results different? I thought Java's distortion coefficient matrix includes both the radial and tangential distortion coefficients.</p>
  
  <p>Also, is CV_64FC1 a good choice of type for the camera / distortion coefficient matrices?</p>
  
  <p>I was trying to test the effect of changing the camera matrix itself (i.e. the value of f_x), but it's not possible to set the 'IntrinsicMatrix' parameter when using cameraparams, so I want to solve the distortion matrix problem first.</p>
  
  <p>Any help would be greatly appreciated.</p>