Пример #1
0
        // Use this for initialization
        public void ProcessWebCam(WebCamTexture webCamTexture)
        {
            var rawImage = gameObject.GetComponent <RawImage>();

            rawImage.texture = null;

            scanner.Settings.NoiseReduction = 0.7;                                              // real-world images are quite noisy, this value proved to be reasonable
            scanner.Settings.EdgesTight     = 8;                                                // higher value cuts off "noise" as well, this time smaller and weaker edges
            scanner.Settings.ExpectedArea   = 0.05;                                             // we expect document to be at least 20% of the total image area
            scanner.Settings.GrayMode       = PaperScanner.ScannerSettings.ColorMode.Grayscale; // color -> grayscale conversion mode

            // process input with PaperScanner
            Mat result = null;

            scanner.Input = Unity.TextureToMat(webCamTexture);//★scanner.Successで使用する


            // should we fail, there is second try - HSV might help to detect paper by color difference
            if (!scanner.Success)
            {
                scanner.Settings.GrayMode = PaperScanner.ScannerSettings.ColorMode.HueGrayscale;
            }

            result = scanner.Output;

            rawImage.texture = Unity.MatToTexture(result);
            var transform = gameObject.GetComponent <RectTransform>();
        }
Пример #2
0
        // Use this for initialization
        void Start()
        {
            Mat mat = Unity.TextureToMat(this.texture);

            Vector3[,] v = new Vector3[mat.Height, mat.Width];
            for (int yi = 0; yi < mat.Height; yi++)
            {
                for (int xi = 0; xi < mat.Width; xi++)
                {
                    Vec3b vyx = mat.At <Vec3b>(yi, xi);
                    v[yi, xi][0] = vyx[0];
                    v[yi, xi][1] = vyx[1];
                    v[yi, xi][2] = vyx[2];
                }
            }
            v = Median(v, mat.Height, mat.Width);
            for (int yi = 0; yi < mat.Height; yi++)
            {
                for (int xi = 0; xi < mat.Width; xi++)
                {
                    Vec3b vyx = new Vec3b();
                    vyx[0] = (byte)v[yi, xi][0];
                    vyx[1] = (byte)v[yi, xi][1];
                    vyx[2] = (byte)v[yi, xi][2];
                    mat.Set <Vec3b>(yi, xi, vyx);
                }
            }
            Texture2D changedTex = Unity.MatToTexture(mat);

            GetComponent <RawImage>().texture = changedTex;
        }
Пример #3
0
        public Texture getScanFrame(WebCamTexture inputTexture)
        {
            Mat  original  = Unity.TextureToMat(inputTexture);
            Size inputSize = new Size(original.Width, original.Height);

            scanner.Input = Unity.TextureToMat(inputTexture);

            if (!scanner.Success)
            {
                scanner.Settings.GrayMode = PaperScanner.ScannerSettings.ColorMode.HueGrayscale;
            }

            Point[] detectedContour = scanner.PaperShape;

            var matCombinedFrame = new Mat(new Size(inputSize.Width, inputSize.Height), original.Type(), Scalar.FromRgb(64, 64, 64));

            original.CopyTo(matCombinedFrame.SubMat(0, inputSize.Height, 0, inputSize.Width));
            if (null != detectedContour && detectedContour.Length > 2)
            {
                matCombinedFrame.DrawContours(new Point[][] { detectedContour }, 0, Scalar.FromRgb(255, 255, 0), 3);
            }


            return(Unity.MatToTexture(matCombinedFrame));
        }
Пример #4
0
        // Use this for initialization
        void Start()
        {
            Mat mat = Unity.TextureToMat(this.texture);

            for (int yi = 0; yi < mat.Height; yi++)
            {
                for (int xi = 0; xi < mat.Width; xi++)
                {
                    Vec3b v = mat.At <Vec3b>(yi, xi);
                    Debug.Log(v[0]);
                    float gr = 0.2126f * v[2] + 0.7152f * v[1] + 0.0722f * v[0];
                    if (gr < 128)
                    {
                        gr = 0;
                    }
                    else
                    {
                        gr = 255;
                    }
                    v[0] = (byte)gr;
                    v[1] = (byte)gr;
                    v[2] = (byte)gr;
                    mat.Set <Vec3b>(yi, xi, v);
                }
            }
            Texture2D changedTex = Unity.MatToTexture(mat);

            GetComponent <RawImage>().texture = changedTex;
        }
