//Devuelve una nueva segmentacion que se forma al unir "clusCount" pares de clusters en la segmentacion actual.
        public Segmentation Join(int clusCount, SuperPixelGraph spg)
        {
            int[] newLabels = new int[Labels.Length];
            for (int i = 0; i < Labels.Length; i++)
            {
                newLabels[i] = Labels[i];
            }
            Random r = new Random();

            for (int i = 0; i < clusCount; i++)
            {
                int pos  = r.Next(0, Labels.Length);
                int reg  = r.Next(0, spg.AdjList[pos].Count);
                int sp   = spg.AdjList[pos][reg];
                int newL = newLabels[sp];

                int value = newLabels[pos];
                for (int j = 0; j < newLabels.Length; j++)
                {
                    if (newLabels[j] == value)
                    {
                        newLabels[j] = newL;
                    }
                }
            }
            Segmentation result = new Segmentation(newLabels);

            return(result);
        }
        //Aqui estoy asumiendo q ya la segmentacion inicial tiene k clusters, con k el valor q quiero q tengan todas.
        public Segmentation GetNeighborKFIJO(SuperPixelGraph spg)
        {
            int[] newLabels = new int[Labels.Length];
            for (int i = 0; i < Labels.Length; i++)
            {
                newLabels[i] = Labels[i];
            }
            Random r = new Random();

            //posicion del objeto q voy a cambiar de region
            int pos = 0;

            do
            {
                pos = r.Next(0, Labels.Length);
            }while (this.Clusters[Labels[pos]].Count <= 1);

            //numero de la nueva region a seleccionar entre las posibles segun el grafo de la imagen
            int reg = r.Next(0, spg.AdjList[pos].Count);

            //este es el superpixel con el q me voy a pegar en el mismo cluster.
            int sp = spg.AdjList[pos][reg];

            //Esta es la etiqueta del cluster al q voy a ser asignado.
            int newL = Labels[sp];


            //Tiene el problema de q si es la misma etiqueta...estoy generando la misma segmentacion de nuevo!!
            newLabels[pos] = newL;
            Segmentation result = new Segmentation(newLabels);

            return(result);
        }
        public Segmentation GetNeighbor(SuperPixelGraph spg)
        {
            int[] newLabels = new int[Labels.Length];
            for (int i = 0; i < Labels.Length; i++)
            {
                newLabels[i] = Labels[i];
            }
            Random r = new Random();

            //posicion del objeto q voy a cambiar de region
            int pos = r.Next(0, Labels.Length);

            //numero de la nueva region a seleccionar entre las posibles segun el grafo de la imagen
            int reg = r.Next(0, spg.AdjList[pos].Count);

            //este es el superpixel con el q me voy a pegar en el mismo cluster.
            int sp = spg.AdjList[pos][reg];

            //Esta es la etiqueta del cluster al q voy a ser asignado.
            int newL = Labels[sp];

            //Si la nueva posicion es la misma, cojo esta posibilidad para poner el elemento en un nuevo cluster donde esta el solo.
            if (newLabels[pos] == newL)
            {
                newLabels[pos] = this.maxLabel + 1;
            }
            else
            {
                newLabels[pos] = newL;
            }
            Segmentation result = new Segmentation(newLabels);

            return(result);
        }
