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);
    }
Ejemplo n.º 3
0
    private void ChangeTarget(int targetIndex)
    {
        maxRate = 0;

        currentTargetIndex = targetIndex % targetList.Length;

        if (targetImage.texture != null)
        {
            //Resources.UnloadAsset(targetImage.texture);
        }

        var data    = File.ReadAllBytes(targetList[currentTargetIndex]);
        var texture = new Texture2D(4, 4);

        texture.LoadImage(data);

        targetImage.texture = texture;
        targetImage.GetComponent <AspectRatioFitter>().aspectRatio = (float)targetImage.texture.width / targetImage.texture.height;

        if (targetMat != null)
        {
            targetMat.Dispose();
        }
        targetMat = new Mat();
        Mat targetColorMat = CvUnity.TextureToMat((Texture2D)targetImage.texture);

        Cv2.CvtColor(targetColorMat, targetMat, ColorConversionCodes.BGRA2GRAY);

        if (targetMat.Width > 512)
        {
            var size = new Size(512, (double)targetMat.Height / targetMat.Width * 512);
            targetMat = targetMat.Resize(size);
        }

        targetPixelCount = 0;

        for (int y = 0; y < targetMat.Height; ++y)
        {
            for (int x = 0; x < targetMat.Width; ++x)
            {
                int index = targetMat.Width * y + x;

                unsafe
                {
                    byte val = *(targetMat.DataPointer + index);
                    if (val == 0)
                    {
                        targetPixelCount++;
                    }
                }
            }
        }

        UpdateLine(highLine, 0);
    }
Ejemplo n.º 4
0
    private void UpdateBackground()
    {
        Mat cameraMat = CvUnity.TextureToMat(webCamTexture, webCamParams);

        if (cameraMat.Width > 512)
        {
            var size = new Size(512, (double)cameraMat.Height / cameraMat.Width * 512);
            cameraMat = cameraMat.Resize(size);
        }

        backgroundMat = cameraMat.Clone();
    }
Ejemplo n.º 5
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();
        }
Ejemplo n.º 6
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;
        }
    }
    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();
        }
Ejemplo n.º 9
0
    private void UpdateImages()
    {
        if (backgroundMat == null)
        {
            return;
        }

        Mat cameraMat = CvUnity.TextureToMat(webCamTexture, webCamParams);

        if (cameraMat.Width > 512)
        {
            var size = new Size(512, (double)cameraMat.Height / cameraMat.Width * 512);
            cameraMat = cameraMat.Resize(size);
        }

        var width      = cameraMat.Width - cameraOffset.left - cameraOffset.right;
        var height     = cameraMat.Height - cameraOffset.top - cameraOffset.bottom;
        var posX       = cameraOffset.left;
        var posY       = cameraOffset.top;
        var cameraRect = new OpenCvSharp.Rect(posX, posY, width, height);

        cameraMat = cameraMat.Clone(cameraRect);
        Mat trimBgMat = backgroundMat.Clone(cameraRect);
        Mat diffBgMat = cameraMat.Clone();

        Cv2.Absdiff(diffBgMat, trimBgMat, diffBgMat);


        Mat grayscaleMat = new Mat();

        Cv2.CvtColor(diffBgMat, grayscaleMat, ColorConversionCodes.BGR2GRAY);

        //Mat blurMat = new Mat();
        //Cv2.GaussianBlur(grayscaleMat, blurMat, new Size(5, 5), 0);

        Mat maskMat = new Mat();

        Cv2.Threshold(grayscaleMat, maskMat, maskThreshold, maskMaxVal, ThresholdTypes.BinaryInv);
        //Cv2.Threshold(blurMat, maskMat, maskThreshold, maskMaxVal, ThresholdTypes.BinaryInv);

        Mat dilateMat = maskMat.Dilate(new Mat(), null, smoothIterationCount);
        Mat erodeMat  = dilateMat.Erode(new Mat(), null, smoothIterationCount);

        Mat outputMat = erodeMat.Clone();

        float targetAspectRatio = (float)targetMat.Width / targetMat.Height;
        float outputAspectRatio = (float)erodeMat.Width / erodeMat.Height;

        if (targetMat.Height != outputMat.Height)
        {
            var rate = (double)targetMat.Height / outputMat.Height;
            var size = new Size(outputMat.Width * rate, outputMat.Height * rate);
            outputMat = outputMat.Resize(size);
        }

        var rect      = new OpenCvSharp.Rect((outputMat.Width - targetMat.Width) / 2, 0, targetMat.Width, targetMat.Height);
        Mat resizeMat = outputMat.Clone(rect);
        Mat diffMat   = new Mat();

        Cv2.Absdiff(targetMat, resizeMat, diffMat);

        Mat resultColorMat = new Mat(resizeMat.Rows, resizeMat.Cols, MatType.CV_8UC4);

        CountDiff(resizeMat, targetMat, resultColorMat);

        MatToImage(cameraMat, cameraImage, ref cameraTexture);
        //MatToImage(backgroundMat, backgroundImage, ref backgroundTexture);
        //MatToImage(resizeMat, outputImage, ref outputTexture);
        //MatToImage(diffMat, diffImage, ref diffTexture);
        MatToImage(resultColorMat, diffImage, ref diffTexture);
    }
        // 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;
        }
Ejemplo n.º 11
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;
            }
        }