예제 #1
0
        Mat calc_simil(Mat pts)
        {
            float[] pts_float = new float[pts.total()];
            MatUtils.copyFromMat <float>(pts, pts_float);
            int pts_cols = pts.cols();

            //compute translation
            int   n = pts.rows() / 2;
            float mx = 0, my = 0;

            for (int i = 0; i < n; i++)
            {
                mx += pts_float[(2 * pts_cols) * i];
                my += pts_float[((2 * pts_cols) * i) + 1];
            }
            using (Mat p = new Mat(2 * n, 1, CvType.CV_32F))
            {
                float[] p_float = new float[p.total()];
                MatUtils.copyFromMat <float>(p, p_float);
                int p_cols = p.cols();

                mx /= n;
                my /= n;
                for (int i = 0; i < n; i++)
                {
                    p_float[(2 * p_cols) * i]       = pts_float[(2 * pts_cols) * i] - mx;
                    p_float[((2 * p_cols) * i) + 1] = pts_float[((2 * pts_cols) * i) + 1] - my;
                }
                MatUtils.copyToMat(p_float, p);

                //compute rotation and scale
                float[] reference_float = new float[reference.total()];
                MatUtils.copyFromMat <float>(reference, reference_float);
                int reference_cols = reference.cols();

                float a = 0, b = 0, c = 0;
                for (int i = 0; i < n; i++)
                {
                    a += reference_float[(2 * reference_cols) * i] * reference_float[(2 * reference_cols) * i] +
                         reference_float[((2 * reference_cols) * i) + 1] * reference_float[((2 * reference_cols) * i) + 1];
                    b += reference_float[(2 * reference_cols) * i] * p_float[(2 * p_cols) * i] +
                         reference_float[((2 * reference_cols) * i) + 1] * p_float[((2 * p_cols) * i) + 1];
                    c += reference_float[(2 * reference_cols) * i] * p_float[((2 * p_cols) * i) + 1] -
                         reference_float[((2 * reference_cols) * i) + 1] * p_float[(2 * p_cols) * i];
                }
                b /= a;
                c /= a;
                float scale = (float)Math.Sqrt(b * b + c * c), theta = (float)Math.Atan2(c, b);
                float sc = scale * (float)Math.Cos(theta), ss = scale * (float)Math.Sin(theta);

                Mat returnMat = new Mat(2, 3, CvType.CV_32F);
                returnMat.put(0, 0, sc, -ss, mx, ss, sc, my);

                return(returnMat);
            }
        }
        public void CaptureFrame(Mat matrix)
        {
            if (uprightBuffer == null)
            {
                return;
            }

            MatUtils.copyToMat(uprightBuffer, matrix);
            Core.flip(matrix, matrix, 0);
        }
예제 #3
0
    public static double CalcError(SMat3 origA, Vector3 x, Vector3 b)
    {
        var A = new Mat3();

        A.SetSymmetric(origA);
        var vtmp = MatUtils.VMul(A, x);

        vtmp = b - vtmp;
        return(vtmp.Dot(vtmp));
    }
예제 #4
0
    private static float calcError(SMat3 origA, Vec3 x, Vec3 b)
    {
        Mat3 A = new Mat3();
        Vec3 vtmp;

        A.setSymmetric(origA);
        MatUtils.vmul(out vtmp, A, x);
        VecUtils.sub(out vtmp, b, vtmp);
        return(VecUtils.dot(vtmp, vtmp));
    }
예제 #5
0
    public static float calcError(SMat3 origA, Vector3 x, Vector3 b)
    {
        Mat3    A = new Mat3();
        Vector3 vtmp;

        A.setSymmetric(origA);
        MatUtils.vmul(out vtmp, A, x);
        vtmp = b - vtmp;
        return(Vector3.Dot(vtmp, vtmp));
    }
