Beispiel #1
0
    void Update()
    {
        // if space is down do AR tranformation
        if (notCalibrated && Input.GetKeyDown(KeyCode.Space))
        {
            // laod camera calibrations setting
            CameraCalibSerializable calidSaveData = Utilities.LoadCameraCalibrationParams();
            // initialise marhsallers
            MatDoubleMarshaller distCoeffs   = new MatDoubleMarshaller(calidSaveData.distortionCoefficients);
            MatDoubleMarshaller cameraMatrix = new MatDoubleMarshaller(calidSaveData.cameraMatrix);
            double reProjectionError         = calidSaveData.reProjectionError;
            // get calibration data
            calibData     = new UCameraCalibrationData(distCoeffs, cameraMatrix, reProjectionError);
            notCalibrated = false;
        }
        else if (!notCalibrated)
        {
            // estimate charuco board pose
            (
                UDetectMarkersData markerData,
                UBoardMarkerPoseEstimationDataEuler poseEstimationData
            ) = Aruco.UEstimateCharucoBoardPose(
                _webCamTexture.GetPixels32(),
                _webCamTexture.width,
                _webCamTexture.height,
                boardParameters,
                calibData.cameraMatrix.NativeDataPointer,
                calibData.distCoeffs.NativeDataPointer
                );

            TransformGameObjects(poseEstimationData, markerData);
        }
    }
Beispiel #2
0
        // find calibrates camera with collected data found by UFindCharucoBoardCorners method
        unsafe public static UCameraCalibrationData UCalibrateCameraCharuco(int width, int height, CharucoBoardParameters boardParameters, DoubleVectorIntMarshaller allCharucoIds, DoubleVectorPoint2FMarshaller allCharucoCorners)
        {
            MatDoubleMarshaller distCoeffs   = new MatDoubleMarshaller();
            MatDoubleMarshaller cameraMatrix = new MatDoubleMarshaller();

            double reProjectionError = CalibrateCameraCharuco(
                height,
                width,
                boardParameters.squaresW,
                boardParameters.squaresH,
                boardParameters.squareLength,
                boardParameters.markerLength,
                allCharucoIds.NativeDataPointer,
                allCharucoCorners.NativeDataPointer,
                cameraMatrix.NativeDataPointer,
                distCoeffs.NativeDataPointer
                );


            UCameraCalibrationData calibrationData = new UCameraCalibrationData(
                distCoeffs,
                cameraMatrix,
                reProjectionError
                );

            return(calibrationData);
        }
