private void SetupSimpleSegmentationAlgorithm(SimpleSegmentationAlgorithm algorithm)
 {
     if (!String.IsNullOrEmpty(this.segmentationProperties.InitialShape))
     {
         algorithm.Shape = Shape.LoadFromFile(this.segmentationProperties.InitialShape);
     }
 }
        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 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;
        }