예제 #6
0
        public void read(object root_json)
        {
            IDictionary smodel_json = (IDictionary)root_json;

            IDictionary V_json = (IDictionary)smodel_json ["V"];

            V = new Mat((int)(long)V_json ["rows"], (int)(long)V_json ["cols"], CvType.CV_32F);
//              Debug.Log ("V " + V.ToString ());

            IList V_data_json = (IList)V_json ["data"];

            float[] V_data = new float[V.rows() * V.cols()];
            for (int i = 0; i < V_data_json.Count; i++)
            {
                V_data [i] = (float)(double)V_data_json [i];
            }
            MatUtils.copyToMat(V_data, V);
//              Debug.Log ("V dump " + V.dump ());



            IDictionary e_json = (IDictionary)smodel_json ["e"];

            e = new Mat((int)(long)e_json ["rows"], (int)(long)e_json ["cols"], CvType.CV_32F);
//              Debug.Log ("e " + e.ToString ());

            IList e_data_json = (IList)e_json ["data"];

            float[] e_data = new float[e.rows() * e.cols()];
            for (int i = 0; i < e_data_json.Count; i++)
            {
                e_data [i] = (float)(double)e_data_json [i];
            }
            MatUtils.copyToMat(e_data, e);
//              Debug.Log ("e dump " + e.dump ());



            IDictionary C_json = (IDictionary)smodel_json ["C"];

            C = new Mat((int)(long)C_json ["rows"], (int)(long)C_json ["cols"], CvType.CV_32S);
//              Debug.Log ("C " + C.ToString ());

            IList C_data_json = (IList)C_json ["data"];

            int[] C_data = new int[C.rows() * C.cols()];
            for (int i = 0; i < C_data_json.Count; i++)
            {
                C_data [i] = (int)(long)C_data_json [i];
            }
            MatUtils.copyToMat(C_data, C);
//              Debug.Log ("C dump " + C.dump ());

            p = Mat.zeros(e.rows(), 1, CvType.CV_32F);
        }
예제 #7
0
파일: FaceDetect.cs 프로젝트: mbyase/eyecam
        // Update is called once per frame
        void Update()
        {
            if (webCamTextureToMatHelper.IsPlaying() && webCamTextureToMatHelper.DidUpdateThisFrame())
            {
                Mat rgbaMat = webCamTextureToMatHelper.GetMat();



                Imgproc.cvtColor(rgbaMat, grayMat, Imgproc.COLOR_RGBA2GRAY);
                Imgproc.equalizeHist(grayMat, grayMat);

                // detect faces
                cascade.detectMultiScale(grayMat, faces, 1.1, 2, 2, // TODO: objdetect.CV_HAAR_SCALE_IMAGE
                                         new Size(grayMat.cols() * 0.2, grayMat.rows() * 0.2), new Size());

                if (faces.total() > 0)
                {
                    // fit landmarks for each found face
                    List <MatOfPoint2f> landmarks = new List <MatOfPoint2f>();
                    facemark.fit(grayMat, faces, landmarks);


                    Rect[] rects = faces.toArray();
                    for (int i = 0; i < rects.Length; i++)
                    {
                        //Debug.Log ("detect faces " + rects [i]);

                        Imgproc.rectangle(rgbaMat, new Point(rects[i].x, rects[i].y), new Point(rects[i].x + rects[i].width, rects[i].y + rects[i].height), new Scalar(255, 0, 0, 255), 2);
                    }

                    // draw them
                    for (int i = 0; i < landmarks.Count; i++)
                    {
                        MatOfPoint2f lm       = landmarks[i];
                        float[]      lm_float = new float[lm.total() * lm.channels()];
                        MatUtils.copyFromMat <float>(lm, lm_float);

                        DrawFaceLandmark(rgbaMat, ConvertArrayToPointList(lm_float), new Scalar(0, 255, 0, 255), 2);

                        //for (int j = 0; j < lm_float.Length; j = j + 2)
                        //{
                        //    Point p = new Point(lm_float[j], lm_float[j + 1]);
                        //    Imgproc.circle(rgbaMat, p, 2, new Scalar(255, 0, 0, 255), 1);
                        //}
                    }
                }


                //     rgbaMat.convertTo(rgbaMat, CvType.CV_8UC3);
                //    Debug.Log(rgbaMat.type());
                //  OpenCVForUnity.XphotoModule.Xphoto.applyChannelGains(rgbaMat, effectsMat, 1.0f,220.0f, 1.0f);
                //  OpenCVForUnity.XphotoModule.Xphoto.oilPainting(effects, effects, 10, 10);
                Utils.fastMatToTexture2D(rgbaMat, texture);
            }
        }
