Ejemplo n.º 1
0
    protected override void CheckIfDetectedMarkers()
    {
        base.CheckIfDetectedMarkers();

        for (int i = 0; i < ids.Length; i++)
        {
            Cv2.CornerSubPix(grayedImg, corners[i], new Size(5, 5), new Size(-1, -1), TermCriteria.Both(30, 0.1));

            if (!MarkerManager.IsMarkerRegistered(ids[i]))
            {
                continue;
            }

            MarkerBehaviour m = MarkerManager.GetMarker(ids[i]);

            if (!allDetectedMarkers.ContainsKey(ids[i]))
            {
                m.OnMarkerDetected.Invoke();
                allDetectedMarkers.Add(m.GetMarkerID(), m);
            }

            // m.UpdateMarker(img.Cols, img.Rows, corners[i], rejectedImgPoints[i]);
            m.UpdateMarker(corners[i], calibrationData.GetCameraMatrix(),
                           calibrationData.GetDistortionCoefficients(), grayedImg);
        }
    }
Ejemplo n.º 2
0
    protected override void CheckIfDetectedMarkers()
    {
        base.CheckIfDetectedMarkers();

        for (int i = 0; i < ids.Length; i++)
        {
            Cv2.CornerSubPix(grayedImg, corners[i], new Size(5, 5), new Size(-1, -1), TermCriteria.Both(30, 0.1));

            if (!MarkerManager.IsMarkerRegistered(ids[i]))
            {
                continue;
            }

            MarkerBehaviour m = MarkerManager.GetMarker(ids[i]);

            if (!allDetectedMarkers.ContainsKey(ids[i]))
            {
                Debug.Log("FOUND MARKER: " + m.GetMarkerID());
                m.OnMarkerDetected.Invoke();
                allDetectedMarkers.Add(m.GetMarkerID(), m);
            }

            float rotZ = 0;

            switch (Screen.orientation)
            {
            case ScreenOrientation.Portrait:
                rotZ = 90;
                break;

            case ScreenOrientation.LandscapeLeft:
                rotZ = 180;
                break;

            case ScreenOrientation.LandscapeRight:
                rotZ = 0;
                break;

            case ScreenOrientation.PortraitUpsideDown:
                rotZ = -90;
                break;
            }

            if (!UseCustomCalibration)
            {
                cameraManager.TryGetIntrinsics(out cameraIntrinsics);

                m.UpdateMarker(corners[i], cameraIntrinsics, grayedImg, Vector3.forward * rotZ);
            }
            else
            {
                m.UpdateMarker(corners[i], calibrationData.GetCameraMatrix(), calibrationData.GetDistortionCoefficients(), grayedImg, Vector3.forward * rotZ);
            }
        }
    }
Ejemplo n.º 3
0
    //Calibrates the scene camera based on web cam calibration data
    void Calibrate()
    {
        float width  = currentRectSize.x;
        float height = currentRectSize.y;

        float imgScale = 1.0f;

        //Do nothing if the texture is wrong
        if (width <= 0 || height <= 0)
        {
            return;
        }

        //Get the screen/texture ratios
        float widthScale  = (float)Screen.width / width;
        float heightScale = (float)Screen.height / height;

        float aspect = 1;
        Size  imgSize;

        //Get the aspect ratio
        if (widthScale < heightScale)
        {
            aspect  = heightScale;
            imgSize = new Size(width, height);
        }
        else
        {
            float k = (float)Screen.height / (float)Screen.width;
            imgSize = new Size(width, width * k);
            aspect  = widthScale;
        }

        //scale to fit the aspect ratio
        outputImage.transform.localScale = new UnityEngine.Vector3(aspect, aspect, 1);

        //prepare the data for the calibration
        int maxSize = (int)Mathf.Max(width, height);

        double fx = maxSize;
        double fy = maxSize;

        //Principal Point
        double cx = width / 2;
        double cy = height / 2;

        double[,] cameraMatrix = new Double[3, 3];

        cameraMatrix = calibrationData.GetCameraMatrix(ref cameraMatrix);

        double  apertureWidth  = 0;
        double  apertureHeight = 0;
        double  fovx           = -1;
        double  fovy           = -1;
        double  focalLength    = -1;
        Point2d principalPoint = new Point(0, 0);
        double  aspectratio    = -1;

        //Do the camera calibration
        Cv2.CalibrationMatrixValues(cameraMatrix, imgSize, apertureWidth, apertureHeight,
                                    out fovx, out fovy, out focalLength, out principalPoint, out aspectratio);

        fx = cameraMatrix[0, 0];
        fy = cameraMatrix[1, 1];
        cx = cameraMatrix[0, 2];
        cy = cameraMatrix[1, 2];

        //Get the fov scale
        double fovYScale = (2.0 * Mathf.Atan((float)(imgSize.Height / (2.0 * fy)))) / (Mathf.Atan2((float)cy, (float)fy) + Mathf.Atan2((float)(imgSize.Height - cy), (float)fy));

        arCam.fieldOfView = (float)fovy * (float)fovYScale;
    }