Beispiel #1
0
    void _ProcessCalibration()
    {
        Utils.webCamTextureToMat(webCamTexture, rgbaMat, colors);
        Imgproc.cvtColor(rgbaMat, rgbMat, Imgproc.COLOR_RGBA2RGB);

        int imageWidth  = Screen.width;
        int imageHeight = Screen.height;
        Mat cameraMat   = new Mat(new Size(imageWidth, imageHeight), CvType.CV_8UC3);

        Imgproc.resize(rgbMat, cameraMat, cameraMat.size());


        Mat gray = new Mat(imageHeight, imageWidth, CvType.CV_8UC1);

        Imgproc.cvtColor(cameraMat, gray, Imgproc.COLOR_RGB2GRAY);

        Mat grayC3 = new Mat(imageHeight, imageWidth, CvType.CV_8UC3);

        Imgproc.cvtColor(gray, grayC3, Imgproc.COLOR_GRAY2RGB);

        int rectW = (int)(imageHeight * 0.4);
        int rectH = (int)(imageHeight * 0.3);
        var x     = (int)(imageWidth * 0.5 - (rectW / 2));
        var y     = (int)(imageHeight * 0.5 - (rectH / 2));
        var rect  = new OpenCVForUnity.Rect(x, y, rectW, rectH);

        var center    = new Point(imageWidth / 2.0, imageHeight / 2.0);
        var lineColor = new Scalar(255, 153, 153);

        var rotatedRect      = new RotatedRect(center, new Size(rectW, rectH), 0);
        var rotatedSmallRect = new RotatedRect(center, new Size((int)(rectW * 0.7), (int)(rectH * 0.7)), 0);

        Imgproc.ellipse(grayC3, rotatedRect, lineColor, 3);
        Imgproc.ellipse(grayC3, rotatedSmallRect, lineColor, 3);

        //outputScreenQuad.setMat(grayC3);

        if (startProcess)
        {
            var mask = Mat.zeros(imageHeight, imageWidth, CvType.CV_8UC1);
            Imgproc.ellipse(mask, rotatedRect, new Scalar(255), -1);

            var hsvChs   = ARUtil.getHSVChannels(cameraMat);
            var yCrCbChs = ARUtil.getYCrCbChannels(cameraMat);


            foreach (var chStr in new List <string> {
                "s", "v", "cr"
            })
            {
                MatOfDouble meanMat   = new MatOfDouble();
                MatOfDouble stddevMat = new MatOfDouble();
                Mat         chMat     = new Mat();
                if (chStr == "s")
                {
                    chMat = hsvChs[1];
                }
                else if (chStr == "v")
                {
                    chMat = hsvChs[2];
                }
                else
                {
                    chMat = yCrCbChs[1];
                }
                Core.meanStdDev(chMat, meanMat, stddevMat, mask);
                var mean   = meanMat.toList()[0];
                var stddev = stddevMat.toList()[0];

                // 95%信頼区間
                if (chStr == "s")
                {
                    s_threshold_lower = mean - stddev * 1.96 - 20;
                    s_threshold_upper = mean + stddev * 1.96 + 20;
                }
                else if (chStr == "v")
                {
                    v_threshold_lower = mean - stddev * 1.96 - 80;
                    v_threshold_upper = mean + stddev * 1.96 + 80;
                }
                else
                {
                    cr_threshold_lower = mean - stddev * 1.96 - 20;
                    cr_threshold_upper = mean + stddev * 1.96 + 20;
                }
            }

            H_sourceMean = (int)(Core.mean(hsvChs[0], mask).val[0]);

            doneSetThreshlod = true;
        }
        else
        {
            outputScreenQuad.setMat(grayC3);
        }
    }
    void ProcessCalibration()
    {
        // UnityのTexture2DからOpencvのMatに変換
        int imageWidth  = cameraTexture.width;
        int imageHeight = cameraTexture.height;

        UnityEngine.Rect wholeRect = new UnityEngine.Rect(0, 0, cameraTexture.width, cameraTexture.height);
        cameraTexture.ReadPixels(wholeRect, 0, 0, true);
        //cameraMat = new Mat(imageHeight, imageWidth, CvType.CV_8UC3);
        //cameraTexture = new Texture2D(imageWidth, imageHeight, TextureFormat.ARGB32, false);
        //image.CopyToTexture(cameraTexture);
        Utils.texture2DToMat(cameraTexture, cameraMat);


        Mat gray = new Mat(imageHeight, imageWidth, CvType.CV_8UC1);

        Imgproc.cvtColor(cameraMat, gray, Imgproc.COLOR_RGB2GRAY);

        Mat grayC3 = new Mat(imageHeight, imageWidth, CvType.CV_8UC3);

        Imgproc.cvtColor(gray, grayC3, Imgproc.COLOR_GRAY2RGB);

        int rectW = (int)(imageHeight * 0.4);
        int rectH = (int)(imageHeight * 0.3);
        var x     = (int)(imageWidth * 0.5 - (rectW / 2));
        var y     = (int)(imageHeight * 0.5 - (rectH / 2));
        var rect  = new OpenCVForUnity.Rect(x, y, rectW, rectH);

        var center    = new Point(imageWidth / 2.0, imageHeight / 2.0);
        var lineColor = new Scalar(255, 153, 153);

        var rotatedRect      = new RotatedRect(center, new Size(rectW, rectH), 0);
        var rotatedSmallRect = new RotatedRect(center, new Size((int)(rectW * 0.7), (int)(rectH * 0.7)), 0);

        Imgproc.ellipse(grayC3, rotatedRect, lineColor, 3);
        Imgproc.ellipse(grayC3, rotatedSmallRect, lineColor, 3);

        //outputScreenQuad.setMat(grayC3);


        if (startProcess)
        {
            Debug.Log("startProcess");
            var mask = Mat.zeros(imageHeight, imageWidth, CvType.CV_8UC1);
            Imgproc.ellipse(mask, rotatedRect, new Scalar(255), -1);

            var hsvChs   = ARUtil.getHSVChannels(cameraMat);
            var yCrCbChs = ARUtil.getYCrCbChannels(cameraMat);

            foreach (var chStr in new List <string> {
                "s", "v", "cr"
            })
            {
                MatOfDouble meanMat   = new MatOfDouble();
                MatOfDouble stddevMat = new MatOfDouble();
                Mat         chMat     = new Mat();
                if (chStr == "s")
                {
                    chMat = hsvChs[1];
                }
                else if (chStr == "v")
                {
                    chMat = hsvChs[2];
                }
                else
                {
                    chMat = yCrCbChs[1];
                }
                Core.meanStdDev(chMat, meanMat, stddevMat, mask);
                var mean   = meanMat.toList()[0];
                var stddev = stddevMat.toList()[0];

                if (chStr == "s")
                {
                    s_threshold_lower = mean - stddev * 2;
                    s_threshold_upper = mean + stddev * 2;
                }
                else if (chStr == "v")
                {
                    v_threshold_lower = mean - stddev * 2;
                    v_threshold_upper = mean + stddev * 2;
                }
                else
                {
                    cr_threshold_lower = mean - stddev * 2;
                    cr_threshold_upper = mean + stddev * 2;
                }
            }


            doneSetThreshlod = true;
        }
        else
        {
            outputScreenQuad.setMat(grayC3);
        }
    }