예제 #8
0
    public static float solveLeastSquares(Mat3 a, Vector3 b, Vector3 x, float svd_tol, int svd_sweeps, float pinv_tol)
    {
        Mat3    at;
        SMat3   ata;
        Vector3 atb;

        MatUtils.transpose(out at, a);
        MatUtils.mmul_ata(out ata, a);
        MatUtils.vmul(out atb, at, b);
        return(solveSymmetric(ata, atb, x, svd_tol, svd_sweeps, pinv_tol));
    }
예제 #9
0
    public static float solveSymmetric(SMat3 A, Vector3 b, Vector3 x, float svd_tol, int svd_sweeps, float pinv_tol)
    {
        Mat3  pinv;
        Mat3  V    = new Mat3();
        SMat3 VTAV = new SMat3();

        getSymmetricSvd(A, VTAV, V, svd_tol, svd_sweeps);
        pseudoinverse(out pinv, VTAV, V, pinv_tol);
        MatUtils.vmul(out x, pinv, b);
        return(calcError(A, x, b));
    }
        void DoProcess()
        {
            if (!(mat.Value is OpenCVForUnityPlayMakerActions.Mat))
            {
                LogError("mat is not initialized. Add Action \"newMat\".");
                return;
            }
            OpenCVForUnity.CoreModule.Mat wrapped_mat = OpenCVForUnityPlayMakerActionsUtils.GetWrappedObject <OpenCVForUnityPlayMakerActions.Mat, OpenCVForUnity.CoreModule.Mat>(mat);

            MatUtils.copyToMat <int>(array.intValues, wrapped_mat);
        }
예제 #11
0
    public static double SolveSymmetric(SMat3 A, Vector3 b, out Vector3 x, double svd_tol, int svd_sweeps, double pinv_tol)
    {
        var V    = new Mat3();
        var VTAV = new SMat3();

        GetSymmetricSvd(A, VTAV, V, svd_tol, svd_sweeps);
        var pinv = PseudoInverse(VTAV, V, pinv_tol);

        x = MatUtils.VMul(pinv, b);
        return(CalcError(A, x, b));
    }
예제 #12
0
    public static float solveSymmetric(SMat3 A, Vec3 b, out Vec3 x,
                                       float svd_tol, int svd_sweeps, float pinv_tol)
    {
        Mat3  mtmp, pinv, V;
        SMat3 VTAV;

        getSymmetricSvd(A, out VTAV, out V, svd_tol, svd_sweeps);
        psuedoinverse(out pinv, VTAV, V, pinv_tol);
        MatUtils.vmul(out x, pinv, b);
        return(calcError(A, x, b));
    }
예제 #13
0
    public double GetError(Vector3 pos)
    {
        if (!hasSolution)
        {
            SetAta();
            SetAtb();
        }

        Vector3 atax = MatUtils.VMuSymmetric(ata, pos);

        return(pos.Dot(atax) - 2 * pos.Dot(atb) + QefData.btb);
    }
예제 #14
0
        // -------------------------------------------------------------------------------
        public float GetError(Vector3 pos)
        {
            if (!this.hasSolution)
            {
                this.SetAta();
                this.SetAtb();
            }

            Vector3 atax = MatUtils.Vmul_symmetric(this.ata, pos);

            return(VecUtils.Dot(pos, atax) - 2 * VecUtils.Dot(pos, this.atb) + this.data.btb);
        }
