Exemplo n.º 1
0
        public override void Dispose()
        {
            if (objectPoints68 != null)
            {
                objectPoints68.Dispose();
            }

            if (camMatrix != null)
            {
                camMatrix.Dispose();
            }
            if (distCoeffs != null)
            {
                distCoeffs.Dispose();
            }

            if (imagePoints != null)
            {
                imagePoints.Dispose();
            }

            if (rvec != null)
            {
                rvec.Dispose();
            }

            if (tvec != null)
            {
                tvec.Dispose();
            }
        }
 public void Dispose()
 {
     objectPoints.Dispose();
     imagePoints.Dispose();
     rvec.Dispose();
     tvec.Dispose();
     rotM.Dispose();
     camMatrix.Dispose();
     distCoeffs.Dispose();
 }
Exemplo n.º 3
0
 void OnDestroy()
 {
     if (_anchorPointsImageMat != null)
     {
         _anchorPointsImageMat.Dispose();
     }
     if (_anchorPointsWorldMat != null)
     {
         _anchorPointsWorldMat.Dispose();
     }
     if (_distCoeffs != null)
     {
         _distCoeffs.Dispose();
     }
     if (_cameraMatrix != null)
     {
         _cameraMatrix.Dispose();
     }
     if (_rVec != null)
     {
         _rVec.Dispose();
     }
     if (_rVec != null)
     {
         _rVec.Dispose();
     }
     if (_camTexMat != null)
     {
         _camTexMat.Dispose();
     }
     if (_camTexGrayMat != null)
     {
         _camTexGrayMat.Dispose();
     }
     if (_camTexGrayUndistortMat != null)
     {
         _camTexGrayUndistortMat.Dispose();
     }
 }
Exemplo n.º 4
0
        public void Dispose()
        {
            if (objectPoints != null && !objectPoints.IsDisposed)
            {
                objectPoints.Dispose();
            }

            if (imagePoints != null && !imagePoints.IsDisposed)
            {
                imagePoints.Dispose();
            }

            if (rvec != null && !rvec.IsDisposed)
            {
                rvec.Dispose();
            }

            if (tvec != null && !tvec.IsDisposed)
            {
                tvec.Dispose();
            }

            if (rotM != null && !rotM.IsDisposed)
            {
                rotM.Dispose();
            }

            if (camMatrix != null && !camMatrix.IsDisposed)
            {
                camMatrix.Dispose();
            }

            if (distCoeffs != null && !distCoeffs.IsDisposed)
            {
                distCoeffs.Dispose();
            }
        }
Exemplo n.º 5
0
        void OnDestroy()
        {
            if (_camTexMat != null)
            {
                _camTexMat.Dispose();
            }
            if (_tempTransferTexture)
            {
                Destroy(_tempTransferTexture);
            }
            if (_chessCornersImageMat != null)
            {
                _chessCornersImageMat.Dispose();
            }
            if (_previewMaterial)
            {
                Destroy(_previewMaterial);
            }
            if (_arTexture)
            {
                _arTexture.Release();
            }
            if (_chessPatternTexture)
            {
                _chessPatternTexture.Release();
            }
            if (_patternRenderMaterial)
            {
                Destroy(_patternRenderMaterial);
            }
            if (_chessCornersWorldMat != null)
            {
                _chessCornersWorldMat.Dispose();
            }

            Reset();
        }
        private double CaptureFrame(Mat frameMat)
        {
            double repErr = -1;

            switch (markerType)
            {
            default:
            case MarkerType.ChArUcoBoard:
                List <Mat> corners = new List <Mat>();
                Mat        ids     = new Mat();
                Aruco.detectMarkers(frameMat, dictionary, corners, ids, detectorParams, rejectedCorners, camMatrix, distCoeffs);

                if (refineMarkerDetection)
                {
                    Aruco.refineDetectedMarkers(frameMat, charucoBoard, corners, ids, rejectedCorners, camMatrix, distCoeffs, 10f, 3f, true, recoveredIdxs, detectorParams);
                }

                if (ids.total() > 0)
                {
                    Debug.Log("Frame captured.");

                    allCorners.Add(corners);
                    allIds.Add(ids);
                    allImgs.Add(frameMat);
                }
                else
                {
                    Debug.Log("Invalid frame.");

                    frameMat.Dispose();
                    if (ids != null)
                    {
                        ids.Dispose();
                    }
                    foreach (var item in corners)
                    {
                        item.Dispose();
                    }
                    corners.Clear();

                    return(-1);
                }

                // calibrate camera using aruco markers
                //double arucoRepErr = CalibrateCameraAruco (allCorners, allIds, charucoBoard, frameMat.size(), camMatrix, distCoeffs, rvecs, tvecs, calibrationFlags);
                //Debug.Log ("arucoRepErr: " + arucoRepErr);

                // calibrate camera using charuco
                repErr = CalibrateCameraCharuco(allCorners, allIds, charucoBoard, frameMat.size(), camMatrix, distCoeffs, rvecs, tvecs, calibrationFlags, calibrationFlags);

                break;

            case MarkerType.ChessBoard:
            case MarkerType.CirclesGlid:
            case MarkerType.AsymmetricCirclesGlid:

                MatOfPoint2f points      = new MatOfPoint2f();
                Size         patternSize = new Size((int)squaresX, (int)squaresY);

                bool found = false;
                switch (markerType)
                {
                default:
                case MarkerType.ChessBoard:
                    found = Calib3d.findChessboardCorners(frameMat, patternSize, points, Calib3d.CALIB_CB_ADAPTIVE_THRESH | Calib3d.CALIB_CB_FAST_CHECK | Calib3d.CALIB_CB_NORMALIZE_IMAGE);
                    break;

                case MarkerType.CirclesGlid:
                    found = Calib3d.findCirclesGrid(frameMat, patternSize, points, Calib3d.CALIB_CB_SYMMETRIC_GRID);
                    break;

                case MarkerType.AsymmetricCirclesGlid:
                    found = Calib3d.findCirclesGrid(frameMat, patternSize, points, Calib3d.CALIB_CB_ASYMMETRIC_GRID);
                    break;
                }

                if (found)
                {
                    Debug.Log("Frame captured.");
                    if (markerType == MarkerType.ChessBoard)
                    {
                        Imgproc.cornerSubPix(frameMat, points, new Size(5, 5), new Size(-1, -1), new TermCriteria(TermCriteria.EPS + TermCriteria.COUNT, 30, 0.1));
                    }

                    imagePoints.Add(points);
                    allImgs.Add(frameMat);
                }
                else
                {
                    Debug.Log("Invalid frame.");
                    frameMat.Dispose();
                    if (points != null)
                    {
                        points.Dispose();
                    }
                    return(-1);
                }

                if (imagePoints.Count < 1)
                {
                    Debug.Log("Not enough points for calibration.");
                    repErr = -1;
                }
                else
                {
                    MatOfPoint3f objectPoint = new MatOfPoint3f(new Mat(imagePoints[0].rows(), 1, CvType.CV_32FC3));
                    CalcChessboardCorners(patternSize, squareSize, objectPoint, markerType);

                    List <Mat> objectPoints = new List <Mat>();
                    for (int i = 0; i < imagePoints.Count; ++i)
                    {
                        objectPoints.Add(objectPoint);
                    }

                    repErr = Calib3d.calibrateCamera(objectPoints, imagePoints, frameMat.size(), camMatrix, distCoeffs, rvecs, tvecs, calibrationFlags);
                    objectPoint.Dispose();
                }

                break;
            }

            Debug.Log("repErr: " + repErr);
            Debug.Log("camMatrix: " + camMatrix.dump());
            Debug.Log("distCoeffs: " + distCoeffs.dump());

            return(repErr);
        }
