// Use this for initialization void Start() { Calibration.CameraCalibrationResult result = Calibration.ComputeCameraCalibration( xyz, uv, new System.Drawing.Size(_projector.pixelWidth, _projector.pixelHeight), new Emgu.CV.Matrix <double>(3, 3), out status); result.extrinsics.ApplyToTransform(_projector.transform); _projector.projectionMatrix = result.intrinsics.ProjectionMatrix(_projector.nearClipPlane, _projector.farClipPlane); }
public Calibration.CameraCalibrationResult ComputeCalibration(int pixelWidth, int pixelHeight, float near, float far) { string status; Vector2[] uvPoints = subsampling ? subsampledUV.ToArray() : uv.ToArray(); Vector3[] xyzPoints = subsampling ? subsampledXYZ.ToArray() : xyz.ToArray(); if (subsampling) { SubSample(); uvPoints = subsampledUV.ToArray(); xyzPoints = subsampledXYZ.ToArray(); } else { uvPoints = uv.ToArray(); xyzPoints = xyz.ToArray(); } //Compute calibration Debug.Log("Calibration requiested: #uv:" + uvPoints.Length + "#xyz:" + xyzPoints.Length); Emgu.CV.CvEnum.CalibType flags = Emgu.CV.CvEnum.CalibType.UseIntrinsicGuess; // uses the intrinsicMatrix as initial estimation, or generates an initial estimation using imageSize //flags |= CalibType.FixFocalLength; // if (CV_CALIB_USE_INTRINSIC_GUESS) then: {fx,fy} are constant // flags |= CalibType.FixAspectRatio; // if (CV_CALIB_USE_INTRINSIC_GUESS) then: fy is a free variable, fx/fy stays constant //flags |= CalibType.FixPrincipalPoint; // if (CV_CALIB_USE_INTRINSIC_GUESS) then: {cx,cy} are constant /*flags |= (CalibType.FixK1 // Given CalibType.FixK{i}: if (CV_CALIB_USE_INTRINSIC_GUESS) then: K{i} = distortionCoefficents[i], else:k ki = 0 | CalibType.FixK2 | CalibType.FixK3 | CalibType.FixK4 | CalibType.FixK5 | CalibType.FixK6); | // flags |= CalibType.FixIntrinsic; | flags |= CalibType.ZeroTangentDist; // tangential distortion is zero: {P1,P2} = {0,0} */ result = Calibration.ComputeCameraCalibration(xyzPoints, uvPoints, new System.Drawing.Size(pixelWidth, pixelHeight), new Emgu.CV.Matrix <double>(3, 3), out status, true, true, flags); Debug.Log(status); Debug.Log("distortion:" + result.distortion.ToString()); error = result.Error; PerspectiveMatrixAfter = result.intrinsics.ProjectionMatrix(near, far); if (targetCamera != null && PerspectiveMatrixAfter != Matrix4x4.identity) { PerspectiveMatrixBefore = targetCamera.projectionMatrix; result.extrinsics.ApplyToTransform(targetCamera.transform); targetCamera.projectionMatrix = PerspectiveMatrixAfter; } return(result); }