private static void PrintGMM(string name, GaussianMixtureColorModel gmm)
 {
     Console.WriteLine("{0}:", name);
     for (int i = 0; i < gmm.Components.Count; ++i)
     {
         Console.WriteLine("WEIGHTS[{0}]={1}", i, gmm.Weights[i]);
         Console.WriteLine("MEANS[{0}]={1}", i, gmm.Components[i].GetMean());
         Console.WriteLine("COVARIANCES[{0}]=\n{1}", i, gmm.Components[i].GetVariance());
         Console.WriteLine();
     }
     Console.WriteLine();
 }
        private void OnLearnColorModelButtonClick(object sender, RoutedEventArgs e)
        {
            if (this.backgroundImagesListBox.SelectedIndex != -1)
            {
                ImageInfo currentImageInfo = this.imageInfos[this.backgroundImagesListBox.SelectedIndex];
                currentImageInfo.ColorModelMask = this.colorMaskEditor.GetMask(currentImageInfo.Image.PixelWidth, currentImageInfo.Image.PixelHeight);
            }

            List <Color> objectColors     = new List <Color>();
            List <Color> backgroundColors = new List <Color>();

            for (int i = 0; i < this.imageInfos.Count; ++i)
            {
                if (this.imageInfos[i].ColorModelMask == null)
                {
                    continue;
                }

                Image2D <Color> image = ImageHelper.BitmapSourceToImage2D(this.imageInfos[i].Image);
                ExtractObjectBackgroundColorsByMask(
                    image,
                    this.imageInfos[i].ColorModelMask,
                    objectColors,
                    backgroundColors);

                Image2D.SaveToFile(image, string.Format("image_{0}.png", i));
                Image2D.SaveToFile(this.imageInfos[i].ColorModelMask, string.Format("mask_{0}.png", i));
            }

            if (objectColors.Count == 0)
            {
                MessageBox.Show("No object pixels specified.");
                return;
            }

            if (backgroundColors.Count == 0)
            {
                MessageBox.Show("No background pixels specified.");
                return;
            }

            Helper.Subsample(objectColors, this.algorithmProperties.MaxPixelsToLearnFrom);
            Helper.Subsample(backgroundColors, this.algorithmProperties.MaxPixelsToLearnFrom);

            try
            {
                GaussianMixtureColorModel objectModel = GaussianMixtureColorModel.Fit(
                    objectColors.Take(this.algorithmProperties.MaxPixelsToLearnFrom),
                    this.algorithmProperties.MixtureComponentCount,
                    this.algorithmProperties.StopTolerance);
                GaussianMixtureColorModel backgroundModel = GaussianMixtureColorModel.Fit(
                    backgroundColors.Take(this.algorithmProperties.MaxPixelsToLearnFrom),
                    this.algorithmProperties.MixtureComponentCount,
                    this.algorithmProperties.StopTolerance);
                this.colorModels = new ObjectBackgroundColorModels(objectModel, backgroundModel);

                this.UpdateControlsAccordingToCurrentState();
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message, "Error!", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }