private void OnLoadShapeModelButtonClick(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openDialog = new OpenFileDialog();

            openDialog.Filter           = "Shape models|*.shp";
            openDialog.RestoreDirectory = true;
            bool?result = openDialog.ShowDialog();

            if (result != true)
            {
                return;
            }

            this.shapeModel = ShapeModel.LoadFromFile(openDialog.FileName);
            for (int i = 0; i < imageInfos.Count; ++i)
            {
                Shape meanShape = this.shapeModel.FitMeanShape(imageInfos[i].Image.PixelWidth, imageInfos[i].Image.PixelHeight);
                imageInfos[i].Shape = meanShape;
            }

            this.UpdateControlsAccordingToCurrentState();
        }
        static void MainForGiraffeAdjustSave()
        {
            ShapeModel model = ShapeModel.LoadFromFile(@"C:\segmentation-with-shape-priors\Data\giraffes\giraffe_learned_pre.shp");

            // Edge
            model.GetMutableEdgeParams(0).WidthToEdgeLengthRatioDeviation = WeightToDeviation(1.819928);
            model.GetMutableEdgeParams(1).WidthToEdgeLengthRatioDeviation = WeightToDeviation(0.586110);
            model.GetMutableEdgeParams(2).WidthToEdgeLengthRatioDeviation = WeightToDeviation(0.249062);
            model.GetMutableEdgeParams(3).WidthToEdgeLengthRatioDeviation = WeightToDeviation(0.673922);
            model.GetMutableEdgeParams(4).WidthToEdgeLengthRatioDeviation = WeightToDeviation(0.541160);

            // Edge pair
            model.GetMutableEdgePairParams(0, 1).AngleDeviation      = WeightToDeviation(1.311689);
            model.GetMutableEdgePairParams(0, 1).LengthDiffDeviation = WeightToDeviation(0.001267);
            model.GetMutableEdgePairParams(0, 2).AngleDeviation      = WeightToDeviation(3.032988);
            model.GetMutableEdgePairParams(0, 2).LengthDiffDeviation = WeightToDeviation(0);
            model.GetMutableEdgePairParams(0, 3).AngleDeviation      = WeightToDeviation(0);
            model.GetMutableEdgePairParams(0, 3).LengthDiffDeviation = WeightToDeviation(0);
            model.GetMutableEdgePairParams(2, 4).AngleDeviation      = WeightToDeviation(0.985037);
            model.GetMutableEdgePairParams(2, 4).LengthDiffDeviation = WeightToDeviation(0);

            model.SaveToFile(@"C:\segmentation-with-shape-priors\Data\giraffes\giraffe_learned.shp");
        }
        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;
        }