Exemplo n.º 1
0
        public SegmentationSolution SegmentImage(Image2D <Color> image, ObjectBackgroundColorModels colorModels)
        {
            if (image == null)
            {
                throw new ArgumentNullException("image");
            }
            if (colorModels == null)
            {
                throw new ArgumentNullException("colorModels");
            }

            this.ImageSegmentator = new ImageSegmentator(
                image,
                colorModels,
                this.ColorDifferencePairwiseTermCutoff,
                this.ColorDifferencePairwiseTermWeight,
                this.ConstantPairwiseTermWeight,
                this.ObjectColorUnaryTermWeight,
                this.BackgroundColorUnaryTermWeight,
                this.ObjectShapeUnaryTermWeight,
                this.BackgroundShapeUnaryTermWeight);

            DebugConfiguration.WriteImportantDebugText(
                "Segmented image size is {0}x{1}.",
                this.ImageSegmentator.ImageSize.Width,
                this.ImageSegmentator.ImageSize.Height);

            SegmentationSolution solution = null;

            try
            {
                if (this.ShapeModel == null)
                {
                    throw new InvalidOperationException("Shape model must be specified before segmenting image.");
                }

                DebugConfiguration.WriteImportantDebugText("Running segmentation algorithm...");
                this.IsRunning = true;
                solution       = this.SegmentCurrentImage();

                if (solution == null)
                {
                    throw new InvalidOperationException("Segmentation solution can not be null.");
                }
            }
            finally
            {
                if (this.IsStopping)
                {
                    this.WasStopped = true;
                }

                this.IsRunning  = false;
                this.IsStopping = false;
            }

            DebugConfiguration.WriteImportantDebugText("Finished");

            return(solution);
        }
Exemplo n.º 2
0
        public ImageSegmentator(
            Image2D <Color> image,
            ObjectBackgroundColorModels colorModels,
            double colorDifferencePairwiseTermCutoff,
            double colorDifferencePairwiseTermWeight,
            double constantPairwiseTermWeight,
            double objectColorUnaryTermWeight,
            double backgroundColorUnaryTermWeight,
            double objectShapeUnaryTermWeight,
            double backgroundShapeUnaryTermWeight)
        {
            if (image == null)
            {
                throw new ArgumentNullException("image");
            }
            if (colorModels == null)
            {
                throw new ArgumentNullException("colorModels");
            }

            if (colorDifferencePairwiseTermCutoff <= 0)
            {
                throw new ArgumentOutOfRangeException("colorDifferencePairwiseTermCutoff", "Parameter value should be positive.");
            }
            if (colorDifferencePairwiseTermWeight < 0)
            {
                throw new ArgumentOutOfRangeException("colorDifferencePairwiseTermWeight", "Parameter value should not be negative.");
            }
            if (constantPairwiseTermWeight < 0)
            {
                throw new ArgumentOutOfRangeException("constantPairwiseTermWeight", "Parameter value should not be negative.");
            }

            this.ColorDifferencePairwiseTermCutoff = colorDifferencePairwiseTermCutoff;
            this.ColorDifferencePairwiseTermWeight = colorDifferencePairwiseTermWeight;
            this.ConstantPairwiseTermWeight        = constantPairwiseTermWeight;
            this.ObjectColorUnaryTermWeight        = objectColorUnaryTermWeight;
            this.BackgroundColorUnaryTermWeight    = backgroundColorUnaryTermWeight;
            this.ObjectShapeUnaryTermWeight        = objectShapeUnaryTermWeight;
            this.BackgroundShapeUnaryTermWeight    = backgroundShapeUnaryTermWeight;

            this.segmentedImage = image;

            this.UnaryTermScaleCoeff    = 1.0 / (image.Width * image.Height);
            this.PairwiseTermScaleCoeff = 1.0 / Math.Sqrt(image.Width * image.Height);

            this.graphCutCalculator = new GraphCutCalculator(this.segmentedImage.Width, this.segmentedImage.Height);

            this.PrepareColorTerms(colorModels);
            this.PreparePairwiseTerms();
            this.PrepareOther();
        }
