Ejemplo n.º 1
0
        public bool ToOpenCV(ref Mat sensorMat, ref MatOfDouble distCoeffsMat, int width, int height)
        {
            if (!ValidateAspect(width, height))
            {
                return(false);
            }

            if (sensorMat == null || sensorMat.IsDisposed || sensorMat.rows() != 3 || sensorMat.cols() != 3)
            {
                sensorMat = Mat.eye(3, 3, CvType.CV_64F);
            }
            if (distCoeffsMat == null || distCoeffsMat.IsDisposed || distCoeffsMat.total() != distortionCoeffs.Length)
            {
                distCoeffsMat = new MatOfDouble(new Mat(1, distortionCoeffs.Length, CvType.CV_64F));                     // This seems to be the only way to get distCoeffs.Length columns.
            }

            // Assuming the rest of the matrix is identity.
            sensorMat.WriteValue(fx * width, 0, 0);
            sensorMat.WriteValue(fy * height, 1, 1);
            sensorMat.WriteValue(cx * width, 0, 2);
            sensorMat.WriteValue(cy * height, 1, 2);

            for (int i = 0; i < distortionCoeffs.Length; i++)
            {
                distCoeffsMat.WriteValue(distortionCoeffs[i], i);
            }

            return(true);
        }
Ejemplo n.º 2
0
        public void UpdateFromOpenCV(Mat sensorMat, MatOfDouble distCoeffsMat, int width, int height, float rmsError)
        {
            if (distortionCoeffs == null || distCoeffsMat.IsDisposed || distortionCoeffs.Length != distCoeffsMat.total())
            {
                distortionCoeffs = new double[distCoeffsMat.total()];
            }

            fx            = sensorMat.ReadValue(0, 0) / (double)width;
            fy            = sensorMat.ReadValue(1, 1) / (double)height;
            cx            = sensorMat.ReadValue(0, 2) / (double)width;
            cy            = sensorMat.ReadValue(1, 2) / (double)height;
            aspect        = width / (double)height;
            this.rmsError = rmsError;

            for (int i = 0; i < distortionCoeffs.Length; i++)
            {
                distortionCoeffs[i] = distCoeffsMat.ReadValue(i);
            }
        }
        private void DrawFrame(Mat grayMat, Mat bgrMat)
        {
            Imgproc.cvtColor(grayMat, bgrMat, Imgproc.COLOR_GRAY2BGR);

            switch (markerType)
            {
            default:
            case MarkerType.ChArUcoBoard:
                // detect markers.
                Aruco.detectMarkers(grayMat, dictionary, corners, ids, detectorParams, rejectedCorners, camMatrix, distCoeffs);

                // refine marker detection.
                if (refineMarkerDetection)
                {
                    Aruco.refineDetectedMarkers(grayMat, charucoBoard, corners, ids, rejectedCorners, camMatrix, distCoeffs, 10f, 3f, true, recoveredIdxs, detectorParams);
                }

                // if at least one marker detected
                if (ids.total() > 0)
                {
                    Aruco.interpolateCornersCharuco(corners, ids, grayMat, charucoBoard, charucoCorners, charucoIds, camMatrix, distCoeffs, charucoMinMarkers);

                    // draw markers.
                    Aruco.drawDetectedMarkers(bgrMat, corners, ids, new Scalar(0, 255, 0, 255));
                    // if at least one charuco corner detected
                    if (charucoIds.total() > 0)
                    {
                        Aruco.drawDetectedCornersCharuco(bgrMat, charucoCorners, charucoIds, new Scalar(0, 0, 255, 255));
                    }
                }
                break;

            case MarkerType.ChessBoard:
            case MarkerType.CirclesGlid:
            case MarkerType.AsymmetricCirclesGlid:
                // detect markers.
                MatOfPoint2f points = new MatOfPoint2f();
                bool         found  = false;

                switch (markerType)
                {
                default:
                case MarkerType.ChessBoard:
                    found = Calib3d.findChessboardCorners(grayMat, new Size((int)squaresX, (int)squaresY), points, Calib3d.CALIB_CB_ADAPTIVE_THRESH | Calib3d.CALIB_CB_FAST_CHECK | Calib3d.CALIB_CB_NORMALIZE_IMAGE);
                    break;

                case MarkerType.CirclesGlid:
                    found = Calib3d.findCirclesGrid(grayMat, new Size((int)squaresX, (int)squaresY), points, Calib3d.CALIB_CB_SYMMETRIC_GRID);
                    break;

                case MarkerType.AsymmetricCirclesGlid:
                    found = Calib3d.findCirclesGrid(grayMat, new Size((int)squaresX, (int)squaresY), points, Calib3d.CALIB_CB_ASYMMETRIC_GRID);
                    break;
                }

                if (found)
                {
                    if (markerType == MarkerType.ChessBoard)
                    {
                        Imgproc.cornerSubPix(grayMat, points, new Size(5, 5), new Size(-1, -1), new TermCriteria(TermCriteria.EPS + TermCriteria.COUNT, 30, 0.1));
                    }

                    // draw markers.
                    Calib3d.drawChessboardCorners(bgrMat, new Size((int)squaresX, (int)squaresY), points, found);
                }
                break;
            }

            double[] camMatrixArr = new double[(int)camMatrix.total()];
            camMatrix.get(0, 0, camMatrixArr);
            double[] distCoeffsArr = new double[(int)distCoeffs.total()];
            distCoeffs.get(0, 0, distCoeffsArr);

            int    ff         = Imgproc.FONT_HERSHEY_SIMPLEX;
            double fs         = 0.4;
            Scalar c          = new Scalar(255, 255, 255, 255);
            int    t          = 0;
            int    lt         = Imgproc.LINE_AA;
            bool   blo        = false;
            int    frameCount = (markerType == MarkerType.ChArUcoBoard) ? allCorners.Count : imagePoints.Count;

            Imgproc.putText(bgrMat, frameCount + " FRAME CAPTURED", new Point(bgrMat.cols() - 300, 20), ff, fs, c, t, lt, blo);
            Imgproc.putText(bgrMat, "IMAGE_WIDTH: " + bgrMat.width(), new Point(bgrMat.cols() - 300, 40), ff, fs, c, t, lt, blo);
            Imgproc.putText(bgrMat, "IMAGE_HEIGHT: " + bgrMat.height(), new Point(bgrMat.cols() - 300, 60), ff, fs, c, t, lt, blo);
            Imgproc.putText(bgrMat, "CALIBRATION_FLAGS: " + calibrationFlags, new Point(bgrMat.cols() - 300, 80), ff, fs, c, t, lt, blo);

            Imgproc.putText(bgrMat, "CAMERA_MATRIX: ", new Point(bgrMat.cols() - 300, 100), ff, fs, c, t, lt, blo);
            for (int i = 0; i < camMatrixArr.Length; i = i + 3)
            {
                Imgproc.putText(bgrMat, "   " + camMatrixArr[i] + ", " + camMatrixArr[i + 1] + ", " + camMatrixArr[i + 2] + ",", new Point(bgrMat.cols() - 300, 120 + 20 * i / 3), ff, fs, c, t, lt, blo);
            }
            Imgproc.putText(bgrMat, "DISTORTION_COEFFICIENTS: ", new Point(bgrMat.cols() - 300, 180), ff, fs, c, t, lt, blo);
            for (int i = 0; i < distCoeffsArr.Length; ++i)
            {
                Imgproc.putText(bgrMat, "   " + distCoeffsArr[i] + ",", new Point(bgrMat.cols() - 300, 200 + 20 * i), ff, fs, c, t, lt, blo);
            }
            Imgproc.putText(bgrMat, "AVG_REPROJECTION_ERROR: " + repErr, new Point(bgrMat.cols() - 300, 300), ff, fs, c, t, lt, blo);

            if (frameCount == 0)
            {
                Imgproc.putText(bgrMat, "To calibration start, please press the calibration button or do air tap gesture!", new Point(5, bgrMat.rows() - 10), Imgproc.FONT_HERSHEY_SIMPLEX, 0.5, new Scalar(255, 255, 255, 255), 1, Imgproc.LINE_AA, false);
            }
        }