public bool calibrate(float squareEdge, Size patternSize, string[] images) { VectorOfVectorOfPointF corners = findCorners(squareEdge, patternSize, images); if (corners.Size == 0) { Console.WriteLine("Cannot find chessboard!"); return(false); } VectorOfPoint3D32F chessboard = getChessboardCorners(squareEdge, patternSize); VectorOfVectorOfPoint3D32F objectPoints = new VectorOfVectorOfPoint3D32F(); for (int i = corners.Size; i > 0; i--) { objectPoints.Push(chessboard); } CameraParam param = new CameraParam(); // set mats Mat rotationMat = new Mat(); Mat translationMat = new Mat(); Image <Gray, Byte> image = new Image <Gray, Byte>(images[0]); imgSize = image.Size; CvInvoke.CalibrateCamera( objectPoints, corners, image.Size, param.cameraMatrix.Mat, param.distortionCoeffs.Mat, rotationMat, translationMat, CalibType.Default, new MCvTermCriteria(30, 0.1)); cameraParam.Clear(); cameraParam.Add(param); return(_isCalibrated = true); }
public bool calibrate(float squareEdge, Size patternSize, string[] imagesLeft, string[] imagesRight) { List <VectorOfVectorOfPointF> listCorners = findCorners(squareEdge, patternSize, imagesLeft, imagesRight); if (listCorners.Last().Size == 0) { Console.WriteLine("Cannot find chessboard!"); return(false); } VectorOfPoint3D32F chessboard = getChessboardCorners(squareEdge, patternSize); VectorOfVectorOfPoint3D32F objectPoints = new VectorOfVectorOfPoint3D32F(); for (int i = listCorners.Last().Size; i > 0; i--) { objectPoints.Push(chessboard); } Image <Gray, Byte> image = new Image <Gray, Byte>(imagesLeft[0]); CameraParam camLeft = new CameraParam(); CameraParam camRight = new CameraParam(); CvInvoke.StereoCalibrate( objectPoints, listCorners[0], listCorners[1], camLeft.cameraMatrix.Mat, camLeft.distortionCoeffs.Mat, camRight.cameraMatrix.Mat, camRight.distortionCoeffs.Mat, image.Size, R, T, E, F, CalibType.Default, new MCvTermCriteria(30, 0.1e5)); Rectangle roi1 = Rectangle.Empty, roi2 = Rectangle.Empty; CvInvoke.StereoRectify( camLeft.cameraMatrix.Mat, camLeft.distortionCoeffs.Mat, camRight.cameraMatrix.Mat, camRight.distortionCoeffs.Mat, image.Size, R, T, camLeft.rotationMatrix.Mat, camRight.rotationMatrix.Mat, camLeft.translationMatrix.Mat, camRight.translationMatrix.Mat, disparityMatrix, StereoRectifyType.Default, -1, Size.Empty, ref roi1, ref roi2); cameraParam.Clear(); cameraParam.Add(camLeft); cameraParam.Add(camRight); rectTransforms = getRectifyTransforms(); Matrix <double> p = new Matrix <double>(new double[, ] { { image.Size.Width / 2 }, { image.Size.Height / 2 }, { 1 } }); Matrix <double> px = rectTransforms[0].Mul(p); double[,] dL = p.Sub(px.Mul(1 / px[2, 0])).Data; px = rectTransforms[1].Mul(p); double[,] dR = p.Sub(px.Mul(1 / px[2, 0])).Data; rectTransforms = getRectifyTransforms(dL, dR); rectMask[0] = getRectMask(image.Size, rectTransforms[0]); rectMask[1] = getRectMask(image.Size, rectTransforms[1]); _isStereo = true; return(_isCalibrated = true); }