Пример #5
0
        // Use this for initialization
        public void Process(string name)
        {
            var rawImage = gameObject.GetComponent <RawImage>();

            rawImage.texture = null;

            Texture2D inputTexture = (Texture2D)Resources.Load("DocumentScanner/" + name);

            scanner.Settings.NoiseReduction = 0.7;                                                      // real-world images are quite noisy, this value proved to be reasonable
            scanner.Settings.EdgesTight     = 0.9;                                                      // higher value cuts off "noise" as well, this time smaller and weaker edges
            scanner.Settings.ExpectedArea   = 0.2;                                                      // we expect document to be at least 20% of the total image area
            scanner.Settings.GrayMode       = PaperScanner.ScannerSettings.ColorMode.Grayscale;         // color -> grayscale conversion mode

            // process input with PaperScanner
            Mat result = null;

            scanner.Input = Unity.TextureToMat(inputTexture);            //★scanner.Successで使用する

            // should we fail, there is second try - HSV might help to detect paper by color difference
            if (!scanner.Success)
            {
                // this will drop current result and re-fetch it next time we query for 'Success' flag or actual data
                scanner.Settings.GrayMode = PaperScanner.ScannerSettings.ColorMode.HueGrayscale;
            }

            // now can combine Original/Scanner image
            //result = CombineMats(scanner.Input, scanner.Output, scanner.PaperShape);
            result = scanner.Output;

            // apply result or source (late for a failed scan)
            //rawImage.texture = Unity.MatToTexture(result);
            rawImage.texture =  Unity.MatToTexture(result);

            var transform = gameObject.GetComponent <RectTransform>();
        }
    public static Texture2D TexturaNoSave(Mat mat)
    {
        var textura = new Texture2D(mat.Width, mat.Height);

        textura.hideFlags = HideFlags.DontSaveInBuild | HideFlags.DontSaveInEditor;
        OCVUnity.MatToTexture(mat, textura);
        return(textura);
    }
    public Texture2D ExtraerSprite(Mat matOriginal, Contorno contorno, int ajustarRotacion)
    {
        var matTexturaAlfa = new Mat(contorno.BoundingRect.Height, contorno.BoundingRect.Width, MatType.CV_8UC1, new Scalar());

        Cv2.DrawContours(matTexturaAlfa, new[] { contorno.contorno }, 0, ProcesarRecuadros.ColEscalarBlanco,
                         -1, LineTypes.AntiAlias, null, 0, -contorno.BoundingRect.TopLeft);

        var matTexturaColor = new Mat(matOriginal, contorno.BoundingRect);

        if (equalizarHistograma || ajusteTresh > 0f || equalizarHistogramaDeSat)
        {
            var convertMat = new Mat();
            Cv2.CvtColor(matTexturaColor, convertMat, ColorConversionCodes.BGR2HSV);
            var splits = convertMat.Split();

            if (equalizarHistograma || equalizarHistogramaDeSat)
            {
                var clahe = Cv2.CreateCLAHE();
                if (equalizarHistograma)
                {
                    Cv2.FastNlMeansDenoising(splits[2], splits[2]);
                    clahe.Apply(splits[2], splits[2]);
                }
                if (equalizarHistogramaDeSat)
                {
                    Cv2.FastNlMeansDenoising(splits[1], splits[1]);
                    clahe.Apply(splits[1], splits[1]);
                }
            }
            // Cv2.Threshold(splits[2],splits[1],ajusteTresh,255,ThresholdTypes.TozeroInv);
            if (ajusteTresh > 0)
            {
                Cv2.Threshold(splits[1], splits[1], ajusteTresh, 255, ThresholdTypes.Tozero);
            }

            // clahe.Apply(splits[1],splits[1]);
            // clahe.Apply(splits[2],splits[2]);
            // Cv2. EqualizeHist(splits[2],splits[2]);
            Cv2.Merge(splits, matTexturaColor = convertMat);
            Cv2.CvtColor(matTexturaColor, matTexturaColor, ColorConversionCodes.HSV2BGR);
        }

        var textura = new Texture2D(matTexturaAlfa.Width, matTexturaAlfa.Height);

        textura.alphaIsTransparency = true;
        textura.hideFlags           = HideFlags.DontSaveInBuild | HideFlags.DontSaveInEditor;

        Cv2.Merge(matTexturaColor.Split().Concat(new[] { matTexturaAlfa }).ToArray(), matTexturaAlfa);

        if (ajustarRotacion > 0)
        {
            Cv2.Rotate(matTexturaAlfa, matTexturaAlfa, (RotateFlags)(ajustarRotacion - 1));
        }
        textura = OCVUnity.MatToTexture(matTexturaAlfa, textura);

        return(textura);
    }
        // Use this for initialization
        void Start()
        {
            Mat mat        = Unity.TextureToMat(this.texture);
            Mat changedMat = new Mat();

            Cv2.CvtColor(mat, changedMat, ColorConversionCodes.BGR2RGB);
            Texture2D changedTex = Unity.MatToTexture(changedMat);

            GetComponent <RawImage>().texture = changedTex;
        }
