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 ClusteringAI(PixelData pixels, int maxRank = 10)
        {
            DateTime start = DateTime.Now;

            Debug.WriteLine("Initialize Start");
            Hierarchies.Add(GridHierarchy.CreateFromPixels(pixels));
            Debug.WriteLine($"Initialize Over,TimeConsuming:{DateTime.Now - start}");
            Debug.WriteLine($"Generation Start");
            for (var i = 0; i <= maxRank - 1; i++)
            {
                start = DateTime.Now;
                Hierarchies.Add(Hierarchies.Last().Generate());
                Debug.WriteLine($"Total:{maxRank},Current:{i + 1},Time Consuming:{DateTime.Now - start},Hierarchy[{Hierarchies.Last()}]");
            }
            Debug.WriteLine(pixels.Colors.Length);

            // Paint lines
            Lines.AddRange(GenerateLines(Hierarchies[9].Clusters.ToList()));

            // Paint pixels
            foreach (var item in Hierarchies[8].Clusters)
            {
                BuildResult2(Lines, item);
            }
        }
Exemplo n.º 3
0
        public ClusteringAI(PixelData pixels, int maxRank = 10)
        {
            DateTime start = DateTime.Now;

            Debug.WriteLine("Initialize Start");
            Hierarchies.Add(GridHierarchy.CreateFromPixels(pixels));
            Debug.WriteLine($"Initialize Over,TimeConsuming:{DateTime.Now - start}");
            Debug.WriteLine($"Generation Start");
            for (var i = 0; i <= maxRank - 1; i++)
            {
                start = DateTime.Now;
                Hierarchies.Add(Hierarchies.Last().Generate());
                Debug.WriteLine($"Total:{maxRank},Current:{i + 1},Time Consuming:{DateTime.Now - start},Hierarchy[{Hierarchies.Last()}]");
            }
            Debug.WriteLine(pixels.Colors.Length);

            int mid = 4;

            for (var i = maxRank - 1; i >= mid; i += -2)
            {
                //Lines.AddRange(GenerateOutlines(Hierarchies[i]));
                Lines.AddRange(GenerateLines(Hierarchies[i]));
            }

            //var index = 8;
            //Lines.AddRange(GenerateLines(Hierarchies[index]));
            //Lines.AddRange(DeepGenerateLines(Hierarchies[mid].Clusters, Hierarchies[mid].Rank));
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
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);
                }
            }
            return(result);
        }
Exemplo n.º 6
0
        public ClusteringAI(PixelData pixels, int maxRank = 10)
        {
            DateTime start = DateTime.Now;

            Debug.WriteLine("Initialize Start");
            Hierarchies.Add(GridHierarchy.CreateFromPixels(pixels));
            Debug.WriteLine($"Initialize Over,TimeConsuming:{DateTime.Now - start}");
            Debug.WriteLine($"Generation Start");
            for (var i = 0; i <= maxRank - 1; i++)
            {
                start = DateTime.Now;
                Hierarchies.Add(Hierarchies.Last().Generate());
                Debug.WriteLine($"Total:{maxRank},Current:{i + 1},Time Consuming:{DateTime.Now - start},Hierarchy[{Hierarchies.Last().ToString()}]");
            }
            Debug.WriteLine(pixels.Colors.Length);
            for (var i = maxRank - 1; i >= 0; i += -1)
            {
                Lines.AddRange(GenerateLines(Hierarchies[i]));
            }
        }
Exemplo n.º 7
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);
            //    }
            //}

            Parallel.ForEach(Clusters, cluster =>
            {
                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
            Parallel.ForEach(result.Clusters, cluster =>
            {
                cluster.Position = cluster.GetAveragePosition();
                cluster.Color    = cluster.GetAverageColor();
            });

            // Locate to cell
            Parallel.ForEach(result.Clusters, cluster =>
            {
                Vector2 p = cluster.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(cluster);
            });

            // Calculate edge vector
            Parallel.ForEach(result.Clusters, cluster =>
            {
                cluster.EdgeVector = result.GetNeighboursEdgeVector(cluster);;
            });

            return(result);
        }