예제 #15
0
        public Point[] calc_peaks(Mat im, Point[] points, Size ssize)
        {
            int n = points.Length;

            using (Mat pt = (new MatOfPoint2f(points)).reshape(1, 2 * n)) using (Mat S = calc_simil(pt)) using (Mat Si = inv_simil(S))
                    {
                        float[] pt_float = new float[pt.total()];
                        MatUtils.copyFromMat <float>(pt, pt_float);
                        int pt_cols = pt.cols();

                        float[] S_float = new float[S.total()];
                        MatUtils.copyFromMat <float>(S, S_float);
                        int S_cols = S.cols();

                        float[] A_float = new float[2 * 3];

                        Point[] pts = apply_simil(Si, points);

                        for (int i = 0; i < n; i++)
                        {
                            Size wsize = new Size(ssize.width + patches[i].patch_size().width, ssize.height + patches[i].patch_size().height);
                            using (Mat A = new Mat(2, 3, CvType.CV_32F))
                            {
                                MatUtils.copyFromMat <float>(A, A_float);
                                int A_cols = A.cols();

                                A_float[0]                = S_float[0];
                                A_float[1]                = S_float[1];
                                A_float[1 * A_cols]       = S_float[1 * S_cols];
                                A_float[(1 * A_cols) + 1] = S_float[(1 * S_cols) + 1];
                                A_float[2]                = (float)(pt_float[(2 * pt_cols) * i] -
                                                                    (A_float[0] * (wsize.width - 1) / 2 + A_float[1] * (wsize.height - 1) / 2));
                                A_float[(1 * A_cols) + 2] = (float)(pt_float[((2 * pt_cols) * i) + 1] -
                                                                    (A_float[1 * A_cols] * (wsize.width - 1) / 2 + A_float[(1 * A_cols) + 1] * (wsize.height - 1) / 2));

                                MatUtils.copyToMat(A_float, A);

                                using (Mat I = new Mat())
                                {
                                    Imgproc.warpAffine(im, I, A, wsize, Imgproc.INTER_LINEAR + Imgproc.WARP_INVERSE_MAP);
                                    using (Mat R = patches[i].calc_response(I, false))
                                    {
                                        Core.MinMaxLocResult minMaxLocResult = Core.minMaxLoc(R);
                                        pts[i].x = pts[i].x + minMaxLocResult.maxLoc.x - 0.5 * ssize.width;
                                        pts[i].y = pts[i].y + minMaxLocResult.maxLoc.y - 0.5 * ssize.height;
                                    }
                                }
                            }
                        }
                        return(apply_simil(S, pts));
                    }
        }
예제 #16
0
    public static void getSymmetricSvd(SMat3 a, SMat3 vtav, Mat3 v, float tol, int max_sweeps)
    {
        vtav.setSymmetric(a);
        v.set(1, 0, 0, 0, 1, 0, 0, 0, 1);
        float delta = tol * MatUtils.fnorm(vtav);

        for (int i = 0; i < max_sweeps && MatUtils.off(vtav) > delta; ++i)
        {
            rotate01(vtav, v);
            rotate02(vtav, v);
            rotate12(vtav, v);
        }
    }
예제 #17
0
    public float getError(Vector3 pos)
    {
        if (!hasSolution)
        {
            setAta();
            setAtb();
        }

        Vector3 atax;

        MatUtils.vmul_symmetric(out atax, ata, pos);
        return(Vector3.Dot(pos, atax) - 2 * Vector3.Dot(pos, atb) + data.btb);
    }
예제 #18
0
        void DoProcess()
        {
            System.Int64[] wrapped_array = OpenCVForUnityPlayMakerActionsUtils.GetWrappedObject <OpenCVForUnityPlayMakerActions.LongArray, System.Int64[]>(array);

            if (!(mat.Value is OpenCVForUnityPlayMakerActions.Mat))
            {
                LogError("mat is not initialized. Add Action \"newMat\".");
                return;
            }
            OpenCVForUnity.CoreModule.Mat wrapped_mat = OpenCVForUnityPlayMakerActionsUtils.GetWrappedObject <OpenCVForUnityPlayMakerActions.Mat, OpenCVForUnity.CoreModule.Mat>(mat);

            MatUtils.copyToMat <long>(wrapped_array, wrapped_mat);
        }
예제 #19
0
    public static void GetSymmetricSvd(SMat3 a, SMat3 vtav, Mat3 v, double tol, int max_sweeps)
    {
        vtav.SetSymmetric(a);
        v.Set(1, 0, 0, 0, 1, 0, 0, 0, 1);
        double delta = tol * MatUtils.Norm(vtav);

        for (int i = 0; i < max_sweeps && MatUtils.off(vtav) > delta; ++i)
        {
            Rotate01(vtav, v);
            Rotate02(vtav, v);
            Rotate12(vtav, v);
        }
    }
        void DoProcess()
        {
            if (!(mat.Value is OpenCVForUnityPlayMakerActions.Mat))
            {
                LogError("mat is not initialized. Add Action \"newMat\".");
                return;
            }
            OpenCVForUnity.CoreModule.Mat wrapped_mat = OpenCVForUnityPlayMakerActionsUtils.GetWrappedObject <OpenCVForUnityPlayMakerActions.Mat, OpenCVForUnity.CoreModule.Mat>(mat);

            System.Double[] wrapped_array = OpenCVForUnityPlayMakerActionsUtils.GetWrappedObject <OpenCVForUnityPlayMakerActions.DoubleArray, System.Double[]>(array);

            MatUtils.copyFromMat <double>(wrapped_mat, wrapped_array);
        }