Esempio n. 4
0
        //Devuelve la lista de superpixels como elementos a partir del diccionario de los superpixels
        private List <Element> GetElements(Dictionary <int, List <Pixel> > spDic, SuperPixelGraph graph)
        {
            //cp--cantidad de pixels, r--R, g--G, b--B (RGB values), cgx--coordenada x del centro de gravedad, cgy--coordenada y del centro de gravedad, ra--radio, definido como la distancia del pixel mas alejado al centro de gravedad.
            List <Element> elements = new List <Element>();

            foreach (int spID in spDic.Keys)
            {
                List <object> realvalues = new List <object>();
                int           cp = 0, r = 0, g = 0, b = 0, cgx = 0, cgy = 0, ra = 0;

                for (int j = 0; j < spDic[spID].Count; j++)
                {
                    cp++;
                    r   += spDic[spID][j].R;
                    g   += spDic[spID][j].G;
                    b   += spDic[spID][j].B;
                    cgx += spDic[spID][j].j;
                    cgy += spDic[spID][j].i;
                }
                r   /= spDic[spID].Count;
                g   /= spDic[spID].Count;
                b   /= spDic[spID].Count;
                cgx /= spDic[spID].Count;
                cgy /= spDic[spID].Count;

                int    aux = 0, auxI = 0, auxJ = 0;
                double r_error = 0, g_error = 0, b_error = 0;
                int    perim = 0;
                for (int j = 0; j < spDic[spID].Count; j++)
                {
                    auxI = spDic[spID][j].i;
                    auxJ = spDic[spID][j].j;
                    aux  = (int)Math.Sqrt(((auxI - cgy) * (auxI - cgy)) + ((auxJ - cgx) * (auxJ - cgx)));
                    if (aux > ra)
                    {
                        ra = aux;
                    }

                    r_error += Math.Abs(spDic[spID][j].R - r);
                    g_error += Math.Abs(spDic[spID][j].G - g);
                    b_error += Math.Abs(spDic[spID][j].B - b);
                }
                r_error /= spDic[spID].Count;
                g_error /= spDic[spID].Count;
                b_error /= spDic[spID].Count;

                perim = graph.PerimList[spID];

                realvalues.Add(cp); realvalues.Add(r); realvalues.Add(g); realvalues.Add(b); realvalues.Add(r_error); realvalues.Add(g_error); realvalues.Add(b_error); realvalues.Add(cgx); realvalues.Add(cgy); realvalues.Add(ra); realvalues.Add(perim);
                Element e = new Element(realvalues);
                e.Name  = "SP-" + spID;
                e.Index = spID;
                elements.Add(e);
            }
            return(elements);
        }
Esempio n. 5
0
        public Set Load()
        {
            Set    set = null;
            Bitmap bmp = (Bitmap)Bitmap.FromFile(SourceFilePath);
            //Ya tengo los atributos
            Attributes attributes = new Attributes(new List <Attribute>(new Attribute[] { new Attribute("PixelsCount", null), new Attribute("R_Ave", null), new Attribute("G_Ave", null), new Attribute("B_Ave", null), new Attribute("R_error", null), new Attribute("G_error", null), new Attribute("B_error", null), new Attribute("X_GravCenter", null), new Attribute("Y_GravCenter", null), new Attribute("Radius", null), new Attribute("Perimeter", null) }));
            //Ya tengo el nombre
            string relationName = Path.GetFileNameWithoutExtension(SourceFilePath);
            //Faltan los elementos...para esto tengo q cargar el archivo q contiene la informacion de los superpixels.

            string directoryName  = Path.GetDirectoryName(SourceFilePath);
            string superPixelPath = directoryName + "//SP_supPix" + relationName + ".mat.txt";

            int[,] sp = LoadSupPixMatrix(superPixelPath);
            Dictionary <int, List <Pixel> > spDic = GetSPDictionary(bmp, sp);

            SuperPixelGraph graph = new SuperPixelGraph(sp, spDic.Count);

            //Ya tengo los elementos
            List <Element> elements = GetElements(spDic, graph);

            set                  = new Set(relationName, elements, attributes);
            set.Image            = bmp;
            set.FolderName       = directoryName;
            set.SPGraph          = graph;
            set.SuperPixelMatrix = sp;
            set.Segmentations    = LoadSegmentations(SourceFilePath, sp, elements.Count);
            set.KValue           = this.kvalue;
            set.GroundTruths     = LoadGTs(SourceFilePath);


            //PRUEBAAAA...BORRAR
            //int[] l0 = new int[set.Segmentations[0].Labels.Length];
            //int[] l1 = new int[set.Segmentations[0].Labels.Length];
            //for (int i = 0; i < l0.Length; i++)
            //    l0[i] = i;

            //Segmentation seg0 = new Segmentation(l0);
            //Segmentation seg1 = new Segmentation(l1);
            //set.Segmentations.Add(seg0);
            //set.Segmentations.Add(seg1);
            ///

            return(set);
        }
        //Devuelve una nueva segmentacion que se forma al separar "clusCount" clusters (en dos nuevos clusters) en la segmentacion actual.
        public Segmentation Split(int clusCount, SuperPixelGraph spg)
        {
            int[] newLabels = new int[Labels.Length];
            for (int i = 0; i < Labels.Length; i++)
            {
                newLabels[i] = Labels[i];
            }
            Random r    = new Random();
            int    last = this.maxLabel;

            for (int i = 0; i < clusCount; i++)
            {
                int pos = r.Next(0, Labels.Length);
                newLabels[pos] = last + 1;
                last++;
            }
            Segmentation result = new Segmentation(newLabels);

            return(result);
        }