Beispiel #3
0
        // public static UCameraCalibrationData UStaticCalibrateCameraData()
        // {

        //     MatDoubleMarshaller distCoeffs = new MatDoubleMarshaller();
        //     MatDoubleMarshaller cameraMatrix = new MatDoubleMarshaller();

        //     StaticCameraCalibData(
        //         cameraMatrix.NativeDataPointer,
        //         distCoeffs.NativeDataPointer,
        //         true
        //     );

        //     UCameraCalibrationData calibrationData = new UCameraCalibrationData(
        //         distCoeffs,
        //         cameraMatrix
        //     );

        //     return calibrationData;
        // }

        // unsafe public static UCameraCalibrationData UCalibrateCamera(Color32[] texture, int width, int height, DoubleVectorIntMarshaller allCharucoIds, DoubleVectorPoint2FMarshaller allCharucoCorners)
        // {

        //     var watch = System.Diagnostics.Stopwatch.StartNew();

        //     // IntPtr distCoeffs = OpenCVMarshal.CreateMatPointer();
        //     MatDoubleMarshaller distCoeffs = new MatDoubleMarshaller();
        //     // IntPtr cameraMatrix = OpenCVMarshal.CreateMatPointer();
        //     MatDoubleMarshaller cameraMatrix = new MatDoubleMarshaller();

        //     fixed (Color32* texP = texture)
        //     {
        //         CalibrateCamera(
        //             texP,
        //             width,
        //             height,
        //             allCharucoIds.NativeDataPointer,
        //             allCharucoCorners.NativeDataPointer,
        //             cameraMatrix.NativeDataPointer,
        //             distCoeffs.NativeDataPointer
        //         );
        //     }


        //     UCameraCalibrationData calibrationData = new UCameraCalibrationData(
        //         distCoeffs,
        //         cameraMatrix
        //     );

        //     watch.Stop();
        //     var elapsedMs = watch.ElapsedMilliseconds;

        //     return calibrationData;
        // }
        unsafe public static UCameraCalibrationData UCalibrateCameraCharuco(int width, int height, CharucoBoardParameters boardParameters, DoubleVectorIntMarshaller allCharucoIds, DoubleVectorPoint2FMarshaller allCharucoCorners)
        {
            var watch = System.Diagnostics.Stopwatch.StartNew();
            // IntPtr distCoeffs = OpenCVMarshal.CreateMatPointer();
            MatDoubleMarshaller distCoeffs = new MatDoubleMarshaller();
            // IntPtr cameraMatrix = OpenCVMarshal.CreateMatPointer();
            MatDoubleMarshaller cameraMatrix = new MatDoubleMarshaller();

            double reProjectionError = CalibrateCameraCharuco(
                height,
                width,
                boardParameters.squaresW,
                boardParameters.squaresH,
                boardParameters.squareLength,
                boardParameters.markerLength,
                allCharucoIds.NativeDataPointer,
                allCharucoCorners.NativeDataPointer,
                cameraMatrix.NativeDataPointer,
                distCoeffs.NativeDataPointer
                );


            UCameraCalibrationData calibrationData = new UCameraCalibrationData(
                distCoeffs,
                cameraMatrix,
                reProjectionError
                );

            watch.Stop();
            var elapsedMs = watch.ElapsedMilliseconds;

            return(calibrationData);
        }
        public unsafe static (UDetectMarkersData, UBoardMarkerPoseEstimationDataEuler) UEstimateCharucoBoardPose(Color32[] texture, int width, int height, CharucoBoardParameters boardParameters, IntPtr cameraMatrix, IntPtr distCoeffs)
        {
            int   cornersH     = 3;
            int   cornersW     = 4;
            float markerLength = 0.045f;
            float squareLength = 0.06f;
            // marshallers
            Vec3dMarshaller     tvecMarshaller = new Vec3dMarshaller();
            Vec3dMarshaller     rvecMarshaller = new Vec3dMarshaller();
            MatDoubleMarshaller eulerAngles    = new MatDoubleMarshaller();
            // marker pointers
            VectorIntMarshaller           markerIds = new VectorIntMarshaller();
            DoubleVectorPoint2FMarshaller markerCornerMarshaller      = new DoubleVectorPoint2FMarshaller();
            DoubleVectorPoint2FMarshaller rejectedCandidateMarshaller = new DoubleVectorPoint2FMarshaller();

            estimateCharucoBoardPosePerfMarker.Begin();
            fixed(Color32 *texP = texture)
            {
                EstimateCharucoBoardPose(
                    texP,
                    width,
                    height,
                    // markerLength,
                    // squareLength,
                    // cornersW,
                    // cornersH,
                    boardParameters.markerLength,
                    boardParameters.squareLength,
                    boardParameters.squaresW,
                    boardParameters.squaresH,
                    markerIds.NativeDataPointer,
                    markerCornerMarshaller.NativeDataPointer,
                    rejectedCandidateMarshaller.NativeDataPointer,
                    cameraMatrix,
                    distCoeffs,
                    rvecMarshaller.NativeDataPointer,
                    tvecMarshaller.NativeDataPointer,
                    eulerAngles.NativeDataPointer
                    );
            }

            estimateCharucoBoardPosePerfMarker.End();

            UDetectMarkersData markerData = new UDetectMarkersData(
                (int[])markerIds.GetMangedObject(),
                (List <List <Vector2> >)markerCornerMarshaller.GetMangedObject(),
                (List <List <Vector2> >)rejectedCandidateMarshaller.GetMangedObject(),
                markerIds.NativeDataPointer,
                markerCornerMarshaller.NativeDataPointer,
                rejectedCandidateMarshaller.NativeDataPointer
                );

            UBoardMarkerPoseEstimationDataEuler PoseEstimateData = new UBoardMarkerPoseEstimationDataEuler(
                (Vec3d)rvecMarshaller.GetMangedObject(),
                (Vec3d)tvecMarshaller.GetMangedObject(),
                (double[][])eulerAngles.GetMangedObject()
                );

            return(markerData : markerData, PoseEstimateData : PoseEstimateData);
        }
        public unsafe static UBoardMarkerPoseEstimationDataEuler UEstimateArucoBoardPose(Color32[] texture, int width, int height, ArucoBoardParameters boardParameters, IntPtr cameraMatrix, IntPtr distCoeffs)
        {
            float markerLength     = 0.04f;
            float markerSeparation = 0.01f;
            int   markersX         = 4;
            int   markersY         = 3;

            Vec3dMarshaller     tvecMarshaller = new Vec3dMarshaller();
            Vec3dMarshaller     rvecMarshaller = new Vec3dMarshaller();
            MatDoubleMarshaller eulerAngles    = new MatDoubleMarshaller();


            fixed(Color32 *texP = texture)
            {
                EstimateArucoBoardPoseV2(
                    texP,
                    width,
                    height,
                    boardParameters.markerLength,
                    boardParameters.markerSeperation,
                    boardParameters.squaresW,
                    boardParameters.squaresH,
                    cameraMatrix,
                    distCoeffs,
                    rvecMarshaller.NativeDataPointer,
                    tvecMarshaller.NativeDataPointer,
                    eulerAngles.NativeDataPointer
                    );
            }

            UBoardMarkerPoseEstimationDataEuler PoseEstimateData = new UBoardMarkerPoseEstimationDataEuler(
                (Vec3d)rvecMarshaller.GetMangedObject(),
                (Vec3d)tvecMarshaller.GetMangedObject(),
                (double[][])eulerAngles.GetMangedObject()
                );

            return(PoseEstimateData);
        }
Beispiel #6
0
 public UCameraCalibrationData(MatDoubleMarshaller distCoeffs, MatDoubleMarshaller cameraMatrix, double reProjectionError)
 {
     this.distCoeffs        = distCoeffs;
     this.cameraMatrix      = cameraMatrix;
     this.reProjectionError = reProjectionError;
 }