private void SetupBranchAndBoundSegmentationAlgorithm(BranchAndBoundSegmentationAlgorithm algorithm)
        {
            algorithm.BreadthFirstBranchAndBoundProgress += OnBfsStatusUpdate;
            algorithm.BranchAndBoundCompleted            += OnBranchAndBoundCompleted;

            ShapeEnergyLowerBoundCalculator shapeEnergyCalculator;

            algorithm.ProgressReportRate = this.segmentationProperties.BranchAndBoundReportRate;
            algorithm.MinEdgeWidth       = this.segmentationProperties.MinEdgeWidth;
            algorithm.MaxEdgeWidth       = this.segmentationProperties.MaxEdgeWidth;
            if (this.segmentationProperties.UseTwoStepApproach)
            {
                algorithm.MaxCoordFreedom = this.segmentationProperties.MaxCoordFreedomPre;
                algorithm.MaxWidthFreedom = this.segmentationProperties.MaxWidthFreedomPre;
                shapeEnergyCalculator     = new ShapeEnergyLowerBoundCalculator(
                    this.segmentationProperties.LengthGridSizePre, this.segmentationProperties.AngleGridSizePre);
            }
            else
            {
                algorithm.MaxCoordFreedom = this.segmentationProperties.MaxCoordFreedom;
                algorithm.MaxWidthFreedom = this.segmentationProperties.MaxWidthFreedom;
                shapeEnergyCalculator     = new ShapeEnergyLowerBoundCalculator(
                    this.segmentationProperties.LengthGridSize, this.segmentationProperties.AngleGridSize);
            }
            algorithm.ShapeEnergyLowerBoundCalculator = shapeEnergyCalculator;
        }
 private void OnStartGpuButtonClick(object sender, EventArgs e)
 {
     if (this.segmentationProperties.Algorithm == SegmentationAlgorithm.BranchAndBound)
     {
         BranchAndBoundSegmentationAlgorithm branchAndBoundSegmentator = new BranchAndBoundSegmentationAlgorithm();
         branchAndBoundSegmentator.ShapeTermCalculator = new GpuShapeTermsLowerBoundCalculator();
         this.segmentator = branchAndBoundSegmentator;
         this.RunSegmentation();
     }
     else
     {
         MessageBox.Show(
             "No GPU version available for this kind of algorithm.",
             "Information",
             MessageBoxButtons.OK,
             MessageBoxIcon.Information);
     }
 }
        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;
        }