public static void Calibrate(string[] imgFiles, out LensParams lensParams) { Size patternSize = new Size(CHESS_PATTERN_WIDTH, CHESS_PATTERN_HEIGHT); VectorOfVectorOfPoint3D32F objPoints = new VectorOfVectorOfPoint3D32F(); VectorOfVectorOfPointF imagePoints = new VectorOfVectorOfPointF(); Size imageSize = Size.Empty; foreach (string file in imgFiles) { Mat img = CvInvoke.Imread(file, ImreadModes.Grayscale); if (imageSize == Size.Empty) { imageSize = new Size(img.Width, img.Height); } //CvInvoke.Imshow("input", img); VectorOfPointF corners = new VectorOfPointF(patternSize.Width * patternSize.Height); bool find = CvInvoke.FindChessboardCorners(img, patternSize, corners); if (find) { MCvPoint3D32f[] points = new MCvPoint3D32f[patternSize.Width * patternSize.Height]; int loopIndex = 0; for (int i = 0; i < patternSize.Height; i++) { for (int j = 0; j < patternSize.Width; j++) { points[loopIndex++] = new MCvPoint3D32f(j, i, 0); } } objPoints.Push(new VectorOfPoint3D32F(points)); imagePoints.Push(corners); } } Matrix <double> K = new Matrix <double>(3, 3); Matrix <double> D = new Matrix <double>(4, 1); Mat rotation = new Mat(); Mat translation = new Mat(); Fisheye.Calibrate(objPoints, imagePoints, imageSize, K, D, rotation, translation, Fisheye.CalibrationFlag.CheckCond, new MCvTermCriteria(30, 0.1) ); lensParams = new LensParams(K, D); }
public static (Mat cameraMatrix, Mat distCoeffs, double rms) CalibrateCharuco(int squaresX, int squaresY, float squareLength, float markerLength, PredefinedDictionaryName dictionary, Size imageSize, VectorOfInt charucoIds, VectorOfPointF charucoCorners, VectorOfInt markerCounterPerFrame, bool fisheye, Func <byte[], byte[]> GetRemoteChessboardCorner) { Mat cameraMatrix = new Mat(3, 3, Emgu.CV.CvEnum.DepthType.Cv64F, 1); Mat distCoeffs = new Mat(1, 4, Emgu.CV.CvEnum.DepthType.Cv64F, 1); double rms = 0.0; VectorOfVectorOfPoint3D32F processedObjectPoints = new VectorOfVectorOfPoint3D32F(); VectorOfVectorOfPointF processedImagePoints = new VectorOfVectorOfPointF(); int k = 0; for (int i = 0; i < markerCounterPerFrame.Size; i++) { int nMarkersInThisFrame = markerCounterPerFrame[i]; VectorOfPointF currentImgPoints = new VectorOfPointF(); VectorOfPoint3D32F currentObjPoints = new VectorOfPoint3D32F(); for (int j = 0; j < nMarkersInThisFrame; j++) { currentImgPoints.Push(new PointF[] { charucoCorners[k] }); currentObjPoints.Push(new MCvPoint3D32f[] { GetChessboardCorner(squaresX, squaresY, squareLength, markerLength, charucoIds[k], dictionary, GetRemoteChessboardCorner) }); k++; } processedImagePoints.Push(currentImgPoints); processedObjectPoints.Push(currentObjPoints); } VectorOfPoint3D32F rvecs = new VectorOfPoint3D32F(); VectorOfPoint3D32F tvecs = new VectorOfPoint3D32F(); if (fisheye) { Fisheye.Calibrate(processedObjectPoints, processedImagePoints, imageSize, cameraMatrix, distCoeffs, rvecs, tvecs, Fisheye.CalibrationFlag.FixSkew | Fisheye.CalibrationFlag.RecomputeExtrinsic, new MCvTermCriteria(400, double.Epsilon)); } else { CvInvoke.CalibrateCamera(processedObjectPoints, processedImagePoints, imageSize, cameraMatrix, distCoeffs, new Mat(), new Mat(), CalibType.FixK3, new MCvTermCriteria(30, 1e-4)); } rms = Validate(processedObjectPoints, processedImagePoints, cameraMatrix, distCoeffs, rvecs, tvecs, fisheye); return(cameraMatrix, distCoeffs, rms); }