Esempio n. 1
0
        public Matrix MergeRegions(Matrix regionMatrix, SetList equivalentRegions, out Vector newRegionSets)
        {
            //merge equivalent regions using union-find algorithm
            UnionFind unionFind = new UnionFind(equivalentRegions);

            //normalize the region-numbers (assign sequential region-numbers starting from 1)
            List <int> regionNumbers = new List <int>();

            regionNumbers.AddRange(unionFind.Roots.Values);

            newRegionSets = new Vector();
            if (regionNumbers.Count > 0)
            {
                int index = 1, max = regionNumbers.Max();
                while (index <= max && regionNumbers.Count > 0)
                {
                    int min = regionNumbers.Min();
                    newRegionSets.Add(min, index);
                    index += 1;

                    while (regionNumbers.Remove(min))
                    {
                        ;
                    }
                }
            }

            //assign the new region-numbers to region-matrix
            for (int y = 0; y < regionMatrix.Hauteur; y++)
            {
                for (int x = 0; x < regionMatrix.Largeur; x++)
                {
                    if (regionMatrix.Values[x, y] > 0)
                    {
                        regionMatrix.Values[x, y] = newRegionSets[unionFind.Roots[regionMatrix.Values[x, y]]];

                        listRegion.Add(regionMatrix.Values[x, y]);
                    }
                }
            }

            //do logging
            // AddToLog("-----union-find-result:" + Environment.NewLine + unionFind.ToString());
            //  AddToLog("-----new-region-sets:" + Environment.NewLine + newRegionSets.ToString());

            return(regionMatrix);
        }
Esempio n. 2
0
        public Boolean MarqueRegionAller(out SetList equivalentRegions)
        {
            string str            = "";
            int    RegionCourante = 1;

            equivalentRegions = null;

            try
            {
                rect      = new Dictionary <string, Structure>();
                FinalRect = new Dictionary <string, Structure>();

                equivalentRegions = new SetList();

                //equivalentRegions = new List<int>();
                structures = new List <Structure>();
                listRegion = new ArrayList();

                for (int y = 0; y < matrix.Hauteur; y++)
                {
                    for (int x = 0; x < matrix.Largeur; x++)
                    {
                        if (y == 128 && x == 313)
                        {
                            str = "";
                        }
                        // Si on est sur une forme
                        if (matrix.Values[x, y] == 0)
                        {
                            List <Cellule> voisinage = null;
                            voisinage = matrix.GetVoisinageAller(x, y);

                            //int matchNombre = 0;

                            //foreach (Cellule elem in voisinage)
                            //{
                            //    if (elem.Value > 0)
                            //        matchNombre++;
                            //}


                            int matchNombre = voisinage.Count(cellule => cellule.Value == 0);

                            // Si pas de pixels labelises
                            if (matchNombre == 0)
                            {
                                regionMatrix.Values[x, y] = RegionCourante;

                                equivalentRegions.Add(RegionCourante, new Set()
                                {
                                    RegionCourante
                                });
                                //equivalentRegions.Add(RegionCourante);
                                //structures.Add(new Structure(x, y, RegionCourante));
                                rect.Add(RegionCourante.ToString(), new Structure(x, y));

                                //equivalentRegions.Add(RegionCourante, new Set() { RegionCourante });
                                RegionCourante++;
                            }
                            else if (matchNombre == 1)
                            {
                                Cellule oneCell = voisinage.First(cellule => cellule.Value == 0);
                                regionMatrix.Values[x, y] = regionMatrix.Values[oneCell.X, oneCell.Y];;
                            }
                            else if (matchNombre > 1)
                            {
                                List <int> distinctRegions = voisinage.Select(cellule => regionMatrix.Values[cellule.X, cellule.Y]).Distinct().ToList();
                                while (distinctRegions.Remove(0))
                                {
                                    ;
                                }

                                if (distinctRegions.Count == 1) //step5
                                {
                                    regionMatrix.Values[x, y] = distinctRegions[0];
                                }
                                else if (distinctRegions.Count > 1)
                                {
                                    // Equivalence tout de suite

                                    int firstRegion = distinctRegions[0];
                                    regionMatrix.Values[x, y] = firstRegion;

                                    foreach (int region in distinctRegions)
                                    {
                                        if (!equivalentRegions[firstRegion].Contains(region))
                                        {
                                            equivalentRegions[firstRegion].Add(region);
                                        }
                                    }

                                    //foreach (Cellule voisi in voisinage)
                                    //{
                                    //    if (voisi.Value == 0)
                                    //        regionMatrix.Values[voisi.X, voisi.Y] = firstRegion;

                                    //}
                                }
                            }
                        }
                    }
                }

                return(true);
            }
            catch (Exception err)
            {
                Console.WriteLine(err.Message);
                return(false);
            }
            finally
            {
            }
        }