Пример #9
0
        // Use this for initialization
        void Start()
        {
            Mat mat        = Unity.TextureToMat(this.texture);
            Mat changedMat = new Mat();

            Cv2.MedianBlur(mat, changedMat, 3);
            Texture2D changedTex = Unity.MatToTexture(changedMat);

            GetComponent <RawImage>().texture = changedTex;
        }
Пример #10
0
        public override void OnInspectorGUI()
        {
            var coso = target as ProcesarRecuadros;

            EditorGUI.BeginChangeCheck();
            var texturaPrueba = EditorGUILayout.ObjectField("Textura De Prueba", coso.texturaPrueba, typeof(Texture2D), true) as Texture2D;
            var rawImgPrueba  = EditorGUILayout.ObjectField("Salida De Prueba", coso.rawImgPrueba, typeof(UnityEngine.UI.RawImage), true) as UnityEngine.UI.RawImage;

            if (EditorGUI.EndChangeCheck())
            {
                Undo.RecordObject(coso, "Coso");
                coso.texturaPrueba = texturaPrueba;
                coso.rawImgPrueba  = rawImgPrueba;
            }
            EditorGUI.BeginDisabledGroup(!coso.texturaPrueba || !coso.rawImgPrueba || coso.rawImgPrueba.gameObject.scene.rootCount == 0);
            if (GUILayout.Button("Probar"))
            {
                var matDebug = coso.ProcesarTextura(coso.texturaPrueba);
                var matdraw  = matDebug;
                // var matdraw = OCVUnity.TextureToMat(coso.texturaPrueba);
                var escala = matdraw.Width / (double)matDebug.Width;
                if (matdraw.Channels() == 1)
                {
                    Cv2.CvtColor(matdraw, matdraw, ColorConversionCodes.GRAY2BGR);
                }
                // Cv2.DrawContours(matdraw, coso.contornosRecuadrables.Select(c => c.padre.contorno.Select(p => p * escala)), -1, ProcesarRecuadros.ColEscalarVerde);
                // Cv2.DrawContours(matdraw, coso.contornosRecuadrables.Select(c => c.contorno.Select(p => p * escala)), -1, ProcesarRecuadros.ColEscalarAzul);
                foreach (var rec in coso.recuadros)
                {
                    // rec.DibujarDebug(matdraw, ColEscalarRojo, escala);
                    // var rect = Cv2.MinAreaRect(rec.contornoOriginal);
                    // Cv2.Polylines(matdraw, new[] { rect.Points().Select(e => new Point(e.X, e.Y)) }, true, ColEscalarRojo, 3);
                }

                var textura = new Texture2D(matdraw.Width, matdraw.Height);
                textura.hideFlags = HideFlags.DontSaveInBuild | HideFlags.DontSaveInEditor;
                OCVUnity.MatToTexture(matdraw, textura);
                coso.rawImgPrueba.texture = textura;
                coso.rawImgPrueba.SetNativeSize();
            }
            EditorGUI.EndDisabledGroup();
            DrawDefaultInspector();
            // EditorGUI.BeginDisabledGroup(coso.Recuadros == null);
            // if (verRecs = EditorGUILayout.Foldout(verRecs && coso.Recuadros != null, "Ver Recuadros"))
            // {
            // foreach (var rec in coso.Recuadros)
            // {
            // GUILayout.Label($"{rec.indiceContorno} - {rec.escalaContornos.X}x{rec.escalaContornos.Y}\n{rec.anchoSupuesto}x{rec.altoSupuesto}");
            // }
            // }
            // EditorGUI.EndDisabledGroup();
        }