Beispiel #3
0
    void ProcessSetting()
    {
        Utils.webCamTextureToMat(webCamTexture, rgbaMat, colors);
        Mat cameraMat = new Mat(rgbaMat.size(), CvType.CV_8UC3);

        Imgproc.cvtColor(rgbaMat, cameraMat, Imgproc.COLOR_RGBA2RGB);

        Mat dst  = new Mat(cameraMat.size(), cameraMat.type());
        Mat _mat = new Mat(cameraMat.size(), cameraMat.type());

        Imgproc.resize(mat, _mat, _mat.size());
        Core.addWeighted(cameraMat, 0.5, _mat, 0.5, 0.0, dst);

        if (isSetClicked)
        {
            Mat handGray = new Mat(_mat.size(), CvType.CV_8UC1);
            Imgproc.cvtColor(_mat, handGray, Imgproc.COLOR_RGB2GRAY);
            Imgproc.threshold(handGray, handGray, 10.0, 255.0, Imgproc.THRESH_BINARY);
            Imgproc.erode(handGray, handGray, Imgproc.getStructuringElement(Imgproc.MORPH_ERODE, new Size(15, 15)), new Point(-1, -1), 8);

            outputScreenQuad.setMat(handGray);

            var hsvChs   = ARUtil.getHSVChannels(cameraMat);
            var yCrCbChs = ARUtil.getYCrCbChannels(cameraMat);

            foreach (var chStr in new List <string> {
                "s", "v", "cr"
            })
            {
                MatOfDouble meanMat   = new MatOfDouble();
                MatOfDouble stddevMat = new MatOfDouble();
                Mat         chMat     = new Mat();
                if (chStr == "s")
                {
                    chMat = hsvChs[1];
                }
                else if (chStr == "v")
                {
                    chMat = hsvChs[2];
                }
                else
                {
                    chMat = yCrCbChs[1];
                }
                Core.meanStdDev(chMat, meanMat, stddevMat, handGray);
                var mean   = meanMat.toList()[0];
                var stddev = stddevMat.toList()[0];

                if (chStr == "s")
                {
                    s_threshold_lower = mean - stddev;
                    s_threshold_upper = mean + stddev;
                }
                else if (chStr == "v")
                {
                    v_threshold_lower = mean - stddev;
                    v_threshold_upper = mean + stddev * 1.96;
                }
                else
                {
                    cr_threshold_lower = mean - stddev;
                    cr_threshold_upper = mean + stddev;
                }
            }
            doneSetThreshlod = true;
        }
        else
        {
            outputScreenQuad.setMat(dst);
        }
    }