Exemplo n.º 3
0
 private void PrepareColorTerms(ObjectBackgroundColorModels colorModels)
 {
     this.colorTerms = new Image2D <ObjectBackgroundTerm>(this.ImageSize.Width, this.ImageSize.Height);
     for (int x = 0; x < this.ImageSize.Width; ++x)
     {
         for (int y = 0; y < this.ImageSize.Height; ++y)
         {
             Color  color          = this.segmentedImage[x, y];
             double objectTerm     = -colorModels.ObjectColorModel.LogProb(color);
             double backgroundTerm = -colorModels.BackgroundColorModel.LogProb(color);
             this.colorTerms[x, y] = new ObjectBackgroundTerm(objectTerm, backgroundTerm);
         }
     }
 }
        public ImageSegmentator(
            Image2D<Color> image,
            ObjectBackgroundColorModels colorModels,
            double colorDifferencePairwiseTermCutoff,
            double colorDifferencePairwiseTermWeight,
            double constantPairwiseTermWeight,
            double objectColorUnaryTermWeight,
            double backgroundColorUnaryTermWeight,
            double objectShapeUnaryTermWeight,
            double backgroundShapeUnaryTermWeight)
        {
            if (image == null)
                throw new ArgumentNullException("image");
            if (colorModels == null)
                throw new ArgumentNullException("colorModels");

            if (colorDifferencePairwiseTermCutoff <= 0)
                throw new ArgumentOutOfRangeException("colorDifferencePairwiseTermCutoff", "Parameter value should be positive.");
            if (colorDifferencePairwiseTermWeight < 0)
                throw new ArgumentOutOfRangeException("colorDifferencePairwiseTermWeight", "Parameter value should not be negative.");
            if (constantPairwiseTermWeight < 0)
                throw new ArgumentOutOfRangeException("constantPairwiseTermWeight", "Parameter value should not be negative.");

            this.ColorDifferencePairwiseTermCutoff = colorDifferencePairwiseTermCutoff;
            this.ColorDifferencePairwiseTermWeight = colorDifferencePairwiseTermWeight;
            this.ConstantPairwiseTermWeight = constantPairwiseTermWeight;
            this.ObjectColorUnaryTermWeight = objectColorUnaryTermWeight;
            this.BackgroundColorUnaryTermWeight = backgroundColorUnaryTermWeight;
            this.ObjectShapeUnaryTermWeight = objectShapeUnaryTermWeight;
            this.BackgroundShapeUnaryTermWeight = backgroundShapeUnaryTermWeight;

            this.segmentedImage = image;
            
            this.UnaryTermScaleCoeff = 1.0 / (image.Width * image.Height);
            this.PairwiseTermScaleCoeff = 1.0 / Math.Sqrt(image.Width * image.Height);

            this.graphCutCalculator = new GraphCutCalculator(this.segmentedImage.Width, this.segmentedImage.Height);

            this.PrepareColorTerms(colorModels);
            this.PreparePairwiseTerms();
            this.PrepareOther();
        }
        public SegmentationSolution SegmentImage(Image2D<Color> image, ObjectBackgroundColorModels colorModels)
        {
            if (image == null)
                throw new ArgumentNullException("image");
            if (colorModels == null)
                throw new ArgumentNullException("colorModels");

            this.ImageSegmentator = new ImageSegmentator(
                image,
                colorModels,
                this.ColorDifferencePairwiseTermCutoff,
                this.ColorDifferencePairwiseTermWeight,
                this.ConstantPairwiseTermWeight,
                this.ObjectColorUnaryTermWeight,
                this.BackgroundColorUnaryTermWeight,
                this.ObjectShapeUnaryTermWeight,
                this.BackgroundShapeUnaryTermWeight);

            DebugConfiguration.WriteImportantDebugText(
                "Segmented image size is {0}x{1}.",
                this.ImageSegmentator.ImageSize.Width,
                this.ImageSegmentator.ImageSize.Height);

            SegmentationSolution solution = null;
            try
            {
                if (this.ShapeModel == null)
                    throw new InvalidOperationException("Shape model must be specified before segmenting image.");

                DebugConfiguration.WriteImportantDebugText("Running segmentation algorithm...");
                this.IsRunning = true;
                solution = this.SegmentCurrentImage();

                if (solution == null)
                    throw new InvalidOperationException("Segmentation solution can not be null.");
            }
            finally
            {
                if (this.IsStopping)
                    this.WasStopped = true;
                
                this.IsRunning = false;
                this.IsStopping = false;
            }

            DebugConfiguration.WriteImportantDebugText("Finished");

            return solution;
        }
 private void PrepareColorTerms(ObjectBackgroundColorModels colorModels)
 {
     this.colorTerms = new Image2D<ObjectBackgroundTerm>(this.ImageSize.Width, this.ImageSize.Height);
     for (int x = 0; x < this.ImageSize.Width; ++x)
     {
         for (int y = 0; y < this.ImageSize.Height; ++y)
         {
             Color color = this.segmentedImage[x, y];
             double objectTerm = -colorModels.ObjectColorModel.LogProb(color);
             double backgroundTerm = -colorModels.BackgroundColorModel.LogProb(color);
             this.colorTerms[x, y] = new ObjectBackgroundTerm(objectTerm, backgroundTerm);
         }
     }
 }