private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            var patches = new List<Patch>();

            if (true)
            {
                //C:\Users\johnw_000\Images\bigger

                string imageFolder = @"C:\Users\johnw_000\Images\imageset1";
                var imfiles = Directory.GetFiles(imageFolder, "*.jpg");
                var images = imfiles.Select(filename => new Image(System.IO.Path.Combine(imageFolder, filename))).ToList();

                // Image im = images[0];
                foreach (var im in images) im.AddPatches(patches, 31, 31, 31, 31);
            }
            else
            {
                string imageFolder = @"C:\Users\johnw_000\Images\bigger";
                var imfiles = Directory.GetFiles(imageFolder, "*.jpg");
                var images = imfiles.Select(filename => new Image(System.IO.Path.Combine(imageFolder, filename))).ToList();

                // Image im = images[0];
                foreach (var im in images.Take(800)) im.AddPatches(patches, 5,5, 47, 47);

            }

            var learner = new TreeLearner();

            Tree tree = null;
            for (int z = 0; z < 4; z++)
            {
                // Learn initial tree
                tree = learner.Learn(10, patches);

                // Learning rotations
                Console.WriteLine("Learning rotations & translations");
                for (int iters = 0; iters < 2; iters++)
                {
                    LearnRotations(patches, tree);
                    Console.WriteLine("Non-empty leaves: " + tree.Nodes.Count(nd => nd.IsLeaf && nd.Count > 0));
                    //LearnTranslations(patches, tree);
                    //Console.WriteLine("Non-empty leaves: " + tree.Nodes.Count(nd => nd.IsLeaf && nd.Count > 0));
                }
                //Console.WriteLine("Learning translations");

                //Console.WriteLine("Non-empty leaves: " + tree.Nodes.Count(nd => nd.IsLeaf && nd.Count > 0));
            }

            var clusters = new List<Cluster>();
            foreach (var nd in tree.Nodes)
            {
                if (nd.IsLeaf)
                {
                    var cl = new Cluster
                    {
                        Count = nd.Count,
                        SamplePatches = new List<Patch>()
                    };

                    var leafPatches = patches.Where(p => p.NodeIndex == nd.Index).ToList();
                    for(int i=0;i<leafPatches.Count;i+=5) {
                        cl.SamplePatches.Add(leafPatches[i]);
                        if (cl.SamplePatches.Count > 20) break;
                    }
                    clusters.Add(cl);
                }
            }
            Console.WriteLine("Empty clusters: " + clusters.Count(c => c.Count == 0) + " out of " + clusters.Count);
            MyClusters.ItemsSource = clusters.OrderByDescending(cl => cl.Count);
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            var patches = new List <Patch>();

            if (true)
            {
                //C:\Users\johnw_000\Images\bigger

                string imageFolder = @"C:\Users\johnw_000\Images\imageset1";
                var    imfiles     = Directory.GetFiles(imageFolder, "*.jpg");
                var    images      = imfiles.Select(filename => new Image(System.IO.Path.Combine(imageFolder, filename))).ToList();

                // Image im = images[0];
                foreach (var im in images)
                {
                    im.AddPatches(patches, 31, 31, 31, 31);
                }
            }
            else
            {
                string imageFolder = @"C:\Users\johnw_000\Images\bigger";
                var    imfiles     = Directory.GetFiles(imageFolder, "*.jpg");
                var    images      = imfiles.Select(filename => new Image(System.IO.Path.Combine(imageFolder, filename))).ToList();

                // Image im = images[0];
                foreach (var im in images.Take(800))
                {
                    im.AddPatches(patches, 5, 5, 47, 47);
                }
            }

            var learner = new TreeLearner();

            Tree tree = null;

            for (int z = 0; z < 4; z++)
            {
                // Learn initial tree
                tree = learner.Learn(10, patches);

                // Learning rotations
                Console.WriteLine("Learning rotations & translations");
                for (int iters = 0; iters < 2; iters++)
                {
                    LearnRotations(patches, tree);
                    Console.WriteLine("Non-empty leaves: " + tree.Nodes.Count(nd => nd.IsLeaf && nd.Count > 0));
                    //LearnTranslations(patches, tree);
                    //Console.WriteLine("Non-empty leaves: " + tree.Nodes.Count(nd => nd.IsLeaf && nd.Count > 0));
                }
                //Console.WriteLine("Learning translations");

                //Console.WriteLine("Non-empty leaves: " + tree.Nodes.Count(nd => nd.IsLeaf && nd.Count > 0));
            }

            var clusters = new List <Cluster>();

            foreach (var nd in tree.Nodes)
            {
                if (nd.IsLeaf)
                {
                    var cl = new Cluster
                    {
                        Count         = nd.Count,
                        SamplePatches = new List <Patch>()
                    };

                    var leafPatches = patches.Where(p => p.NodeIndex == nd.Index).ToList();
                    for (int i = 0; i < leafPatches.Count; i += 5)
                    {
                        cl.SamplePatches.Add(leafPatches[i]);
                        if (cl.SamplePatches.Count > 20)
                        {
                            break;
                        }
                    }
                    clusters.Add(cl);
                }
            }
            Console.WriteLine("Empty clusters: " + clusters.Count(c => c.Count == 0) + " out of " + clusters.Count);
            MyClusters.ItemsSource = clusters.OrderByDescending(cl => cl.Count);
        }