public void CalibrateCV(ChessBoard cb, out Matrix cameraMat, out Matrix distCoeffs) { var worldCoordinates = cb.boardLocalCoordinates_cv; List <List <Point3f> > worldpoints = new List <List <Point3f> >(); for (int i = 0; i < images.Count; i++) { worldpoints.Add(cb.boardLocalCoordinates_cv.ToList()); } double[,] cameraMat2 = new double[3, 3]; var imagepoints = images.Select(x => x.ImagePoints); Matrix cameramat = new Matrix(3, 3); distCoeffs = new Matrix(4, 1); Mat[] rvecs, tvecs; CVI.CalibrateCamera(worldpoints.Select(x => x.ToArray()).ToArray(), imagepoints.ToArray(), images.First().imageSize, cameramat, distCoeffs, CalibType.Default, new MCvTermCriteria(), out rvecs, out tvecs); cameraMat = cameramat; }
/// <summary> /// Estimates intrinsic camera parameters and extrinsic parameters for each of the views /// </summary> /// <param name="objectPoints">The 3D location of the object points. The first index is the index of image, second index is the index of the point</param> /// <param name="imagePoints">The 2D image location of the points. The first index is the index of the image, second index is the index of the point</param> /// <param name="imageSize">The size of the image, used only to initialize intrinsic camera matrix</param> /// <param name="intrinsicParam">The intrisinc parameters, might contains some initial values. The values will be modified by this function.</param> /// <param name="calibrationType">cCalibration type</param> /// <param name="termCriteria">The termination criteria</param> /// <param name="extrinsicParams">The output array of extrinsic parameters.</param> /// <returns>The final reprojection error</returns> public static double CalibrateCamera( MCvPoint3D32f[][] objectPoints, PointF[][] imagePoints, Size imageSize, IntrinsicCameraParameters intrinsicParam, CvEnum.CalibType calibrationType, MCvTermCriteria termCriteria, out ExtrinsicCameraParameters[] extrinsicParams) { Debug.Assert(objectPoints.Length == imagePoints.Length, "The number of images for objects points should be equal to the number of images for image points"); int imageCount = objectPoints.Length; using (VectorOfVectorOfPoint3D32F vvObjPts = new VectorOfVectorOfPoint3D32F(objectPoints)) using (VectorOfVectorOfPointF vvImgPts = new VectorOfVectorOfPointF(imagePoints)) { double reprojectionError = -1; using (VectorOfMat rotationVectors = new VectorOfMat()) using (VectorOfMat translationVectors = new VectorOfMat()) { Mat cameraMat = new Mat(); Mat distorCoeff = new Mat(); reprojectionError = CvInvoke.CalibrateCamera( vvObjPts, vvImgPts, imageSize, intrinsicParam.IntrinsicMatrix, intrinsicParam.DistortionCoeffs, rotationVectors, translationVectors, calibrationType, termCriteria); extrinsicParams = new ExtrinsicCameraParameters[imageCount]; for (int i = 0; i < imageCount; i++) { ExtrinsicCameraParameters p = new ExtrinsicCameraParameters(); using (Mat matR = rotationVectors[i]) matR.CopyTo(p.RotationVector); using (Mat matT = translationVectors[i]) matT.CopyTo(p.TranslationVector); extrinsicParams[i] = p; } } return(reprojectionError); } }
public void CalibrateCV(ChessBoard cb, out PinholeCamera[] cameras) { var worldCoordinates = cb.boardLocalCoordinates_cv; List <List <Point3f> > worldpoints = new List <List <Point3f> >(); for (int i = 0; i < images.Count; i++) { worldpoints.Add(cb.boardLocalCoordinates_cv.ToList()); } double[,] cameraMat2 = new double[3, 3]; var imagepoints = images.Select(x => x.ImagePoints); Matrix cameramat = new Matrix(3, 3); Matrix distcoeffs = new Matrix(4, 1); Mat[] rvecs, tvecs; CVI.CalibrateCamera(worldpoints.Select(x => x.ToArray()).ToArray(), imagepoints.ToArray(), images.First().imageSize, cameramat, distcoeffs, CalibType.Default, new MCvTermCriteria(), out rvecs, out tvecs); cameras = new PinholeCamera[images.Count]; for (int i = 0; i < rvecs.Length; i++) { var rvec = rvecs[i]; var tvec = tvecs[i]; cameras[i] = new PinholeCamera(); var cam = cameras[i]; var rot = new RotationVector3D(); rvec.CopyTo(rot); var worldMat = new Matrix4d(); } for (int i = 0; i < cameras.Length; i++) { worldpoints.Add(cb.boardLocalCoordinates_cv.ToList()); } }