Exemplo n.º 1
0
        public static GridHierarchy CreateFromPixels(PixelData pixels)
        {
            GridHierarchy result = new GridHierarchy(pixels.Width, pixels.Height, 1);

            for (var i = 0; i <= pixels.Width - 1; i++)
            {
                for (var j = 0; j <= pixels.Height - 1; j++)
                {
                    Cluster cluster = new Cluster()
                    {
                        Position = new Vector2(i, j),
                        Color    = pixels.Colors[i, j]
                    };
                    result.Grid[i, j].Add(cluster);
                    result.Clusters.Add(cluster);
                }
            }

            foreach (var cluster in result.Clusters)
            {
                var neighbours     = result.GetNeighbours(cluster);
                var colorDirection = VectorHelper.GetAveragePosition(neighbours.Select(v => v.ColorVector - cluster.ColorVector));
                cluster.ColorDirection = colorDirection;
            }

            return(result);
        }
Exemplo n.º 2
0
        public override IHierarchy Generate()
        {
            float         rate    = 2.0F;
            float         newSize = this.Size * rate;
            GridHierarchy result  = new GridHierarchy(System.Convert.ToInt32(Math.Ceiling(this.Width / (double)rate) + 1), System.Convert.ToInt32(Math.Ceiling(this.Height / (double)rate) + 1), newSize)
            {
                Rank = this.Rank + 1
            };

            // Combine to a new cluster
            foreach (var cluster in Clusters)
            {
                Cluster similar = cluster.GetMostSimilar(GetNeighbours(cluster)).FirstOrDefault();
                if (similar != null)
                {
                    if (cluster.Parent == null && similar.Parent == null)
                    {
                        result.Clusters.Add(Cluster.Combine(cluster, similar));
                    }
                    else
                    {
                        Cluster.Combine(cluster, similar);
                    }
                }
                else
                {
                    result.Clusters.Add(cluster);
                }
            }
            // Initialize properties
            foreach (var cluster in result.Clusters)
            {
                cluster.Position = cluster.GetAveragePosition();
                cluster.Color    = cluster.GetAverageColor();
                //cluster.ColorDirection = cluster.GetAverageColorDirection();
            }
            // Locate to cell
            foreach (var SubCluster in result.Clusters)
            {
                Vector2 p = SubCluster.Position;
                int     x = System.Convert.ToInt32(p.X / (double)result.Size);
                int     y = System.Convert.ToInt32(p.Y / (double)result.Size);
                result.Grid[x, y].Add(SubCluster);
            }

            foreach (var cluster in result.Clusters)
            {
                var neighbours     = result.GetNeighbours(cluster);
                var colorDirection = VectorHelper.GetAveragePosition(neighbours.Select(v => v.ColorVector - cluster.ColorVector));
                cluster.ColorDirection = colorDirection;
            }

            return(result);
        }