private void OnSegmentationCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                this.DisposeStatusImages();
                SegmentationSolution result = (SegmentationSolution)e.Result;
                if (result.Shape != null)
                {
                    this.currentImage.Image = CreateStatusImage(result.Shape);
                }
                else
                {
                    this.currentImage.Image = (Image)this.segmentedImage.Clone();
                }
                if (result.Mask != null)
                {
                    this.segmentationMaskImage.Image = Image2D.ToRegularImage(result.Mask);
                }
            }
            else
            {
                MessageBox.Show(e.Error.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            this.startGpuButton.Enabled             = true;
            this.startCpuButton.Enabled             = true;
            this.stopButton.Enabled                 = false;
            this.pauseContinueButton.Enabled        = false;
            this.segmentationPropertiesGrid.Enabled = true;
        }
        private void SegmentImage(Action <SimpleSegmentationAlgorithm, double> customSetupStep)
        {
            int          selectedIndex = this.backgroundImagesListBox.SelectedIndex;
            BitmapSource originalImage = this.imageInfos[selectedIndex].Image;
            double       scale         = CalcSegmentedImageScale(originalImage);

            BitmapSource scaledImage = ImageHelper.ResizeImage(
                originalImage, (int)(originalImage.PixelWidth * scale), (int)(originalImage.PixelHeight * scale));
            Image2D <Color> image = ImageHelper.BitmapSourceToImage2D(scaledImage);

            Image2D.SaveToFile(image, "image.png");

            // Customize segmentator
            SimpleSegmentationAlgorithm segmentator = new SimpleSegmentationAlgorithm();

            segmentator.ObjectShapeUnaryTermWeight        = this.algorithmProperties.ShapeUnaryTermWeight;
            segmentator.BackgroundShapeUnaryTermWeight    = this.algorithmProperties.ShapeUnaryTermWeight;
            segmentator.ObjectColorUnaryTermWeight        = this.algorithmProperties.ColorUnaryTermWeight;
            segmentator.BackgroundColorUnaryTermWeight    = this.algorithmProperties.ColorUnaryTermWeight;
            segmentator.ColorDifferencePairwiseTermWeight = this.algorithmProperties.BinaryTermWeight;
            segmentator.ColorDifferencePairwiseTermCutoff = this.algorithmProperties.BrightnessBinaryTermCutoff;
            segmentator.ShapeEnergyWeight = this.algorithmProperties.ShapeEnergyWeight;

            // Run custom setup step
            customSetupStep(segmentator, scale);

            // Segment
            SegmentationSolution result = segmentator.SegmentImage(image, this.colorModels);

            Image2D.SaveToFile(result.Mask, "mask.png");
            BitmapSource maskImage = ImageHelper.ResizeImage(
                ImageHelper.MaskToBitmapSource(result.Mask), originalImage.PixelWidth, originalImage.PixelHeight);

            this.imageInfos[selectedIndex].SegmentationMask = maskImage;

            // Show results
            this.UpdateControlsAccordingToCurrentState();
            this.editorTabControl.SelectedIndex = 1;
        }
        private void DoSegmentation(object sender, DoWorkEventArgs e)
        {
            Random.SetSeed(666);

            // Load and downscale image
            Image2D <Color> originalImage   = Image2D.LoadFromFile(this.segmentationProperties.ImageToSegment);
            double          scale           = this.segmentationProperties.DownscaledImageSize / (double)Math.Max(originalImage.Width, originalImage.Height);
            Image2D <Color> downscaledImage = Image2D.LoadFromFile(this.segmentationProperties.ImageToSegment, scale);

            this.segmentedImage = Image2D.ToRegularImage(downscaledImage);

            // Load color models
            ObjectBackgroundColorModels colorModels = ObjectBackgroundColorModels.LoadFromFile(this.segmentationProperties.ColorModel);

            // Setup shape model
            ShapeModel model = ShapeModel.LoadFromFile(this.segmentationProperties.ShapeModel);

            this.segmentator.ShapeModel = model;

            // Common settings
            segmentator.ObjectColorUnaryTermWeight        = this.segmentationProperties.ObjectColorUnaryTermWeight;
            segmentator.BackgroundColorUnaryTermWeight    = this.segmentationProperties.BackgroundColorUnaryTermWeight;
            segmentator.ObjectShapeUnaryTermWeight        = this.segmentationProperties.ObjectShapeUnaryTermWeight;
            segmentator.BackgroundShapeUnaryTermWeight    = this.segmentationProperties.BackgroundShapeUnaryTermWeight;
            segmentator.ColorDifferencePairwiseTermWeight = this.segmentationProperties.ColorDifferencePairwiseTermWeight;
            segmentator.ColorDifferencePairwiseTermCutoff = this.segmentationProperties.ColorDifferencePairwiseTermCutoff;
            segmentator.ConstantPairwiseTermWeight        = this.segmentationProperties.ConstantPairwiseTermWeight;
            segmentator.ShapeEnergyWeight = this.segmentationProperties.ShapeEnergyWeight;

            // Custom setup
            if (this.segmentator is BranchAndBoundSegmentationAlgorithm)
            {
                this.SetupBranchAndBoundSegmentationAlgorithm((BranchAndBoundSegmentationAlgorithm)this.segmentator);
            }
            else if (this.segmentator is CoordinateDescentSegmentationAlgorithm)
            {
                this.SetupCoordinateDescentSegmentationAlgorithm((CoordinateDescentSegmentationAlgorithm)this.segmentator);
            }
            else if (this.segmentator is AnnealingSegmentationAlgorithm)
            {
                this.SetupAnnealingSegmentationAlgorithm((AnnealingSegmentationAlgorithm)this.segmentator);
            }
            else if (this.segmentator is SimpleSegmentationAlgorithm)
            {
                this.SetupSimpleSegmentationAlgorithm((SimpleSegmentationAlgorithm)this.segmentator);
            }

            // Show original image in status window)
            this.currentImage.Image = (Image)this.segmentedImage.Clone();

            // Run segmentation
            SegmentationSolution solution = segmentator.SegmentImage(downscaledImage, colorModels);

            // Re-run B&B segmentation with reduced constraints in two-step mode
            if (this.segmentator is BranchAndBoundSegmentationAlgorithm && this.segmentationProperties.UseTwoStepApproach && !this.segmentator.WasStopped)
            {
                BranchAndBoundSegmentationAlgorithm branchAndBoundSegmentator =
                    (BranchAndBoundSegmentationAlgorithm)this.segmentator;

                branchAndBoundSegmentator.MaxCoordFreedom  = this.segmentationProperties.MaxCoordFreedom;
                branchAndBoundSegmentator.MaxWidthFreedom  = this.segmentationProperties.MaxWidthFreedom;
                branchAndBoundSegmentator.StartConstraints = this.bestConstraints;
                branchAndBoundSegmentator.ShapeEnergyLowerBoundCalculator = new ShapeEnergyLowerBoundCalculator(
                    this.segmentationProperties.LengthGridSize, this.segmentationProperties.AngleGridSize);

                Console.WriteLine("Performing second pass...");
                solution = segmentator.SegmentImage(downscaledImage, colorModels);
            }

            // Save mask as worker result
            e.Result = solution;
        }