예제 #21
0
        /// <summary>
        /// Handles the event of a new image getting captured.
        /// </summary>
        /// <param name="imageData">The raw data of the image.</param>
        private void OnCaptureRawImageComplete(byte[] imageData)
        {
            lock (_cameraLockObject)
            {
                _isCapturing = false;
            }
            //// Initialize to 8x8 texture so there is no discrepency
            //// between uninitalized captures and error texture
            //Texture2D texture = new Texture2D(8, 8);
            //bool status = texture.LoadImage(imageData);

            //if (status && (texture.width != 8 && texture.height != 8))
            //{
            //    _previewObject.SetActive(true);
            //    Renderer renderer = _previewObject.GetComponent<Renderer>();
            //    if (renderer != null)
            //    {
            //        renderer.material.mainTexture = texture;
            //    }
            //}

            Mat buff = new Mat(1, imageData.Length, CvType.CV_8UC1);

            MatUtils.copyToMat <byte>(imageData, buff);

            Mat imgMat = Imgcodecs.imdecode(buff, Imgcodecs.IMREAD_COLOR);

            //            Debug.Log ("imgMat.ToString() " + imgMat.ToString ());
            buff.Dispose();

            Run(imgMat);

            Imgproc.cvtColor(imgMat, imgMat, Imgproc.COLOR_BGR2RGB);

            Texture2D outputTexture = new Texture2D(imgMat.width(), imgMat.height(), TextureFormat.RGBA32, false);

            Utils.matToTexture2D(imgMat, outputTexture);

            imgMat.Dispose();

            //if (status && (texture.width != 8 && texture.height != 8))
            //{
            _previewObject.SetActive(true);
            Renderer renderer = _previewObject.GetComponent <Renderer>();

            if (renderer != null)
            {
                renderer.material.mainTexture = outputTexture;
            }
            //}
        }
예제 #22
0
        public void calc_params(Point[] pts, Mat weight, float c_factor)
        {
            int n = pts.Length;

            //assert(V.rows == 2*n);
            //Debug.Log ("V.rows == 2*n " + V.rows () + " " + 2 * n);

            using (Mat s = (new MatOfPoint2f(pts)).reshape(1, 2 * n))
            { //point set to vector format
                if (weight.total() == 0)
                {
                    Core.gemm(V.t(), s, 1, new Mat(), 0, p);     //simple projection
                }
                else
                {
                    //scaled projection
                    if (weight.rows() != n)
                    {
                        Debug.Log("Invalid weighting matrix");
                    }

                    float[] weight_float = new float[weight.total()];
                    MatUtils.copyFromMat <float>(weight, weight_float);
                    int weight_cols = weight.cols();

                    int K = V.cols();
                    using (Mat H = Mat.zeros(K, K, CvType.CV_32F)) using (Mat g = Mat.zeros(K, 1, CvType.CV_32F))
                        {
                            for (int i = 0; i < n; i++)
                            {
                                using (Mat v = new Mat(V, new OpenCVForUnity.CoreModule.Rect(0, 2 * i, K, 2))) using (Mat tmpMat1 = new Mat()) using (Mat tmpMat2 = new Mat()) using (Mat tmpMat3 = new Mat())
                                            {
                                                float w = weight_float[i * weight_cols];

                                                Core.multiply(v.t(), new Scalar(w), tmpMat1);

                                                Core.gemm(tmpMat1, v, 1, new Mat(), 0, tmpMat2);
                                                Core.add(H, tmpMat2, H);

                                                Core.gemm(tmpMat1, new MatOfPoint2f(pts[i]).reshape(1, 2), 1, new Mat(), 0, tmpMat3);
                                                Core.add(g, tmpMat3, g);
                                            }
                            }

                            Core.solve(H, g, p, Core.DECOMP_SVD);
                        }
                }
            }
            clamp(c_factor);          //clamp resulting parameters
        }