Пример #11
0
    private void MatToImage(Mat mat, RawImage image, ref Texture2D texture)
    {
        if (mat == null)
        {
            return;
        }

        texture = CvUnity.MatToTexture(mat, texture);

        if (image.texture != texture)
        {
            image.texture = texture;
            image.GetComponent <AspectRatioFitter>().aspectRatio = (float)texture.width / texture.height;
        }
    }
Пример #12
0
        // Use this for initialization
        public void Process(string name)
        {
            var rawImage = gameObject.GetComponent <RawImage>();

            rawImage.texture = null;

            Texture2D inputTexture = (Texture2D)Resources.Load("DocumentScanner/" + name);

            // first of all, we set up scan parameters
            //
            // scanner.Settings has more values than we use
            // (like Settings.Decolorization that defines
            // whether b&w filter should be applied), but
            // default values are quite fine and some of
            // them are by default in "smart" mode that
            // uses heuristic to find best choice. so,
            // we change only those that matter for us
            scanner.Settings.NoiseReduction = 0.7;                                                      // real-world images are quite noisy, this value proved to be reasonable
            scanner.Settings.EdgesTight     = 0.9;                                                      // higher value cuts off "noise" as well, this time smaller and weaker edges
            scanner.Settings.ExpectedArea   = 0.2;                                                      // we expect document to be at least 20% of the total image area
            scanner.Settings.GrayMode       = PaperScanner.ScannerSettings.ColorMode.Grayscale;         // color -> grayscale conversion mode

            // process input with PaperScanner
            Mat result = null;

            scanner.Input = Unity.TextureToMat(inputTexture);            //★scanner.Successで使用する

            // should we fail, there is second try - HSV might help to detect paper by color difference
            if (!scanner.Success)
            {
                // this will drop current result and re-fetch it next time we query for 'Success' flag or actual data
                scanner.Settings.GrayMode = PaperScanner.ScannerSettings.ColorMode.HueGrayscale;
            }

            // now can combine Original/Scanner image
            result = CombineMats(scanner.Input, scanner.Output, scanner.PaperShape);
            //result = scanner.Output;

            // apply result or source (late for a failed scan)
            //rawImage.texture = Unity.MatToTexture(result);
            rawImage.texture =  Unity.MatToTexture(result);

            var transform = gameObject.GetComponent <RectTransform>();

            transform.sizeDelta = new Vector2(result.Width, result.Height);
        }
    public void Extraer()
    {
        if (recuadros == null && (recuadros = FindObjectOfType <EncontrarRecuadros>()) == null)
        {
            return;
        }
        if (recuadros.recuadros.Count == 0)
        {
            return;
        }

        ClearTexturas();
        Point[][]        contornos  = recuadros.contornos;
        HierarchyIndex[] jerarquias = recuadros.jerarquiaContornos;

        Recuadro       recuadro  = recuadros.recuadros[indiceRecuadroSel % recuadros.recuadros.Count];
        HierarchyIndex jerarquia = jerarquias[recuadro.indiceContorno];

        Mat matUmbral = recuadros.matUmbralEscalado.Clone();

        Cv2.Dilate(matUmbral, matUmbral, new Mat(), null, 3);
        Cv2.Erode(matUmbral, matUmbral, new Mat(), null, 2);
        Cv2.CvtColor(matUmbral, matUmbral, ColorConversionCodes.GRAY2BGR);
        List <int> hijosDirectos = new List <int>();
        int        hijo          = jerarquia.Child;

        while (hijo != -1)
        {
            if (contornos[hijo].Length > 1)
            {
                hijosDirectos.Add(hijo);
            }
            hijo = jerarquias[hijo].Next;
        }
        Debug.Log(hijosDirectos.Aggregate("", (s, e) => $"{e}({contornos[e].Length})-{s}"));
        foreach (var i in hijosDirectos)
        {
            Cv2.DrawContours(matUmbral, contornos, i, colRojo);
            Cv2.Polylines(matUmbral, new Point[][] { Cv2.ConvexHull(contornos[i]) }, true, colVerde);
        }

        var texturaObjetos = OCVUnity.MatToTexture(matUmbral, GenerarTextura(matUmbral.Width, matUmbral.Height));
    }
        public override void OnInspectorGUI()
        {
            var coso = target as ExtraerSprites;

            EditorGUI.BeginChangeCheck();
            var rawImgPrueba = EditorGUILayout.ObjectField("Salida De Prueba", coso.rawImgPrueba, typeof(UnityEngine.UI.RawImage), true) as UnityEngine.UI.RawImage;

            if (EditorGUI.EndChangeCheck())
            {
                Undo.RecordObject(coso, "Coso");
                coso.rawImgPrueba = rawImgPrueba;
            }
            EditorGUI.BeginDisabledGroup(!coso.rawImgPrueba || coso.rawImgPrueba.gameObject.scene.rootCount == 0);
            {
                mostrarAlfa = EditorGUILayout.Toggle("Mostrar Alfa", mostrarAlfa);
                if (GUILayout.Button("Probar"))
                {
                    coso.Extraer();
                    var matdraw = coso.matRecuadro;
                    if (matdraw != null)
                    {
                        if (!mostrarAlfa)
                        {
                            matdraw = coso.procesarRecuadros.Recuadros[0].matRecuadroNormalizado.Clone();
                            //matdraw.SetTo(new Scalar(0));
                            for (int i = 0; i < coso.contornos.Count; i++)
                            {
                                Cv2.DrawContours(matdraw, coso.contornos.Select(c => c.contorno), i, Scalar.RandomColor(), 7);
                                Cv2.Polylines(matdraw, new[] { coso.contornos[i].BoundingRect.ToArray() }, true, Scalar.RandomColor(), 5);
                            }
                        }

                        var textura = new Texture2D(matdraw.Width, matdraw.Height);
                        textura.hideFlags = HideFlags.DontSaveInBuild | HideFlags.DontSaveInEditor;
                        OCVUnity.MatToTexture(matdraw, textura);
                        coso.rawImgPrueba.texture = textura;
                        coso.rawImgPrueba.SetNativeSize();
                    }
                }
            }
            EditorGUI.EndDisabledGroup();
            DrawDefaultInspector();
        }
        // Use this for initialization
        void Start()
        {
            Mat mat = Unity.TextureToMat(this.texture);

            for (int yi = 0; yi < mat.Height; yi++)
            {
                for (int xi = 0; xi < mat.Width; xi++)
                {
                    Vec3b v = mat.At <Vec3b>(yi, xi);
                    v[0] = (byte)(ReduceColor(v[0]));
                    v[1] = (byte)(ReduceColor(v[1]));
                    v[2] = (byte)(ReduceColor(v[2]));
                    mat.Set <Vec3b>(yi, xi, v);
                }
            }
            Texture2D changedTex = Unity.MatToTexture(mat);

            GetComponent <RawImage>().texture = changedTex;
        }
        // Use this for initialization
        void Start()
        {
            Mat mat = Unity.TextureToMat(this.texture);

            for (int yi = 0; yi < 16; yi++)
            {
                for (int xi = 0; xi < 16; xi++)
                {
                    Vec3b max = new Vec3b();
                    for (int yj = 0; yj < 8; yj++)
                    {
                        for (int xj = 0; xj < 8; xj++)
                        {
                            Vec3b v = mat.At <Vec3b>(yi * 8 + yj, xi * 8 + xj);
                            if (max[0] < v[0])
                            {
                                max[0] = v[0];
                            }
                            if (max[1] < v[1])
                            {
                                max[1] = v[1];
                            }
                            if (max[2] < v[2])
                            {
                                max[2] = v[2];
                            }
                        }
                    }
                    for (int yj = 0; yj < 8; yj++)
                    {
                        for (int xj = 0; xj < 8; xj++)
                        {
                            mat.Set <Vec3b>(yi * 8 + yj, xi * 8 + xj, max);
                        }
                    }
                }
            }
            Texture2D changedTex = Unity.MatToTexture(mat);

            GetComponent <RawImage>().texture = changedTex;
        }
        // Use this for initialization
        void Start()
        {
            Mat mat         = Unity.TextureToMat(this.texture);
            Mat changedMat  = new Mat();
            Mat changedMat1 = new Mat();

            Cv2.CvtColor(mat, changedMat, ColorConversionCodes.BGR2HSV);
            for (int yi = 0; yi < mat.Height; yi++)
            {
                for (int xi = 0; xi < mat.Width; xi++)
                {
                    Vec3b v = changedMat.At <Vec3b>(yi, xi);
                    Debug.Log(v[0]);
                    v[0] = (byte)((v[0] - 180) % 360);
                    changedMat.Set <Vec3b>(yi, xi, v);
                }
            }
            Cv2.CvtColor(changedMat, changedMat1, ColorConversionCodes.HSV2BGR);
            Texture2D changedTex = Unity.MatToTexture(changedMat1);

            GetComponent <RawImage>().texture = changedTex;
        }
        // Use this for initialization
        void Start()
        {
            Mat mat = Unity.TextureToMat(this.texture);

            for (int yi = 0; yi < 16; yi++)
            {
                for (int xi = 0; xi < 16; xi++)
                {
                    Vector3 sum = new Vector3();
                    for (int yj = 0; yj < 8; yj++)
                    {
                        for (int xj = 0; xj < 8; xj++)
                        {
                            Vec3b v = mat.At <Vec3b>(yi * 8 + yj, xi * 8 + xj);
                            sum[0] += v[0];
                            sum[1] += v[1];
                            sum[2] += v[2];
                        }
                    }
                    Debug.Log(sum[0]);
                    Vec3b ave = new Vec3b();
                    ave[0] = (byte)(sum[0] / 64);
                    ave[1] = (byte)(sum[1] / 64);
                    ave[2] = (byte)(sum[2] / 64);

                    for (int yj = 0; yj < 8; yj++)
                    {
                        for (int xj = 0; xj < 8; xj++)
                        {
                            mat.Set <Vec3b>(yi * 8 + yj, xi * 8 + xj, ave);
                        }
                    }
                }
            }
            Texture2D changedTex = Unity.MatToTexture(mat);

            GetComponent <RawImage>().texture = changedTex;
        }
