private static Bitmap FusionProcessing(Bitmap image,
                                               IEnumerable <AlgorithmInfo> algorithms,
                                               IEnumerable <EvaluationResult> algoritmsScores)
        {
            double totalScore = algoritmsScores.Sum(x => x.AlgorithScore);
            var    results    = new Dictionary <double, byte[, ]>();

            foreach (var score in algoritmsScores)
            {
                AlgorithmInfo algorithmInfo = algorithms.Where(x => x.CustomName == score.AlgorithCustomName).Single();
                var           algorithm     = AppFacade.DI.Container.Resolve <IAlgorithm>(algorithmInfo.AlgorithName);
                algorithm.SetParameters(algorithmInfo.Parameters);
                algorithm.Input = new AlgorithmInput(image);
                UnmanagedImage result = algorithm.ProcessData().Image;
                results.Add(score.AlgorithScore / totalScore, result.GetPixels());
            }

            UnmanagedImage fusionImage =
                UnmanagedImage.FromManagedImage(new Bitmap(image.Width, image.Height, image.PixelFormat));

            var fusionBytes = new byte[image.Width, image.Height];

            for (int i = 0; i < image.Width; i++)
            {
                for (int j = 0; j < image.Height; j++)
                {
                    double level = results.Sum(keyValuePair => (keyValuePair.Key * keyValuePair.Value[i, j]));
                    fusionBytes[i, j] = (byte)level;
                }
            }

            fusionImage.SetPixels(fusionBytes);
            return(fusionImage.ToManagedImage());
        }
        public Bitmap ProcessImage(Bitmap image, ObserverData observerData, ProcessingMethod processingMethod)
        {
            if (processingMethod == ProcessingMethod.UseAlgorithmWithHighestScore)
            {
                string bestAlgorithm =
                    observerData.EvaluationResults.OrderByDescending(x => x.AlgorithScore).First().AlgorithCustomName;
                AlgorithmInfo algorithmInfo = this.Algorithms.Where(x => x.CustomName == bestAlgorithm).Single();
                var           algorithm     = AppFacade.DI.Container.Resolve <IAlgorithm>(algorithmInfo.AlgorithName);
                algorithm.SetParameters(algorithmInfo.Parameters);
                return(SimpleProcessing(image, algorithm));
            }
            if (processingMethod == ProcessingMethod.AlgorithmsFusion)
            {
                return(FusionProcessing(image, this.Algorithms, observerData.EvaluationResults));
            }

            throw new NotSupportedException();
        }
        private void OnAddAlgoButtonClick(object sender, EventArgs e)
        {
            var algo = sourceAlgos.SelectedItem as string;
            if (algo == null)
            {
                return;
            }

            string customeName = string.Empty;

            using (var nameWindow = new NameWindow())
            {
                nameWindow.ObjectName = algo;
                do
                {
                    nameWindow.ShowDialog();
                    if (this.SystemInstance.Algorithms.FirstOrDefault(
                        x => x.CustomName == nameWindow.ObjectName) == null)
                    {
                        customeName = nameWindow.ObjectName;
                        break;
                    }

                    if (nameWindow.DialogResult == DialogResult.OK)
                    {
                        MessageBox.Show(Resources.AlgoNameNotUnique, string.Empty, MessageBoxButtons.OK,
                                    MessageBoxIcon.Exclamation);
                    }

                } while (nameWindow.DialogResult != DialogResult.Cancel);

                if (nameWindow.DialogResult == DialogResult.Cancel)
                {
                    return;
                }
            }

            var algorithmInfo = new AlgorithmInfo
                {
                    AlgorithName = algo,
                    CustomName = customeName
                };
            algorithmInfo.Parameters = new List<AlgorithmParameter>();
            foreach (AlgorithmParameter parameter in AppFacade.DI.Container.Resolve<IAlgorithm>(algo).Parameters
                )
            {
                algorithmInfo.Parameters.Add(
                    new AlgorithmParameter(parameter.Name, parameter.DefaultValue)
                    {
                        Value = parameter.DefaultValue
                    });
            }

            SystemInstance.Algorithms.Add(algorithmInfo);
        }