예제 #23
0
    public float getError(Vec3 pos)
    {
        if (!this.hasSolution)
        {
            this.setAta();
            this.setAtb();
        }

        Vec3 atax;

        MatUtils.vmul_symmetric(out atax, this.ata, pos);
        return(VecUtils.dot(pos, atax) - 2.0f * VecUtils.dot(pos, this.atb)
               + this.data.btb);
    }
예제 #24
0
        public Point[] getConnections()
        {
            Point[] c    = new Point[smodel.C.rows()];
            int[]   data = new int[c.Length * 2];
            MatUtils.copyFromMat <int> (smodel.C, data);

            int len = c.Length;

            for (int i = 0; i < len; i++)
            {
                c [i] = new Point(data [i * 2], data [(i * 2) + 1]);
            }

            return(c);
        }
        void Update()
        {
            if (reader != null)
            {
                ColorFrame frame = reader.AcquireLatestFrame();

                if (frame != null)
                {
                    frame.CopyConvertedFrameDataToArray(data, ColorImageFormat.Rgba);

                    frame.Dispose();
                    frame = null;
                }
            }
            else
            {
                return;
            }

            MatUtils.copyToMat(data, rgbaMat);


            if (filterType == FilterTypePreset.NONE)
            {
                Imgproc.putText(rgbaMat, "Filter Type: NONE " + texture.width + "x" + texture.height, new Point(5, texture.height - 5), Imgproc.FONT_HERSHEY_PLAIN, 4.0, new Scalar(255, 0, 0, 255), 3);
            }
            else if (filterType == FilterTypePreset.SEPIA)
            {
                Core.transform(rgbaMat, rgbaMat, sepiaKernel);

                Imgproc.putText(rgbaMat, "Filter Type: SEPIA " + texture.width + "x" + texture.height, new Point(5, texture.height - 5), Imgproc.FONT_HERSHEY_PLAIN, 4.0, new Scalar(255, 0, 0, 255), 3);
            }
            else if (filterType == FilterTypePreset.PIXELIZE)
            {
                Imgproc.resize(rgbaMat, pixelizeIntermediateMat, pixelizeSize0, 0.1, 0.1, Imgproc.INTER_NEAREST);
                Imgproc.resize(pixelizeIntermediateMat, rgbaMat, rgbaMat.size(), 0.0, 0.0, Imgproc.INTER_NEAREST);

                Imgproc.putText(rgbaMat, "Filter Type: PIXELIZE " + texture.width + "x" + texture.height, new Point(5, texture.height - 5), Imgproc.FONT_HERSHEY_PLAIN, 4.0, new Scalar(255, 0, 0, 255), 3);
            }
            else if (filterType == FilterTypePreset.COMIC)
            {
                comicFilter.Process(rgbaMat, rgbaMat);

                Imgproc.putText(rgbaMat, "Filter Type: COMIC " + texture.width + "x" + texture.height, new Point(5, texture.height - 5), Imgproc.FONT_HERSHEY_PLAIN, 4.0, new Scalar(255, 0, 0, 255), 3);
            }

            Utils.matToTexture2D(rgbaMat, texture);
        }
예제 #26
0
    public static void getSymmetricSvd(SMat3 a, out SMat3 vtav, out Mat3 v,
                                       float tol, int max_sweeps)
    {
        vtav = new SMat3();
        v    = new Mat3();
        vtav.setSymmetric(a);
        v.set(1, 0, 0, 0, 1, 0, 0, 0, 1);
        float delta = tol * MatUtils.fnorm(vtav);

        for (int i = 0; i < max_sweeps && MatUtils.off(vtav) > delta; ++i)
        {
            Schur2.rotate01(out vtav, out v, vtav, v);
            Schur2.rotate02(out vtav, out v, vtav, v);
            Schur2.rotate12(out vtav, out v, vtav, v);
        }
    }
예제 #27
0
        Point[] apply_simil(Mat S, Point[] points)
        {
            float[] S_float = new float[S.total()];
            MatUtils.copyFromMat <float> (S, S_float);
            int S_cols = S.cols();

            int n = points.Length;

            Point[] p = new Point[n];
            for (int i = 0; i < n; i++)
            {
                p [i]   = new Point();
                p [i].x = S_float [0] * points [i].x + S_float [1] * points [i].y + S_float [2];
                p [i].y = S_float [1 * S_cols] * points [i].x + S_float [(1 * S_cols) + 1] * points [i].y + S_float [(1 * S_cols) + 2];
            }
            return(p);
        }
예제 #28
0
        public void read(object root_json)
        {
            IDictionary pmodel_json = (IDictionary)root_json;

            IDictionary P_json = (IDictionary)pmodel_json["P"];

            P = new Mat((int)(long)P_json["rows"], (int)(long)P_json["cols"], CvType.CV_32F);
            //Debug.Log ("P " + P.ToString ());

            IList P_data_json = (IList)P_json["data"];

            float[] P_data = new float[P.rows() * P.cols()];
            for (int i = 0; i < P_data_json.Count; i++)
            {
                P_data[i] = (float)(double)P_data_json[i];
            }
            MatUtils.copyToMat(P_data, P);
        }
예제 #29
0
        /// <summary>
        /// Processes the received frame, converts the image to grayscale if requested, and invokes the next photo request.
        /// </summary>
        private void OnCapturedPhotoToMemory(PhotoCapture.PhotoCaptureResult result, PhotoCaptureFrame photoCaptureFrame)
        {
            if (_stopped?.Task != null)
            {
                return;
            }
            if (result.resultType == PhotoCapture.CaptureResultType.UnknownError)
            {
                return;
            }
            if (photoCaptureFrame == null)
            {
                return;
            }
            Size size = new Size(FrameWidth, (double)FrameHeight * 3 / 2); // Luminance (grayscale) of the NV12 format requires image height, chrominance is stored in half resolution. <see href="https://docs.microsoft.com/en-us/windows/win32/medfound/recommended-8-bit-yuv-formats-for-video-rendering#nv12"/>.

            _image = new Mat(size, CvType.CV_8UC1);
            List <byte> imageBuffer = new List <byte>();

            photoCaptureFrame?.CopyRawImageDataIntoBuffer(imageBuffer);
            MatUtils.copyToMat(imageBuffer.ToArray(), _image);

            if (_format == ColorFormat.Grayscale)
            {
                Imgproc.cvtColor(_image, _image, Imgproc.COLOR_YUV2GRAY_NV12);
            }

            Matrix4x4 cameraToWorldMatrix = Matrix4x4.identity;

            photoCaptureFrame?.TryGetCameraToWorldMatrix(out cameraToWorldMatrix);
            CameraExtrinsic extrinsic = new CameraExtrinsic(cameraToWorldMatrix);

            Matrix4x4 projectionMatrix = Matrix4x4.identity;

            photoCaptureFrame?.TryGetProjectionMatrix(out projectionMatrix);
            CameraIntrinsic intrinsic = new CameraIntrinsic(projectionMatrix);

            CameraFrame           cameraFrame = new CameraFrame(_image, intrinsic, extrinsic, FrameWidth, FrameHeight, FrameCount++, _format);
            FrameArrivedEventArgs args        = new FrameArrivedEventArgs(cameraFrame);

            FrameArrived?.Invoke(this, args);

            _photoCaptureObject?.TakePhotoAsync(OnCapturedPhotoToMemory);
        }
예제 #30
0
        void DoProcess()
        {
            if (!(mat.Value is OpenCVForUnityPlayMakerActions.Mat))
            {
                LogError("mat is not initialized. Add Action \"newMat\".");
                return;
            }
            OpenCVForUnity.CoreModule.Mat wrapped_mat = OpenCVForUnityPlayMakerActionsUtils.GetWrappedObject <OpenCVForUnityPlayMakerActions.Mat, OpenCVForUnity.CoreModule.Mat>(mat);

            int[] tmpArray = new int[array.Length];

            MatUtils.copyFromMat <int>(wrapped_mat, tmpArray);

            for (int i = 0; i < tmpArray.Length; i++)
            {
                array.Set(i, (float)tmpArray[i]);
            }
            array.SaveChanges();
        }