Exemplo n.º 1
0
    private void Calibrate()
    {
        Debug.Log("Calibrating Async.....");
        // calibrationMutex.WaitOne();

        if (OnCalibrationStarted != null)
        {
            CalibrateCamera.OnCalibrationStarted();
        }

        //prepare the data which the calibration process will fill up
        int    maxSize = (int)Mathf.Max(imageWidth, imageHeight);
        double fx      = maxSize;
        double fy      = maxSize;

        double cx = (double)imageWidth / 2;
        double cy = (double)imageHeight / 2;

        double[,] k = new double[3, 3]
        {
            { fx, 0d, cx },
            { 0d, fy, cy },
            { 0d, 0d, 1d }
        };

        double[] d = new double[5];
        double   projectionError = -1;

        Vec3d[] rvec = new Vec3d[boardWidth * boardHeight];
        Vec3d[] tvec = new Vec3d[boardWidth * boardHeight];

        Size boardSize = new Size(boardWidth, boardHeight);

        try
        {
            // calibrate the camera
            projectionError = Cv2.CalibrateCamera(objPoints, CornerPoints, new Size(imageWidth, imageHeight), k, d,
                                                  out rvec, out tvec,
                                                  calibrationFlags, TermCriteria.Both(30, 0.1));
            Debug.Log("Error: " + projectionError);
        }
        catch (Exception e)
        {
            ResetCalibrationImmediate();
            Debug.Log("restarting...");
        }

        //register the data and save them
        calibrationData.RegisterMatrix(k);
        calibrationData.RegisterDistortionCoefficients(d);

        calibrationData.projectionError = projectionError;

        string s = "";

        for (int i = 0; i < d.Length; i++)
        {
            s += d[i] + " ";
        }

        Debug.Log(s);
        Debug.Log("Finished!!");

        if (OnCalibrationFinished != null)
        {
            OnCalibrationFinished(calibrationData);
        }

        //calibrationMutex.ReleaseMutex();
    }