Exemplo n.º 7
0
    public virtual double Calibrate(Mat mat)
    {
        var repError    = -1.0;
        var points      = new MatOfPoint2f();
        var patternSize = new Size((int)SizeX, (int)SizeY);

        var found = false;

        switch (boardType)
        {
        case BoardType.ChessBoard:
            found = Calib3d.findChessboardCorners(mat, patternSize, points, Calib3d.CALIB_CB_ADAPTIVE_THRESH | Calib3d.CALIB_CB_FAST_CHECK | Calib3d.CALIB_CB_NORMALIZE_IMAGE);

            break;

        case BoardType.CirclesGrid:
            found = Calib3d.findCirclesGrid(mat, patternSize, points, Calib3d.CALIB_CB_SYMMETRIC_GRID);
            break;

        case BoardType.AsymmetricCirclesGrid:
            found = Calib3d.findCirclesGrid(mat, patternSize, points, Calib3d.CALIB_CB_ASYMMETRIC_GRID);
            break;
        }


        if (found)
        {
            if (boardType == BoardType.ChessBoard)
            {
                Imgproc.cornerSubPix(mat, points, new Size(5, 5), new Size(-1, -1), new TermCriteria(TermCriteria.EPS + TermCriteria.COUNT, 30, 0.1));
            }
            imagePoints.Add(points);
        }
        else
        {
            Debug.Log("Invalid frame.");
            if (points != null)
            {
                points.Dispose();
            }
            ErrorEvent?.Invoke("Invalid frame.");
            return(repError);
        }

        if (imagePoints.Count < 1)
        {
            Debug.Log("Not enough points for calibration.");
            ErrorEvent?.Invoke("Not enough points for calibration.");
            return(repError);
        }
        else
        {
            var objectPoint = new MatOfPoint3f(new Mat(imagePoints[0].rows(), 1, CvType.CV_32FC3));
            CalcCorners(patternSize, squareSize, objectPoint);

            var objectPoints = new List <Mat>();
            for (int i = 0; i < imagePoints.Count; ++i)
            {
                objectPoints.Add(objectPoint);
            }
            repError = Calib3d.calibrateCamera(objectPoints, imagePoints, mat.size(), cameraMatrix, distCoeffs, rvecs, tvecs);
            CalibrationEvent?.Invoke(repError);
            objectPoint.Dispose();
        }

        Debug.Log("-----calibrate-----");
        Debug.Log("repErr: " + repError);
        Debug.Log("camMatrix: " + cameraMatrix.dump());
        Debug.Log("distCoeffs: " + distCoeffs.dump());
        return(repError);
    }