Пример #19
0
        //OpenCVを使用して、座標を求める。
        void OpenCVTexture(Texture2D texture)
        {
            Mat newMat = Unity.TextureToMat(texture);

            //画像をCv2.Equalsで変化があるかグローバルのoldMatと比較して検知しようとしたが、できなかった。

            //Convert image to grayscale
            Mat imgGray = new Mat();

            Cv2.CvtColor(newMat, imgGray, ColorConversionCodes.BGR2GRAY);

            //Debug.Log(Cv2.Equals(imgGray, imgGray));


            // Clean up image using Gaussian Blur
            Mat imgGrayBlur = new Mat();

            Cv2.GaussianBlur(imgGray, imgGrayBlur, new Size(5, 5), 0);

            //Extract edges
            Mat cannyEdges = new Mat();

            Cv2.Canny(imgGrayBlur, cannyEdges, 10.0, 70.0);

            //Do an invert binarize the image
            Mat mask = new Mat();

            Cv2.Threshold(cannyEdges, mask, 70.0, 255.0, ThresholdTypes.BinaryInv);

            // Extract Contours
            Point[][]        contours;                                                                                                    //特徴点が格納される変数。
            HierarchyIndex[] hierarchy;                                                                                                   //特徴点の階層が格納される。
            Cv2.FindContours(cannyEdges, out contours, out hierarchy, RetrievalModes.Tree, ContourApproximationModes.ApproxSimple, null); //特徴点を検出する。


            PointChangeNumSendUDP(contours);  //Pointが変化しなければ送信を実装しようとしている途中。
            //StartCoroutine(udpSendCoroutine(contours));


            //輪郭描画
            int width    = (int)transform.GetComponent <RectTransform>().sizeDelta.x;
            int height   = (int)transform.GetComponent <RectTransform>().sizeDelta.y;
            Mat Contours = new Mat(width, height, MatType.CV_8UC3, new Scalar(0, 0, 0)); //初期値として黒い画面を作成する。

            Cv2.DrawContours(Contours, contours, -1, new Scalar(0, 255, 0, 255), 1);     //MatにCountours(特徴点)を描画する。
            Texture2D changedTex = Unity.MatToTexture(Contours);                         //MatをTexture2Dへ変更

            GetComponent <RawImage>().texture = changedTex;                              //RaxImageにTexture2Dを書き込み。

            //MatをDisposeする。
            newMat.Dispose();
            imgGray.Dispose();
            imgGrayBlur.Dispose();
            cannyEdges.Dispose();
            mask.Dispose();
            Contours.Dispose();

            //TextureをDestryしないとメモリーリークを送りました。
            MonoBehaviour.Destroy(texture);
            if (changedTex != oldChangedTex)
            {
                MonoBehaviour.Destroy(oldChangedTex);
                oldChangedTex = changedTex;
            }
        }
        // Use this for initialization
        void Start()
        {
            Mat mat = Unity.TextureToMat(this.texture);

            float[] results = new float[256];
            float[,] grs = new float[mat.Height, mat.Width];
            for (int yi = 0; yi < mat.Height; yi++)
            {
                for (int xi = 0; xi < mat.Width; xi++)
                {
                    Vec3b v  = mat.At <Vec3b>(yi, xi);
                    float gr = 0.2126f * v[2] + 0.7152f * v[1] + 0.0722f * v[0];
                    grs[yi, xi] = gr;
                }
            }
            for (int thi = 1; thi < 255; thi++)
            {
                int   w0 = 0;
                int   w1 = 0;
                float M0 = 0;
                float M1 = 0;
                foreach (float gr in grs)
                {
                    if (gr < thi)
                    {
                        w0++;
                        M0 += gr;
                    }
                    else
                    {
                        w1++;
                        M1 += gr;
                    }
                }
                Debug.Log(w0 + w1);
                float tmp0 = w0 == 0 ? 0 : M0 / w0;
                float tmp1 = w1 == 0 ? 0 : M1 / w1;
                results[thi] = ((float)w0 / (mat.Height * mat.Width)) * ((float)w1 / (mat.Height * mat.Width)) * Mathf.Pow(tmp0 - tmp1, 2);
            }
            int z = 0;

            for (int i = 1; i < 255; i++)
            {
                if (results[i] > results[z])
                {
                    z = i;
                }
            }
            for (int yi = 0; yi < mat.Height; yi++)
            {
                for (int xi = 0; xi < mat.Width; xi++)
                {
                    if (grs[yi, xi] < z)
                    {
                        Vec3b v = new Vec3b();
                        v[0] = (byte)0; v[1] = (byte)0; v[2] = (byte)0;
                        mat.Set <Vec3b>(yi, xi, v);
                    }
                    else
                    {
                        Vec3b v = new Vec3b();
                        v[0] = (byte)255; v[1] = (byte)255; v[2] = (byte)255;
                        mat.Set <Vec3b>(yi, xi, v);
                    }
                }
            }
            Texture2D changedTex = Unity.MatToTexture(mat);

            GetComponent <RawImage>().texture = changedTex;
        }