Class MaskingOptions

Class MaskingOptions

Nazwa przestrzeń: Aspose.Imaging.Masking.Options Zgromadzenie: Aspose.Imaging.dll (25.4.0)

Przedstawia powszechne opcje maskowania obrazu.

public class MaskingOptions

Inheritance

object MaskingOptions

Derived

GraphCutMaskingOptions

Dziedziczeni członkowie

object.GetType() , object.MemberwiseClone() , object.ToString() , object.Equals(object?) , object.Equals(object?, object?) , object.ReferenceEquals(object?, object?) , object.GetHashCode()

Examples

Ten przykład pokazuje, jak rozdzielić obraz rasterowy na kilka obrazów za pomocą maski obrazu i algorytmu segmentacji K-means.

string dir = "c:\\temp\\";

                                                                                                                                                                                                                                                                     using (Aspose.Imaging.RasterImage image = (Aspose.Imaging.RasterImage)Aspose.Imaging.Image.Load(dir + "Blue hills.png"))
                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                         Aspose.Imaging.Masking.Options.AutoMaskingArgs args = new Aspose.Imaging.Masking.Options.AutoMaskingArgs();

                                                                                                                                                                                                                                                                         // Set the number of clusters (separated objects). The default value is 2, the foreground object and the background.
                                                                                                                                                                                                                                                                         args.NumberOfObjects = 3;

                                                                                                                                                                                                                                                                         // Set the maximum number of iterations.
                                                                                                                                                                                                                                                                         args.MaxIterationNumber = 50;

                                                                                                                                                                                                                                                                         // Set the precision of segmentation method (optional)
                                                                                                                                                                                                                                                                         args.Precision = 1;

                                                                                                                                                                                                                                                                         // Each cluster (segment) will be stored to a separate PNG file.
                                                                                                                                                                                                                                                                         Aspose.Imaging.ImageOptions.PngOptions exportOptions = new Aspose.Imaging.ImageOptions.PngOptions();
                                                                                                                                                                                                                                                                         exportOptions.ColorType = Aspose.Imaging.FileFormats.Png.PngColorType.TruecolorWithAlpha;
                                                                                                                                                                                                                                                                         exportOptions.Source = new Aspose.Imaging.Sources.StreamSource(new System.IO.MemoryStream());

                                                                                                                                                                                                                                                                         Aspose.Imaging.Masking.Options.MaskingOptions maskingOptions = new Aspose.Imaging.Masking.Options.MaskingOptions();

                                                                                                                                                                                                                                                                         // Use K-means clustering.
                                                                                                                                                                                                                                                                         // K-means clustering allows to split image into several independent clusters (segments).
                                                                                                                                                                                                                                                                         maskingOptions.Method = Masking.Options.SegmentationMethod.KMeans;
                                                                                                                                                                                                                                                                         maskingOptions.Decompose = true;
                                                                                                                                                                                                                                                                         maskingOptions.Args = args;

                                                                                                                                                                                                                                                                         // The backgroung color will be orange.
                                                                                                                                                                                                                                                                         maskingOptions.BackgroundReplacementColor = Aspose.Imaging.Color.Orange;
                                                                                                                                                                                                                                                                         maskingOptions.ExportOptions = exportOptions;

                                                                                                                                                                                                                                                                         // Create an instance of the ImageMasking class.
                                                                                                                                                                                                                                                                         Aspose.Imaging.Masking.ImageMasking masking = new Aspose.Imaging.Masking.ImageMasking(image);

                                                                                                                                                                                                                                                                         // Divide the source image into several clusters (segments).
                                                                                                                                                                                                                                                                         using (Aspose.Imaging.Masking.Result.MaskingResult maskingResult = masking.Decompose(maskingOptions))
                                                                                                                                                                                                                                                                         {
                                                                                                                                                                                                                                                                             // Obtain images from masking result and save them to PNG.
                                                                                                                                                                                                                                                                             for (int i = 0; i < maskingResult.Length; i++)
                                                                                                                                                                                                                                                                             {
                                                                                                                                                                                                                                                                                 string outputFileName = string.Format("Blue hills.Segment{0}.png", maskingResult[i].ObjectNumber);
                                                                                                                                                                                                                                                                                 using (Aspose.Imaging.Image resultImage = maskingResult[i].GetImage())
                                                                                                                                                                                                                                                                                 {
                                                                                                                                                                                                                                                                                     resultImage.Save(dir + outputFileName);
                                                                                                                                                                                                                                                                                 }
                                                                                                                                                                                                                                                                             }
                                                                                                                                                                                                                                                                         }
                                                                                                                                                                                                                                                                     }

Używanie maski segmentowej do przyspieszenia procesu segmentacji

// Masking export options
                                                                    Aspose.Imaging.ImageOptions.PngOptions exportOptions = new Aspose.Imaging.ImageOptions.PngOptions();
                                                                    exportOptions.ColorType = Aspose.Imaging.FileFormats.Png.PngColorType.TruecolorWithAlpha;
                                                                    exportOptions.Source = new Aspose.Imaging.Sources.StreamSource(new System.IO.MemoryStream());

                                                                    Aspose.Imaging.Masking.Options.MaskingOptions maskingOptions = new Aspose.Imaging.Masking.Options.MaskingOptions();

                                                                    // Use GraphCut clustering.
                                                                    maskingOptions.Method = Masking.Options.SegmentationMethod.GraphCut;
                                                                    maskingOptions.Decompose = false;
                                                                    maskingOptions.Args = new Aspose.Imaging.Masking.Options.AutoMaskingArgs();

                                                                    // The backgroung color will be transparent.
                                                                    maskingOptions.BackgroundReplacementColor = Aspose.Imaging.Color.Transparent;
                                                                    maskingOptions.ExportOptions = exportOptions;

                                                                    string dir = "c:\\temp\\";
                                                                    using (Aspose.Imaging.RasterImage image = (Aspose.Imaging.RasterImage)Aspose.Imaging.Image.Load(dir + "BigImage.jpg"))
                                                                    {
                                                                        Aspose.Imaging.Size imageSize = image.Size;

                                                                        // Reducing image size to speed up the segmentation process
                                                                        image.ResizeHeightProportionally(600, Aspose.Imaging.ResizeType.HighQualityResample);

                                                                        // Create an instance of the ImageMasking class.
                                                                        Aspose.Imaging.Masking.ImageMasking masking = new Aspose.Imaging.Masking.ImageMasking(image);

                                                                        // Divide the source image into several clusters (segments).
                                                                        using (Aspose.Imaging.Masking.Result.MaskingResult maskingResult = masking.Decompose(maskingOptions))
                                                                        {
                                                                            // Getting the foreground mask
                                                                            using (Aspose.Imaging.RasterImage foregroundMask = maskingResult[1].GetMask()) 
                                                                            {
                                                                                // Increase the size of the mask to the size of the original image
                                                                                foregroundMask.Resize(imageSize.Width, imageSize.Height, Aspose.Imaging.ResizeType.NearestNeighbourResample);

                                                                                // Applying the mask to the original image to obtain a foreground segment
                                                                                using (Aspose.Imaging.RasterImage originImage = (Aspose.Imaging.RasterImage)Aspose.Imaging.Image.Load(dir + "BigImage.jpg"))
                                                                                {
                                                                                    Aspose.Imaging.Masking.ImageMasking.ApplyMask(originImage, foregroundMask, maskingOptions);
                                                                                    originImage.Save(dir + "BigImage_foreground.png", exportOptions);
                                                                                }
                                                                            }
                                                                        }
                                                                    }

Ten przykład pokazuje, jak rozdzielić obraz rasterowy na kilka obrazów za pomocą maski obrazu i ręcznie. maska obraza jest techniką przetwarzania obrazy, która jest używana do podziału tła z przedmiotów obrazowych z przodu.

string dir = "c:\\temp\\";

                                                                                                                                                                                                                                                // Define a manual mask.
                                                                                                                                                                                                                                                Aspose.Imaging.GraphicsPath manualMask = new Aspose.Imaging.GraphicsPath();
                                                                                                                                                                                                                                                Aspose.Imaging.Figure figure = new Aspose.Imaging.Figure();
                                                                                                                                                                                                                                                figure.AddShape(new Aspose.Imaging.Shapes.EllipseShape(new RectangleF(50, 50, 40, 40)));
                                                                                                                                                                                                                                                figure.AddShape(new Aspose.Imaging.Shapes.RectangleShape(new RectangleF(10, 20, 50, 30)));
                                                                                                                                                                                                                                                manualMask.AddFigure(figure);

                                                                                                                                                                                                                                                // Each cluster (segment) will be stored to a separate PNG file.
                                                                                                                                                                                                                                                Aspose.Imaging.ImageOptions.PngOptions exportOptions = new Aspose.Imaging.ImageOptions.PngOptions();
                                                                                                                                                                                                                                                exportOptions.ColorType = Aspose.Imaging.FileFormats.Png.PngColorType.TruecolorWithAlpha;
                                                                                                                                                                                                                                                exportOptions.Source = new Aspose.Imaging.Sources.StreamSource(new System.IO.MemoryStream());

                                                                                                                                                                                                                                                // Set the manual mask.
                                                                                                                                                                                                                                                Aspose.Imaging.Masking.Options.ManualMaskingArgs args = new Aspose.Imaging.Masking.Options.ManualMaskingArgs();
                                                                                                                                                                                                                                                args.Mask = manualMask;

                                                                                                                                                                                                                                                using (RasterImage image = (RasterImage)Image.Load(dir + "Blue hills.png"))
                                                                                                                                                                                                                                                {
                                                                                                                                                                                                                                                    Aspose.Imaging.Masking.Options.MaskingOptions maskingOptions = new Aspose.Imaging.Masking.Options.MaskingOptions();

                                                                                                                                                                                                                                                    // Use manual clustering algorithm.
                                                                                                                                                                                                                                                    maskingOptions.Method = Masking.Options.SegmentationMethod.Manual;

                                                                                                                                                                                                                                                    // All shapes making up a mask will be combined into one. 
                                                                                                                                                                                                                                                    maskingOptions.Decompose = false;
                                                                                                                                                                                                                                                    maskingOptions.Args = args;

                                                                                                                                                                                                                                                    // The backgroung color will be orange.
                                                                                                                                                                                                                                                    maskingOptions.BackgroundReplacementColor = Aspose.Imaging.Color.Orange;
                                                                                                                                                                                                                                                    maskingOptions.ExportOptions = exportOptions;

                                                                                                                                                                                                                                                    // The area of the source image that masking will be applied to.
                                                                                                                                                                                                                                                    maskingOptions.MaskingArea = new Rectangle(50, 50, 120, 120);

                                                                                                                                                                                                                                                    // Create an instance of the ImageMasking class.
                                                                                                                                                                                                                                                    Aspose.Imaging.Masking.ImageMasking masking = new Aspose.Imaging.Masking.ImageMasking(image);

                                                                                                                                                                                                                                                    // Divide the source image into several clusters (segments).
                                                                                                                                                                                                                                                    using (Aspose.Imaging.Masking.Result.MaskingResult maskingResult = masking.Decompose(maskingOptions))
                                                                                                                                                                                                                                                    {
                                                                                                                                                                                                                                                        // Obtain images from masking result and save them to PNG.
                                                                                                                                                                                                                                                        for (int i = 0; i < maskingResult.Length; i++)
                                                                                                                                                                                                                                                        {
                                                                                                                                                                                                                                                            string outputFileName = string.Format("Blue hills.Segment{0}.png", maskingResult[i].ObjectNumber);
                                                                                                                                                                                                                                                            using (Aspose.Imaging.Image resultImage = maskingResult[i].GetImage())
                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                resultImage.Save(dir + outputFileName);
                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                }

Ten przykład pokazuje, jak określić sugestie dla algorytmu ukrywania obrazu w celu poprawy precyzji metody segmentacji (klastrowania).

string dir = "c:\\temp\\";

                                                                                                                                                                                                                                                                     using (Aspose.Imaging.RasterImage image = (Aspose.Imaging.RasterImage)Aspose.Imaging.Image.Load(dir + "Gorilla.bmp"))
                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                         Aspose.Imaging.Masking.Options.AutoMaskingArgs args = new Aspose.Imaging.Masking.Options.AutoMaskingArgs();

                                                                                                                                                                                                                                                                         // Suggestion #1.
                                                                                                                                                                                                                                                                         // Analyze the image visually and set the area of interest. The result of segmentation will include only objects that will be completely located within this area.
                                                                                                                                                                                                                                                                         args.ObjectsRectangles = new Rectangle[]
                                                                                                                                                                                                                                                                         {
                                                                                                                                                                                                                                                                             new Rectangle(86, 6, 270, 364),
                                                                                                                                                                                                                                                                         };

                                                                                                                                                                                                                                                                         // Suggestion #2.
                                                                                                                                                                                                                                                                         // Analyze the image visually and set the points that belong to separated objects.
                                                                                                                                                                                                                                                                         args.ObjectsPoints = new Point[][]
                                                                                                                                                                                                                                                                         {
                                                                                                                                                                                                                                                                             new Point[] { new Point(103, 326) },
                                                                                                                                                                                                                                                                             new Point[] { new Point(280, 43) },
                                                                                                                                                                                                                                                                             new Point[] { new Point(319, 86) },
                                                                                                                                                                                                                                                                         };

                                                                                                                                                                                                                                                                         // Each cluster (segment) will be stored to a separate PNG file.
                                                                                                                                                                                                                                                                         Aspose.Imaging.ImageOptions.PngOptions exportOptions = new Aspose.Imaging.ImageOptions.PngOptions();
                                                                                                                                                                                                                                                                         exportOptions.ColorType = Aspose.Imaging.FileFormats.Png.PngColorType.TruecolorWithAlpha;
                                                                                                                                                                                                                                                                         exportOptions.Source = new Aspose.Imaging.Sources.StreamSource(new System.IO.MemoryStream());

                                                                                                                                                                                                                                                                         Aspose.Imaging.Masking.Options.MaskingOptions maskingOptions = new Aspose.Imaging.Masking.Options.MaskingOptions();

                                                                                                                                                                                                                                                                         // Use GraphCut clustering.
                                                                                                                                                                                                                                                                         maskingOptions.Method = Masking.Options.SegmentationMethod.GraphCut;
                                                                                                                                                                                                                                                                         maskingOptions.Decompose = false;
                                                                                                                                                                                                                                                                         maskingOptions.Args = args;

                                                                                                                                                                                                                                                                         // The backgroung color will be orange.
                                                                                                                                                                                                                                                                         maskingOptions.BackgroundReplacementColor = Aspose.Imaging.Color.Orange;
                                                                                                                                                                                                                                                                         maskingOptions.ExportOptions = exportOptions;

                                                                                                                                                                                                                                                                         // Create an instance of the ImageMasking class.
                                                                                                                                                                                                                                                                         Aspose.Imaging.Masking.ImageMasking masking = new Aspose.Imaging.Masking.ImageMasking(image);

                                                                                                                                                                                                                                                                         // Divide the source image into several clusters (segments).
                                                                                                                                                                                                                                                                         using (Aspose.Imaging.Masking.Result.MaskingResult maskingResult = masking.Decompose(maskingOptions))
                                                                                                                                                                                                                                                                         {
                                                                                                                                                                                                                                                                             // Obtain images from masking result and save them to PNG.
                                                                                                                                                                                                                                                                             for (int i = 0; i < maskingResult.Length; i++)
                                                                                                                                                                                                                                                                             {
                                                                                                                                                                                                                                                                                 string outputFileName = string.Format("Gorilla.Segment{0}.png", maskingResult[i].ObjectNumber);
                                                                                                                                                                                                                                                                                 using (Aspose.Imaging.Image resultImage = maskingResult[i].GetImage())
                                                                                                                                                                                                                                                                                 {
                                                                                                                                                                                                                                                                                     resultImage.Save(dir + outputFileName);
                                                                                                                                                                                                                                                                                 }
                                                                                                                                                                                                                                                                             }
                                                                                                                                                                                                                                                                         }
                                                                                                                                                                                                                                                                     }

Oszczędzanie sesji masku do pliku na długie sesje, a także możliwość ponownego odtwarzania w innym środowisku.

string dir = "c:\\temp\\";
                                                                                                                                                 string sessionBackupFile = dir + "session.bak";

                                                                                                                                                 // Masking export options
                                                                                                                                                 Aspose.Imaging.ImageOptions.PngOptions exportOptions = new Aspose.Imaging.ImageOptions.PngOptions();
                                                                                                                                                 exportOptions.ColorType = Aspose.Imaging.FileFormats.Png.PngColorType.TruecolorWithAlpha;
                                                                                                                                                 exportOptions.Source = new Aspose.Imaging.Sources.StreamSource(new System.IO.MemoryStream());

                                                                                                                                                 Aspose.Imaging.Masking.Options.MaskingOptions maskingOptions = new Aspose.Imaging.Masking.Options.MaskingOptions();

                                                                                                                                                 // Use GraphCut clustering.
                                                                                                                                                 maskingOptions.Method = Masking.Options.SegmentationMethod.GraphCut;
                                                                                                                                                 maskingOptions.Decompose = false;
                                                                                                                                                 maskingOptions.Args = new Aspose.Imaging.Masking.Options.AutoMaskingArgs();

                                                                                                                                                 // The backgroung color will be orange.
                                                                                                                                                 maskingOptions.BackgroundReplacementColor = Aspose.Imaging.Color.Orange;
                                                                                                                                                 maskingOptions.ExportOptions = exportOptions;

                                                                                                                                                 // Starting a session for the first time and saving to a file
                                                                                                                                                 using (Aspose.Imaging.RasterImage image = (Aspose.Imaging.RasterImage)Aspose.Imaging.Image.Load(dir + "Gorilla.bmp"))
                                                                                                                                                 {
                                                                                                                                                     // Create an instance of the ImageMasking class.
                                                                                                                                                     Aspose.Imaging.Masking.ImageMasking masking = new Aspose.Imaging.Masking.ImageMasking(image);

                                                                                                                                                     using (Aspose.Imaging.Masking.IMaskingSession session = masking.CreateSession(maskingOptions))
                                                                                                                                                     {
                                                                                                                                                         using (Aspose.Imaging.Masking.Result.MaskingResult maskingResult = session.Decompose())
                                                                                                                                                         {
                                                                                                                                                             using (Aspose.Imaging.RasterImage segmentImage = maskingResult[1].GetImage())
                                                                                                                                                             {
                                                                                                                                                                 segmentImage.Save(dir + "step1.png");
                                                                                                                                                             }
                                                                                                                                                         }

                                                                                                                                                         session.Save(sessionBackupFile);
                                                                                                                                                     }
                                                                                                                                                 }

                                                                                                                                                 // Resuming a masking session from a file
                                                                                                                                                 using (Aspose.Imaging.RasterImage image = (Aspose.Imaging.RasterImage)Aspose.Imaging.Image.Load(dir + "Gorilla.bmp"))
                                                                                                                                                 {
                                                                                                                                                     // Create an instance of the ImageMasking class.
                                                                                                                                                     Aspose.Imaging.Masking.ImageMasking masking = new Aspose.Imaging.Masking.ImageMasking(image);

                                                                                                                                                     using (Aspose.Imaging.Masking.IMaskingSession session = masking.LoadSession(sessionBackupFile))
                                                                                                                                                     {
                                                                                                                                                         Aspose.Imaging.Masking.Options.AutoMaskingArgs args = new Aspose.Imaging.Masking.Options.AutoMaskingArgs();

                                                                                                                                                         // Analyze the image visually and set the points that belong to separated objects.
                                                                                                                                                         args.ObjectsPoints = new Point[][]
                                                                                                                                                                                      {
                                                                                                                                                                                          new Point[]
                                                                                                                                                                                              {
                                                                                                                                                                                                  new Point(0, 0), new Point(0, 1), new Point(1, 0),
                                                                                                                                                                                                  new Point(1, 1), new Point(2, 0), new Point(2, 1),
                                                                                                                                                                                                  new Point(3, 0), new Point(3, 1)
                                                                                                                                                                                              },
                                                                                                                                                                                      };
                                                                                                                                                         using (Aspose.Imaging.Masking.Result.MaskingResult maskingResult = session.ImproveDecomposition(args))
                                                                                                                                                         {
                                                                                                                                                             // Explicit transfer of export options, since it is not serializable
                                                                                                                                                             maskingResult.MaskingOptions.ExportOptions = exportOptions;

                                                                                                                                                             using (Aspose.Imaging.RasterImage segmentImage = maskingResult[1].GetImage())
                                                                                                                                                             {
                                                                                                                                                                 segmentImage.Save(dir + "step2.png");
                                                                                                                                                             }
                                                                                                                                                         }
                                                                                                                                                     }
                                                                                                                                                 }

Constructors

MaskingOptions()

public MaskingOptions()

Fields

BackgroundObjectNumber

Liczba obiektów tła

public const int BackgroundObjectNumber = 0

Wartość pola

int

Properties

Args

Otrzymuje lub ustawia argumenty dla algorytmu segmentacji.

public IMaskingArgs Args { get; set; }

Wartość nieruchomości

IMaskingArgs

Examples

Ten przykład pokazuje, jak rozdzielić obraz rasterowy na kilka obrazów za pomocą maski obrazu i algorytmu segmentacji K-means.

string dir = "c:\\temp\\";

                                                                                                                                                                                                                                                                     using (Aspose.Imaging.RasterImage image = (Aspose.Imaging.RasterImage)Aspose.Imaging.Image.Load(dir + "Blue hills.png"))
                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                         Aspose.Imaging.Masking.Options.AutoMaskingArgs args = new Aspose.Imaging.Masking.Options.AutoMaskingArgs();

                                                                                                                                                                                                                                                                         // Set the number of clusters (separated objects). The default value is 2, the foreground object and the background.
                                                                                                                                                                                                                                                                         args.NumberOfObjects = 3;

                                                                                                                                                                                                                                                                         // Set the maximum number of iterations.
                                                                                                                                                                                                                                                                         args.MaxIterationNumber = 50;

                                                                                                                                                                                                                                                                         // Set the precision of segmentation method (optional)
                                                                                                                                                                                                                                                                         args.Precision = 1;

                                                                                                                                                                                                                                                                         // Each cluster (segment) will be stored to a separate PNG file.
                                                                                                                                                                                                                                                                         Aspose.Imaging.ImageOptions.PngOptions exportOptions = new Aspose.Imaging.ImageOptions.PngOptions();
                                                                                                                                                                                                                                                                         exportOptions.ColorType = Aspose.Imaging.FileFormats.Png.PngColorType.TruecolorWithAlpha;
                                                                                                                                                                                                                                                                         exportOptions.Source = new Aspose.Imaging.Sources.StreamSource(new System.IO.MemoryStream());

                                                                                                                                                                                                                                                                         Aspose.Imaging.Masking.Options.MaskingOptions maskingOptions = new Aspose.Imaging.Masking.Options.MaskingOptions();

                                                                                                                                                                                                                                                                         // Use K-means clustering.
                                                                                                                                                                                                                                                                         // K-means clustering allows to split image into several independent clusters (segments).
                                                                                                                                                                                                                                                                         maskingOptions.Method = Masking.Options.SegmentationMethod.KMeans;
                                                                                                                                                                                                                                                                         maskingOptions.Decompose = true;
                                                                                                                                                                                                                                                                         maskingOptions.Args = args;

                                                                                                                                                                                                                                                                         // The backgroung color will be orange.
                                                                                                                                                                                                                                                                         maskingOptions.BackgroundReplacementColor = Aspose.Imaging.Color.Orange;
                                                                                                                                                                                                                                                                         maskingOptions.ExportOptions = exportOptions;

                                                                                                                                                                                                                                                                         // Create an instance of the ImageMasking class.
                                                                                                                                                                                                                                                                         Aspose.Imaging.Masking.ImageMasking masking = new Aspose.Imaging.Masking.ImageMasking(image);

                                                                                                                                                                                                                                                                         // Divide the source image into several clusters (segments).
                                                                                                                                                                                                                                                                         using (Aspose.Imaging.Masking.Result.MaskingResult maskingResult = masking.Decompose(maskingOptions))
                                                                                                                                                                                                                                                                         {
                                                                                                                                                                                                                                                                             // Obtain images from masking result and save them to PNG.
                                                                                                                                                                                                                                                                             for (int i = 0; i < maskingResult.Length; i++)
                                                                                                                                                                                                                                                                             {
                                                                                                                                                                                                                                                                                 string outputFileName = string.Format("Blue hills.Segment{0}.png", maskingResult[i].ObjectNumber);
                                                                                                                                                                                                                                                                                 using (Aspose.Imaging.Image resultImage = maskingResult[i].GetImage())
                                                                                                                                                                                                                                                                                 {
                                                                                                                                                                                                                                                                                     resultImage.Save(dir + outputFileName);
                                                                                                                                                                                                                                                                                 }
                                                                                                                                                                                                                                                                             }
                                                                                                                                                                                                                                                                         }
                                                                                                                                                                                                                                                                     }

Używanie maski segmentowej do przyspieszenia procesu segmentacji

// Masking export options
                                                                    Aspose.Imaging.ImageOptions.PngOptions exportOptions = new Aspose.Imaging.ImageOptions.PngOptions();
                                                                    exportOptions.ColorType = Aspose.Imaging.FileFormats.Png.PngColorType.TruecolorWithAlpha;
                                                                    exportOptions.Source = new Aspose.Imaging.Sources.StreamSource(new System.IO.MemoryStream());

                                                                    Aspose.Imaging.Masking.Options.MaskingOptions maskingOptions = new Aspose.Imaging.Masking.Options.MaskingOptions();

                                                                    // Use GraphCut clustering.
                                                                    maskingOptions.Method = Masking.Options.SegmentationMethod.GraphCut;
                                                                    maskingOptions.Decompose = false;
                                                                    maskingOptions.Args = new Aspose.Imaging.Masking.Options.AutoMaskingArgs();

                                                                    // The backgroung color will be transparent.
                                                                    maskingOptions.BackgroundReplacementColor = Aspose.Imaging.Color.Transparent;
                                                                    maskingOptions.ExportOptions = exportOptions;

                                                                    string dir = "c:\\temp\\";
                                                                    using (Aspose.Imaging.RasterImage image = (Aspose.Imaging.RasterImage)Aspose.Imaging.Image.Load(dir + "BigImage.jpg"))
                                                                    {
                                                                        Aspose.Imaging.Size imageSize = image.Size;

                                                                        // Reducing image size to speed up the segmentation process
                                                                        image.ResizeHeightProportionally(600, Aspose.Imaging.ResizeType.HighQualityResample);

                                                                        // Create an instance of the ImageMasking class.
                                                                        Aspose.Imaging.Masking.ImageMasking masking = new Aspose.Imaging.Masking.ImageMasking(image);

                                                                        // Divide the source image into several clusters (segments).
                                                                        using (Aspose.Imaging.Masking.Result.MaskingResult maskingResult = masking.Decompose(maskingOptions))
                                                                        {
                                                                            // Getting the foreground mask
                                                                            using (Aspose.Imaging.RasterImage foregroundMask = maskingResult[1].GetMask()) 
                                                                            {
                                                                                // Increase the size of the mask to the size of the original image
                                                                                foregroundMask.Resize(imageSize.Width, imageSize.Height, Aspose.Imaging.ResizeType.NearestNeighbourResample);

                                                                                // Applying the mask to the original image to obtain a foreground segment
                                                                                using (Aspose.Imaging.RasterImage originImage = (Aspose.Imaging.RasterImage)Aspose.Imaging.Image.Load(dir + "BigImage.jpg"))
                                                                                {
                                                                                    Aspose.Imaging.Masking.ImageMasking.ApplyMask(originImage, foregroundMask, maskingOptions);
                                                                                    originImage.Save(dir + "BigImage_foreground.png", exportOptions);
                                                                                }
                                                                            }
                                                                        }
                                                                    }

Ten przykład pokazuje, jak rozdzielić obraz rasterowy na kilka obrazów za pomocą maski obrazu i ręcznie. maska obraza jest techniką przetwarzania obrazy, która jest używana do podziału tła z przedmiotów obrazowych z przodu.

string dir = "c:\\temp\\";

                                                                                                                                                                                                                                                // Define a manual mask.
                                                                                                                                                                                                                                                Aspose.Imaging.GraphicsPath manualMask = new Aspose.Imaging.GraphicsPath();
                                                                                                                                                                                                                                                Aspose.Imaging.Figure figure = new Aspose.Imaging.Figure();
                                                                                                                                                                                                                                                figure.AddShape(new Aspose.Imaging.Shapes.EllipseShape(new RectangleF(50, 50, 40, 40)));
                                                                                                                                                                                                                                                figure.AddShape(new Aspose.Imaging.Shapes.RectangleShape(new RectangleF(10, 20, 50, 30)));
                                                                                                                                                                                                                                                manualMask.AddFigure(figure);

                                                                                                                                                                                                                                                // Each cluster (segment) will be stored to a separate PNG file.
                                                                                                                                                                                                                                                Aspose.Imaging.ImageOptions.PngOptions exportOptions = new Aspose.Imaging.ImageOptions.PngOptions();
                                                                                                                                                                                                                                                exportOptions.ColorType = Aspose.Imaging.FileFormats.Png.PngColorType.TruecolorWithAlpha;
                                                                                                                                                                                                                                                exportOptions.Source = new Aspose.Imaging.Sources.StreamSource(new System.IO.MemoryStream());

                                                                                                                                                                                                                                                // Set the manual mask.
                                                                                                                                                                                                                                                Aspose.Imaging.Masking.Options.ManualMaskingArgs args = new Aspose.Imaging.Masking.Options.ManualMaskingArgs();
                                                                                                                                                                                                                                                args.Mask = manualMask;

                                                                                                                                                                                                                                                using (RasterImage image = (RasterImage)Image.Load(dir + "Blue hills.png"))
                                                                                                                                                                                                                                                {
                                                                                                                                                                                                                                                    Aspose.Imaging.Masking.Options.MaskingOptions maskingOptions = new Aspose.Imaging.Masking.Options.MaskingOptions();

                                                                                                                                                                                                                                                    // Use manual clustering algorithm.
                                                                                                                                                                                                                                                    maskingOptions.Method = Masking.Options.SegmentationMethod.Manual;

                                                                                                                                                                                                                                                    // All shapes making up a mask will be combined into one. 
                                                                                                                                                                                                                                                    maskingOptions.Decompose = false;
                                                                                                                                                                                                                                                    maskingOptions.Args = args;

                                                                                                                                                                                                                                                    // The backgroung color will be orange.
                                                                                                                                                                                                                                                    maskingOptions.BackgroundReplacementColor = Aspose.Imaging.Color.Orange;
                                                                                                                                                                                                                                                    maskingOptions.ExportOptions = exportOptions;

                                                                                                                                                                                                                                                    // The area of the source image that masking will be applied to.
                                                                                                                                                                                                                                                    maskingOptions.MaskingArea = new Rectangle(50, 50, 120, 120);

                                                                                                                                                                                                                                                    // Create an instance of the ImageMasking class.
                                                                                                                                                                                                                                                    Aspose.Imaging.Masking.ImageMasking masking = new Aspose.Imaging.Masking.ImageMasking(image);

                                                                                                                                                                                                                                                    // Divide the source image into several clusters (segments).
                                                                                                                                                                                                                                                    using (Aspose.Imaging.Masking.Result.MaskingResult maskingResult = masking.Decompose(maskingOptions))
                                                                                                                                                                                                                                                    {
                                                                                                                                                                                                                                                        // Obtain images from masking result and save them to PNG.
                                                                                                                                                                                                                                                        for (int i = 0; i < maskingResult.Length; i++)
                                                                                                                                                                                                                                                        {
                                                                                                                                                                                                                                                            string outputFileName = string.Format("Blue hills.Segment{0}.png", maskingResult[i].ObjectNumber);
                                                                                                                                                                                                                                                            using (Aspose.Imaging.Image resultImage = maskingResult[i].GetImage())
                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                resultImage.Save(dir + outputFileName);
                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                }

Ten przykład pokazuje, jak określić sugestie dla algorytmu ukrywania obrazu w celu poprawy precyzji metody segmentacji (klastrowania).

string dir = "c:\\temp\\";

                                                                                                                                                                                                                                                                     using (Aspose.Imaging.RasterImage image = (Aspose.Imaging.RasterImage)Aspose.Imaging.Image.Load(dir + "Gorilla.bmp"))
                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                         Aspose.Imaging.Masking.Options.AutoMaskingArgs args = new Aspose.Imaging.Masking.Options.AutoMaskingArgs();

                                                                                                                                                                                                                                                                         // Suggestion #1.
                                                                                                                                                                                                                                                                         // Analyze the image visually and set the area of interest. The result of segmentation will include only objects that will be completely located within this area.
                                                                                                                                                                                                                                                                         args.ObjectsRectangles = new Rectangle[]
                                                                                                                                                                                                                                                                         {
                                                                                                                                                                                                                                                                             new Rectangle(86, 6, 270, 364),
                                                                                                                                                                                                                                                                         };

                                                                                                                                                                                                                                                                         // Suggestion #2.
                                                                                                                                                                                                                                                                         // Analyze the image visually and set the points that belong to separated objects.
                                                                                                                                                                                                                                                                         args.ObjectsPoints = new Point[][]
                                                                                                                                                                                                                                                                         {
                                                                                                                                                                                                                                                                             new Point[] { new Point(103, 326) },
                                                                                                                                                                                                                                                                             new Point[] { new Point(280, 43) },
                                                                                                                                                                                                                                                                             new Point[] { new Point(319, 86) },
                                                                                                                                                                                                                                                                         };

                                                                                                                                                                                                                                                                         // Each cluster (segment) will be stored to a separate PNG file.
                                                                                                                                                                                                                                                                         Aspose.Imaging.ImageOptions.PngOptions exportOptions = new Aspose.Imaging.ImageOptions.PngOptions();
                                                                                                                                                                                                                                                                         exportOptions.ColorType = Aspose.Imaging.FileFormats.Png.PngColorType.TruecolorWithAlpha;
                                                                                                                                                                                                                                                                         exportOptions.Source = new Aspose.Imaging.Sources.StreamSource(new System.IO.MemoryStream());

                                                                                                                                                                                                                                                                         Aspose.Imaging.Masking.Options.MaskingOptions maskingOptions = new Aspose.Imaging.Masking.Options.MaskingOptions();

                                                                                                                                                                                                                                                                         // Use GraphCut clustering.
                                                                                                                                                                                                                                                                         maskingOptions.Method = Masking.Options.SegmentationMethod.GraphCut;
                                                                                                                                                                                                                                                                         maskingOptions.Decompose = false;
                                                                                                                                                                                                                                                                         maskingOptions.Args = args;

                                                                                                                                                                                                                                                                         // The backgroung color will be orange.
                                                                                                                                                                                                                                                                         maskingOptions.BackgroundReplacementColor = Aspose.Imaging.Color.Orange;
                                                                                                                                                                                                                                                                         maskingOptions.ExportOptions = exportOptions;

                                                                                                                                                                                                                                                                         // Create an instance of the ImageMasking class.
                                                                                                                                                                                                                                                                         Aspose.Imaging.Masking.ImageMasking masking = new Aspose.Imaging.Masking.ImageMasking(image);

                                                                                                                                                                                                                                                                         // Divide the source image into several clusters (segments).
                                                                                                                                                                                                                                                                         using (Aspose.Imaging.Masking.Result.MaskingResult maskingResult = masking.Decompose(maskingOptions))
                                                                                                                                                                                                                                                                         {
                                                                                                                                                                                                                                                                             // Obtain images from masking result and save them to PNG.
                                                                                                                                                                                                                                                                             for (int i = 0; i < maskingResult.Length; i++)
                                                                                                                                                                                                                                                                             {
                                                                                                                                                                                                                                                                                 string outputFileName = string.Format("Gorilla.Segment{0}.png", maskingResult[i].ObjectNumber);
                                                                                                                                                                                                                                                                                 using (Aspose.Imaging.Image resultImage = maskingResult[i].GetImage())
                                                                                                                                                                                                                                                                                 {
                                                                                                                                                                                                                                                                                     resultImage.Save(dir + outputFileName);
                                                                                                                                                                                                                                                                                 }
                                                                                                                                                                                                                                                                             }
                                                                                                                                                                                                                                                                         }
                                                                                                                                                                                                                                                                     }

Oszczędzanie sesji masku do pliku na długie sesje, a także możliwość ponownego odtwarzania w innym środowisku.

string dir = "c:\\temp\\";
                                                                                                                                                 string sessionBackupFile = dir + "session.bak";

                                                                                                                                                 // Masking export options
                                                                                                                                                 Aspose.Imaging.ImageOptions.PngOptions exportOptions = new Aspose.Imaging.ImageOptions.PngOptions();
                                                                                                                                                 exportOptions.ColorType = Aspose.Imaging.FileFormats.Png.PngColorType.TruecolorWithAlpha;
                                                                                                                                                 exportOptions.Source = new Aspose.Imaging.Sources.StreamSource(new System.IO.MemoryStream());

                                                                                                                                                 Aspose.Imaging.Masking.Options.MaskingOptions maskingOptions = new Aspose.Imaging.Masking.Options.MaskingOptions();

                                                                                                                                                 // Use GraphCut clustering.
                                                                                                                                                 maskingOptions.Method = Masking.Options.SegmentationMethod.GraphCut;
                                                                                                                                                 maskingOptions.Decompose = false;
                                                                                                                                                 maskingOptions.Args = new Aspose.Imaging.Masking.Options.AutoMaskingArgs();

                                                                                                                                                 // The backgroung color will be orange.
                                                                                                                                                 maskingOptions.BackgroundReplacementColor = Aspose.Imaging.Color.Orange;
                                                                                                                                                 maskingOptions.ExportOptions = exportOptions;

                                                                                                                                                 // Starting a session for the first time and saving to a file
                                                                                                                                                 using (Aspose.Imaging.RasterImage image = (Aspose.Imaging.RasterImage)Aspose.Imaging.Image.Load(dir + "Gorilla.bmp"))
                                                                                                                                                 {
                                                                                                                                                     // Create an instance of the ImageMasking class.
                                                                                                                                                     Aspose.Imaging.Masking.ImageMasking masking = new Aspose.Imaging.Masking.ImageMasking(image);

                                                                                                                                                     using (Aspose.Imaging.Masking.IMaskingSession session = masking.CreateSession(maskingOptions))
                                                                                                                                                     {
                                                                                                                                                         using (Aspose.Imaging.Masking.Result.MaskingResult maskingResult = session.Decompose())
                                                                                                                                                         {
                                                                                                                                                             using (Aspose.Imaging.RasterImage segmentImage = maskingResult[1].GetImage())
                                                                                                                                                             {
                                                                                                                                                                 segmentImage.Save(dir + "step1.png");
                                                                                                                                                             }
                                                                                                                                                         }

                                                                                                                                                         session.Save(sessionBackupFile);
                                                                                                                                                     }
                                                                                                                                                 }

                                                                                                                                                 // Resuming a masking session from a file
                                                                                                                                                 using (Aspose.Imaging.RasterImage image = (Aspose.Imaging.RasterImage)Aspose.Imaging.Image.Load(dir + "Gorilla.bmp"))
                                                                                                                                                 {
                                                                                                                                                     // Create an instance of the ImageMasking class.
                                                                                                                                                     Aspose.Imaging.Masking.ImageMasking masking = new Aspose.Imaging.Masking.ImageMasking(image);

                                                                                                                                                     using (Aspose.Imaging.Masking.IMaskingSession session = masking.LoadSession(sessionBackupFile))
                                                                                                                                                     {
                                                                                                                                                         Aspose.Imaging.Masking.Options.AutoMaskingArgs args = new Aspose.Imaging.Masking.Options.AutoMaskingArgs();

                                                                                                                                                         // Analyze the image visually and set the points that belong to separated objects.
                                                                                                                                                         args.ObjectsPoints = new Point[][]
                                                                                                                                                                                      {
                                                                                                                                                                                          new Point[]
                                                                                                                                                                                              {
                                                                                                                                                                                                  new Point(0, 0), new Point(0, 1), new Point(1, 0),
                                                                                                                                                                                                  new Point(1, 1), new Point(2, 0), new Point(2, 1),
                                                                                                                                                                                                  new Point(3, 0), new Point(3, 1)
                                                                                                                                                                                              },
                                                                                                                                                                                      };
                                                                                                                                                         using (Aspose.Imaging.Masking.Result.MaskingResult maskingResult = session.ImproveDecomposition(args))
                                                                                                                                                         {
                                                                                                                                                             // Explicit transfer of export options, since it is not serializable
                                                                                                                                                             maskingResult.MaskingOptions.ExportOptions = exportOptions;

                                                                                                                                                             using (Aspose.Imaging.RasterImage segmentImage = maskingResult[1].GetImage())
                                                                                                                                                             {
                                                                                                                                                                 segmentImage.Save(dir + "step2.png");
                                                                                                                                                             }
                                                                                                                                                         }
                                                                                                                                                     }
                                                                                                                                                 }

BackgroundReplacementColor

Otrzymuje lub ustawia kolor wymiany tła.

public Color BackgroundReplacementColor { get; set; }

Wartość nieruchomości

Color

Examples

Ten przykład pokazuje, jak rozdzielić obraz rasterowy na kilka obrazów za pomocą maski obrazu i algorytmu segmentacji K-means.

string dir = "c:\\temp\\";

                                                                                                                                                                                                                                                                     using (Aspose.Imaging.RasterImage image = (Aspose.Imaging.RasterImage)Aspose.Imaging.Image.Load(dir + "Blue hills.png"))
                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                         Aspose.Imaging.Masking.Options.AutoMaskingArgs args = new Aspose.Imaging.Masking.Options.AutoMaskingArgs();

                                                                                                                                                                                                                                                                         // Set the number of clusters (separated objects). The default value is 2, the foreground object and the background.
                                                                                                                                                                                                                                                                         args.NumberOfObjects = 3;

                                                                                                                                                                                                                                                                         // Set the maximum number of iterations.
                                                                                                                                                                                                                                                                         args.MaxIterationNumber = 50;

                                                                                                                                                                                                                                                                         // Set the precision of segmentation method (optional)
                                                                                                                                                                                                                                                                         args.Precision = 1;

                                                                                                                                                                                                                                                                         // Each cluster (segment) will be stored to a separate PNG file.
                                                                                                                                                                                                                                                                         Aspose.Imaging.ImageOptions.PngOptions exportOptions = new Aspose.Imaging.ImageOptions.PngOptions();
                                                                                                                                                                                                                                                                         exportOptions.ColorType = Aspose.Imaging.FileFormats.Png.PngColorType.TruecolorWithAlpha;
                                                                                                                                                                                                                                                                         exportOptions.Source = new Aspose.Imaging.Sources.StreamSource(new System.IO.MemoryStream());

                                                                                                                                                                                                                                                                         Aspose.Imaging.Masking.Options.MaskingOptions maskingOptions = new Aspose.Imaging.Masking.Options.MaskingOptions();

                                                                                                                                                                                                                                                                         // Use K-means clustering.
                                                                                                                                                                                                                                                                         // K-means clustering allows to split image into several independent clusters (segments).
                                                                                                                                                                                                                                                                         maskingOptions.Method = Masking.Options.SegmentationMethod.KMeans;
                                                                                                                                                                                                                                                                         maskingOptions.Decompose = true;
                                                                                                                                                                                                                                                                         maskingOptions.Args = args;

                                                                                                                                                                                                                                                                         // The backgroung color will be orange.
                                                                                                                                                                                                                                                                         maskingOptions.BackgroundReplacementColor = Aspose.Imaging.Color.Orange;
                                                                                                                                                                                                                                                                         maskingOptions.ExportOptions = exportOptions;

                                                                                                                                                                                                                                                                         // Create an instance of the ImageMasking class.
                                                                                                                                                                                                                                                                         Aspose.Imaging.Masking.ImageMasking masking = new Aspose.Imaging.Masking.ImageMasking(image);

                                                                                                                                                                                                                                                                         // Divide the source image into several clusters (segments).
                                                                                                                                                                                                                                                                         using (Aspose.Imaging.Masking.Result.MaskingResult maskingResult = masking.Decompose(maskingOptions))
                                                                                                                                                                                                                                                                         {
                                                                                                                                                                                                                                                                             // Obtain images from masking result and save them to PNG.
                                                                                                                                                                                                                                                                             for (int i = 0; i < maskingResult.Length; i++)
                                                                                                                                                                                                                                                                             {
                                                                                                                                                                                                                                                                                 string outputFileName = string.Format("Blue hills.Segment{0}.png", maskingResult[i].ObjectNumber);
                                                                                                                                                                                                                                                                                 using (Aspose.Imaging.Image resultImage = maskingResult[i].GetImage())
                                                                                                                                                                                                                                                                                 {
                                                                                                                                                                                                                                                                                     resultImage.Save(dir + outputFileName);
                                                                                                                                                                                                                                                                                 }
                                                                                                                                                                                                                                                                             }
                                                                                                                                                                                                                                                                         }
                                                                                                                                                                                                                                                                     }

Używanie maski segmentowej do przyspieszenia procesu segmentacji

// Masking export options
                                                                    Aspose.Imaging.ImageOptions.PngOptions exportOptions = new Aspose.Imaging.ImageOptions.PngOptions();
                                                                    exportOptions.ColorType = Aspose.Imaging.FileFormats.Png.PngColorType.TruecolorWithAlpha;
                                                                    exportOptions.Source = new Aspose.Imaging.Sources.StreamSource(new System.IO.MemoryStream());

                                                                    Aspose.Imaging.Masking.Options.MaskingOptions maskingOptions = new Aspose.Imaging.Masking.Options.MaskingOptions();

                                                                    // Use GraphCut clustering.
                                                                    maskingOptions.Method = Masking.Options.SegmentationMethod.GraphCut;
                                                                    maskingOptions.Decompose = false;
                                                                    maskingOptions.Args = new Aspose.Imaging.Masking.Options.AutoMaskingArgs();

                                                                    // The backgroung color will be transparent.
                                                                    maskingOptions.BackgroundReplacementColor = Aspose.Imaging.Color.Transparent;
                                                                    maskingOptions.ExportOptions = exportOptions;

                                                                    string dir = "c:\\temp\\";
                                                                    using (Aspose.Imaging.RasterImage image = (Aspose.Imaging.RasterImage)Aspose.Imaging.Image.Load(dir + "BigImage.jpg"))
                                                                    {
                                                                        Aspose.Imaging.Size imageSize = image.Size;

                                                                        // Reducing image size to speed up the segmentation process
                                                                        image.ResizeHeightProportionally(600, Aspose.Imaging.ResizeType.HighQualityResample);

                                                                        // Create an instance of the ImageMasking class.
                                                                        Aspose.Imaging.Masking.ImageMasking masking = new Aspose.Imaging.Masking.ImageMasking(image);

                                                                        // Divide the source image into several clusters (segments).
                                                                        using (Aspose.Imaging.Masking.Result.MaskingResult maskingResult = masking.Decompose(maskingOptions))
                                                                        {
                                                                            // Getting the foreground mask
                                                                            using (Aspose.Imaging.RasterImage foregroundMask = maskingResult[1].GetMask()) 
                                                                            {
                                                                                // Increase the size of the mask to the size of the original image
                                                                                foregroundMask.Resize(imageSize.Width, imageSize.Height, Aspose.Imaging.ResizeType.NearestNeighbourResample);

                                                                                // Applying the mask to the original image to obtain a foreground segment
                                                                                using (Aspose.Imaging.RasterImage originImage = (Aspose.Imaging.RasterImage)Aspose.Imaging.Image.Load(dir + "BigImage.jpg"))
                                                                                {
                                                                                    Aspose.Imaging.Masking.ImageMasking.ApplyMask(originImage, foregroundMask, maskingOptions);
                                                                                    originImage.Save(dir + "BigImage_foreground.png", exportOptions);
                                                                                }
                                                                            }
                                                                        }
                                                                    }

Ten przykład pokazuje, jak rozdzielić obraz rasterowy na kilka obrazów za pomocą maski obrazu i ręcznie. maska obraza jest techniką przetwarzania obrazy, która jest używana do podziału tła z przedmiotów obrazowych z przodu.

string dir = "c:\\temp\\";

                                                                                                                                                                                                                                                // Define a manual mask.
                                                                                                                                                                                                                                                Aspose.Imaging.GraphicsPath manualMask = new Aspose.Imaging.GraphicsPath();
                                                                                                                                                                                                                                                Aspose.Imaging.Figure figure = new Aspose.Imaging.Figure();
                                                                                                                                                                                                                                                figure.AddShape(new Aspose.Imaging.Shapes.EllipseShape(new RectangleF(50, 50, 40, 40)));
                                                                                                                                                                                                                                                figure.AddShape(new Aspose.Imaging.Shapes.RectangleShape(new RectangleF(10, 20, 50, 30)));
                                                                                                                                                                                                                                                manualMask.AddFigure(figure);

                                                                                                                                                                                                                                                // Each cluster (segment) will be stored to a separate PNG file.
                                                                                                                                                                                                                                                Aspose.Imaging.ImageOptions.PngOptions exportOptions = new Aspose.Imaging.ImageOptions.PngOptions();
                                                                                                                                                                                                                                                exportOptions.ColorType = Aspose.Imaging.FileFormats.Png.PngColorType.TruecolorWithAlpha;
                                                                                                                                                                                                                                                exportOptions.Source = new Aspose.Imaging.Sources.StreamSource(new System.IO.MemoryStream());

                                                                                                                                                                                                                                                // Set the manual mask.
                                                                                                                                                                                                                                                Aspose.Imaging.Masking.Options.ManualMaskingArgs args = new Aspose.Imaging.Masking.Options.ManualMaskingArgs();
                                                                                                                                                                                                                                                args.Mask = manualMask;

                                                                                                                                                                                                                                                using (RasterImage image = (RasterImage)Image.Load(dir + "Blue hills.png"))
                                                                                                                                                                                                                                                {
                                                                                                                                                                                                                                                    Aspose.Imaging.Masking.Options.MaskingOptions maskingOptions = new Aspose.Imaging.Masking.Options.MaskingOptions();

                                                                                                                                                                                                                                                    // Use manual clustering algorithm.
                                                                                                                                                                                                                                                    maskingOptions.Method = Masking.Options.SegmentationMethod.Manual;

                                                                                                                                                                                                                                                    // All shapes making up a mask will be combined into one. 
                                                                                                                                                                                                                                                    maskingOptions.Decompose = false;
                                                                                                                                                                                                                                                    maskingOptions.Args = args;

                                                                                                                                                                                                                                                    // The backgroung color will be orange.
                                                                                                                                                                                                                                                    maskingOptions.BackgroundReplacementColor = Aspose.Imaging.Color.Orange;
                                                                                                                                                                                                                                                    maskingOptions.ExportOptions = exportOptions;

                                                                                                                                                                                                                                                    // The area of the source image that masking will be applied to.
                                                                                                                                                                                                                                                    maskingOptions.MaskingArea = new Rectangle(50, 50, 120, 120);

                                                                                                                                                                                                                                                    // Create an instance of the ImageMasking class.
                                                                                                                                                                                                                                                    Aspose.Imaging.Masking.ImageMasking masking = new Aspose.Imaging.Masking.ImageMasking(image);

                                                                                                                                                                                                                                                    // Divide the source image into several clusters (segments).
                                                                                                                                                                                                                                                    using (Aspose.Imaging.Masking.Result.MaskingResult maskingResult = masking.Decompose(maskingOptions))
                                                                                                                                                                                                                                                    {
                                                                                                                                                                                                                                                        // Obtain images from masking result and save them to PNG.
                                                                                                                                                                                                                                                        for (int i = 0; i < maskingResult.Length; i++)
                                                                                                                                                                                                                                                        {
                                                                                                                                                                                                                                                            string outputFileName = string.Format("Blue hills.Segment{0}.png", maskingResult[i].ObjectNumber);
                                                                                                                                                                                                                                                            using (Aspose.Imaging.Image resultImage = maskingResult[i].GetImage())
                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                resultImage.Save(dir + outputFileName);
                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                }

Ten przykład pokazuje, jak określić sugestie dla algorytmu ukrywania obrazu w celu poprawy precyzji metody segmentacji (klastrowania).

string dir = "c:\\temp\\";

                                                                                                                                                                                                                                                                     using (Aspose.Imaging.RasterImage image = (Aspose.Imaging.RasterImage)Aspose.Imaging.Image.Load(dir + "Gorilla.bmp"))
                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                         Aspose.Imaging.Masking.Options.AutoMaskingArgs args = new Aspose.Imaging.Masking.Options.AutoMaskingArgs();

                                                                                                                                                                                                                                                                         // Suggestion #1.
                                                                                                                                                                                                                                                                         // Analyze the image visually and set the area of interest. The result of segmentation will include only objects that will be completely located within this area.
                                                                                                                                                                                                                                                                         args.ObjectsRectangles = new Rectangle[]
                                                                                                                                                                                                                                                                         {
                                                                                                                                                                                                                                                                             new Rectangle(86, 6, 270, 364),
                                                                                                                                                                                                                                                                         };

                                                                                                                                                                                                                                                                         // Suggestion #2.
                                                                                                                                                                                                                                                                         // Analyze the image visually and set the points that belong to separated objects.
                                                                                                                                                                                                                                                                         args.ObjectsPoints = new Point[][]
                                                                                                                                                                                                                                                                         {
                                                                                                                                                                                                                                                                             new Point[] { new Point(103, 326) },
                                                                                                                                                                                                                                                                             new Point[] { new Point(280, 43) },
                                                                                                                                                                                                                                                                             new Point[] { new Point(319, 86) },
                                                                                                                                                                                                                                                                         };

                                                                                                                                                                                                                                                                         // Each cluster (segment) will be stored to a separate PNG file.
                                                                                                                                                                                                                                                                         Aspose.Imaging.ImageOptions.PngOptions exportOptions = new Aspose.Imaging.ImageOptions.PngOptions();
                                                                                                                                                                                                                                                                         exportOptions.ColorType = Aspose.Imaging.FileFormats.Png.PngColorType.TruecolorWithAlpha;
                                                                                                                                                                                                                                                                         exportOptions.Source = new Aspose.Imaging.Sources.StreamSource(new System.IO.MemoryStream());

                                                                                                                                                                                                                                                                         Aspose.Imaging.Masking.Options.MaskingOptions maskingOptions = new Aspose.Imaging.Masking.Options.MaskingOptions();

                                                                                                                                                                                                                                                                         // Use GraphCut clustering.
                                                                                                                                                                                                                                                                         maskingOptions.Method = Masking.Options.SegmentationMethod.GraphCut;
                                                                                                                                                                                                                                                                         maskingOptions.Decompose = false;
                                                                                                                                                                                                                                                                         maskingOptions.Args = args;

                                                                                                                                                                                                                                                                         // The backgroung color will be orange.
                                                                                                                                                                                                                                                                         maskingOptions.BackgroundReplacementColor = Aspose.Imaging.Color.Orange;
                                                                                                                                                                                                                                                                         maskingOptions.ExportOptions = exportOptions;

                                                                                                                                                                                                                                                                         // Create an instance of the ImageMasking class.
                                                                                                                                                                                                                                                                         Aspose.Imaging.Masking.ImageMasking masking = new Aspose.Imaging.Masking.ImageMasking(image);

                                                                                                                                                                                                                                                                         // Divide the source image into several clusters (segments).
                                                                                                                                                                                                                                                                         using (Aspose.Imaging.Masking.Result.MaskingResult maskingResult = masking.Decompose(maskingOptions))
                                                                                                                                                                                                                                                                         {
                                                                                                                                                                                                                                                                             // Obtain images from masking result and save them to PNG.
                                                                                                                                                                                                                                                                             for (int i = 0; i < maskingResult.Length; i++)
                                                                                                                                                                                                                                                                             {
                                                                                                                                                                                                                                                                                 string outputFileName = string.Format("Gorilla.Segment{0}.png", maskingResult[i].ObjectNumber);
                                                                                                                                                                                                                                                                                 using (Aspose.Imaging.Image resultImage = maskingResult[i].GetImage())
                                                                                                                                                                                                                                                                                 {
                                                                                                                                                                                                                                                                                     resultImage.Save(dir + outputFileName);
                                                                                                                                                                                                                                                                                 }
                                                                                                                                                                                                                                                                             }
                                                                                                                                                                                                                                                                         }
                                                                                                                                                                                                                                                                     }

Oszczędzanie sesji masku do pliku na długie sesje, a także możliwość ponownego odtwarzania w innym środowisku.

string dir = "c:\\temp\\";
                                                                                                                                                 string sessionBackupFile = dir + "session.bak";

                                                                                                                                                 // Masking export options
                                                                                                                                                 Aspose.Imaging.ImageOptions.PngOptions exportOptions = new Aspose.Imaging.ImageOptions.PngOptions();
                                                                                                                                                 exportOptions.ColorType = Aspose.Imaging.FileFormats.Png.PngColorType.TruecolorWithAlpha;
                                                                                                                                                 exportOptions.Source = new Aspose.Imaging.Sources.StreamSource(new System.IO.MemoryStream());

                                                                                                                                                 Aspose.Imaging.Masking.Options.MaskingOptions maskingOptions = new Aspose.Imaging.Masking.Options.MaskingOptions();

                                                                                                                                                 // Use GraphCut clustering.
                                                                                                                                                 maskingOptions.Method = Masking.Options.SegmentationMethod.GraphCut;
                                                                                                                                                 maskingOptions.Decompose = false;
                                                                                                                                                 maskingOptions.Args = new Aspose.Imaging.Masking.Options.AutoMaskingArgs();

                                                                                                                                                 // The backgroung color will be orange.
                                                                                                                                                 maskingOptions.BackgroundReplacementColor = Aspose.Imaging.Color.Orange;
                                                                                                                                                 maskingOptions.ExportOptions = exportOptions;

                                                                                                                                                 // Starting a session for the first time and saving to a file
                                                                                                                                                 using (Aspose.Imaging.RasterImage image = (Aspose.Imaging.RasterImage)Aspose.Imaging.Image.Load(dir + "Gorilla.bmp"))
                                                                                                                                                 {
                                                                                                                                                     // Create an instance of the ImageMasking class.
                                                                                                                                                     Aspose.Imaging.Masking.ImageMasking masking = new Aspose.Imaging.Masking.ImageMasking(image);

                                                                                                                                                     using (Aspose.Imaging.Masking.IMaskingSession session = masking.CreateSession(maskingOptions))
                                                                                                                                                     {
                                                                                                                                                         using (Aspose.Imaging.Masking.Result.MaskingResult maskingResult = session.Decompose())
                                                                                                                                                         {
                                                                                                                                                             using (Aspose.Imaging.RasterImage segmentImage = maskingResult[1].GetImage())
                                                                                                                                                             {
                                                                                                                                                                 segmentImage.Save(dir + "step1.png");
                                                                                                                                                             }
                                                                                                                                                         }

                                                                                                                                                         session.Save(sessionBackupFile);
                                                                                                                                                     }
                                                                                                                                                 }

                                                                                                                                                 // Resuming a masking session from a file
                                                                                                                                                 using (Aspose.Imaging.RasterImage image = (Aspose.Imaging.RasterImage)Aspose.Imaging.Image.Load(dir + "Gorilla.bmp"))
                                                                                                                                                 {
                                                                                                                                                     // Create an instance of the ImageMasking class.
                                                                                                                                                     Aspose.Imaging.Masking.ImageMasking masking = new Aspose.Imaging.Masking.ImageMasking(image);

                                                                                                                                                     using (Aspose.Imaging.Masking.IMaskingSession session = masking.LoadSession(sessionBackupFile))
                                                                                                                                                     {
                                                                                                                                                         Aspose.Imaging.Masking.Options.AutoMaskingArgs args = new Aspose.Imaging.Masking.Options.AutoMaskingArgs();

                                                                                                                                                         // Analyze the image visually and set the points that belong to separated objects.
                                                                                                                                                         args.ObjectsPoints = new Point[][]
                                                                                                                                                                                      {
                                                                                                                                                                                          new Point[]
                                                                                                                                                                                              {
                                                                                                                                                                                                  new Point(0, 0), new Point(0, 1), new Point(1, 0),
                                                                                                                                                                                                  new Point(1, 1), new Point(2, 0), new Point(2, 1),
                                                                                                                                                                                                  new Point(3, 0), new Point(3, 1)
                                                                                                                                                                                              },
                                                                                                                                                                                      };
                                                                                                                                                         using (Aspose.Imaging.Masking.Result.MaskingResult maskingResult = session.ImproveDecomposition(args))
                                                                                                                                                         {
                                                                                                                                                             // Explicit transfer of export options, since it is not serializable
                                                                                                                                                             maskingResult.MaskingOptions.ExportOptions = exportOptions;

                                                                                                                                                             using (Aspose.Imaging.RasterImage segmentImage = maskingResult[1].GetImage())
                                                                                                                                                             {
                                                                                                                                                                 segmentImage.Save(dir + "step2.png");
                                                                                                                                                             }
                                                                                                                                                         }
                                                                                                                                                     }
                                                                                                                                                 }

Decompose

otrzymuje lub ustawia wartość wskazującą, czyBez konieczności oddzielenia każdej formy od maski jako pojedynczego obiektu lub jako zjednoczony obiekt od mask odrębnych od tła.

public bool Decompose { get; set; }

Wartość nieruchomości

bool

Examples

Ten przykład pokazuje, jak rozdzielić obraz rasterowy na kilka obrazów za pomocą maski obrazu i ręcznie. maska obraza jest techniką przetwarzania obrazy, która jest używana do podziału tła z przedmiotów obrazowych z przodu.

string dir = "c:\\temp\\";

                                                                                                                                                                                                                                                // Define a manual mask.
                                                                                                                                                                                                                                                Aspose.Imaging.GraphicsPath manualMask = new Aspose.Imaging.GraphicsPath();
                                                                                                                                                                                                                                                Aspose.Imaging.Figure figure = new Aspose.Imaging.Figure();
                                                                                                                                                                                                                                                figure.AddShape(new Aspose.Imaging.Shapes.EllipseShape(new RectangleF(50, 50, 40, 40)));
                                                                                                                                                                                                                                                figure.AddShape(new Aspose.Imaging.Shapes.RectangleShape(new RectangleF(10, 20, 50, 30)));
                                                                                                                                                                                                                                                manualMask.AddFigure(figure);

                                                                                                                                                                                                                                                // Each cluster (segment) will be stored to a separate PNG file.
                                                                                                                                                                                                                                                Aspose.Imaging.ImageOptions.PngOptions exportOptions = new Aspose.Imaging.ImageOptions.PngOptions();
                                                                                                                                                                                                                                                exportOptions.ColorType = Aspose.Imaging.FileFormats.Png.PngColorType.TruecolorWithAlpha;
                                                                                                                                                                                                                                                exportOptions.Source = new Aspose.Imaging.Sources.StreamSource(new System.IO.MemoryStream());

                                                                                                                                                                                                                                                // Set the manual mask.
                                                                                                                                                                                                                                                Aspose.Imaging.Masking.Options.ManualMaskingArgs args = new Aspose.Imaging.Masking.Options.ManualMaskingArgs();
                                                                                                                                                                                                                                                args.Mask = manualMask;

                                                                                                                                                                                                                                                using (RasterImage image = (RasterImage)Image.Load(dir + "Blue hills.png"))
                                                                                                                                                                                                                                                {
                                                                                                                                                                                                                                                    Aspose.Imaging.Masking.Options.MaskingOptions maskingOptions = new Aspose.Imaging.Masking.Options.MaskingOptions();

                                                                                                                                                                                                                                                    // Use manual clustering algorithm.
                                                                                                                                                                                                                                                    maskingOptions.Method = Masking.Options.SegmentationMethod.Manual;

                                                                                                                                                                                                                                                    // All shapes making up a mask will be combined into one. 
                                                                                                                                                                                                                                                    maskingOptions.Decompose = false;
                                                                                                                                                                                                                                                    maskingOptions.Args = args;

                                                                                                                                                                                                                                                    // The backgroung color will be orange.
                                                                                                                                                                                                                                                    maskingOptions.BackgroundReplacementColor = Aspose.Imaging.Color.Orange;
                                                                                                                                                                                                                                                    maskingOptions.ExportOptions = exportOptions;

                                                                                                                                                                                                                                                    // The area of the source image that masking will be applied to.
                                                                                                                                                                                                                                                    maskingOptions.MaskingArea = new Rectangle(50, 50, 120, 120);

                                                                                                                                                                                                                                                    // Create an instance of the ImageMasking class.
                                                                                                                                                                                                                                                    Aspose.Imaging.Masking.ImageMasking masking = new Aspose.Imaging.Masking.ImageMasking(image);

                                                                                                                                                                                                                                                    // Divide the source image into several clusters (segments).
                                                                                                                                                                                                                                                    using (Aspose.Imaging.Masking.Result.MaskingResult maskingResult = masking.Decompose(maskingOptions))
                                                                                                                                                                                                                                                    {
                                                                                                                                                                                                                                                        // Obtain images from masking result and save them to PNG.
                                                                                                                                                                                                                                                        for (int i = 0; i < maskingResult.Length; i++)
                                                                                                                                                                                                                                                        {
                                                                                                                                                                                                                                                            string outputFileName = string.Format("Blue hills.Segment{0}.png", maskingResult[i].ObjectNumber);
                                                                                                                                                                                                                                                            using (Aspose.Imaging.Image resultImage = maskingResult[i].GetImage())
                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                resultImage.Save(dir + outputFileName);
                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                }

ExportOptions

Uzyskuje lub ustawia opcje eksportu obrazu.

public ImageOptionsBase ExportOptions { get; set; }

Wartość nieruchomości

ImageOptionsBase

Examples

Ten przykład pokazuje, jak rozdzielić obraz rasterowy na kilka obrazów za pomocą maski obrazu i algorytmu segmentacji K-means.

string dir = "c:\\temp\\";

                                                                                                                                                                                                                                                                     using (Aspose.Imaging.RasterImage image = (Aspose.Imaging.RasterImage)Aspose.Imaging.Image.Load(dir + "Blue hills.png"))
                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                         Aspose.Imaging.Masking.Options.AutoMaskingArgs args = new Aspose.Imaging.Masking.Options.AutoMaskingArgs();

                                                                                                                                                                                                                                                                         // Set the number of clusters (separated objects). The default value is 2, the foreground object and the background.
                                                                                                                                                                                                                                                                         args.NumberOfObjects = 3;

                                                                                                                                                                                                                                                                         // Set the maximum number of iterations.
                                                                                                                                                                                                                                                                         args.MaxIterationNumber = 50;

                                                                                                                                                                                                                                                                         // Set the precision of segmentation method (optional)
                                                                                                                                                                                                                                                                         args.Precision = 1;

                                                                                                                                                                                                                                                                         // Each cluster (segment) will be stored to a separate PNG file.
                                                                                                                                                                                                                                                                         Aspose.Imaging.ImageOptions.PngOptions exportOptions = new Aspose.Imaging.ImageOptions.PngOptions();
                                                                                                                                                                                                                                                                         exportOptions.ColorType = Aspose.Imaging.FileFormats.Png.PngColorType.TruecolorWithAlpha;
                                                                                                                                                                                                                                                                         exportOptions.Source = new Aspose.Imaging.Sources.StreamSource(new System.IO.MemoryStream());

                                                                                                                                                                                                                                                                         Aspose.Imaging.Masking.Options.MaskingOptions maskingOptions = new Aspose.Imaging.Masking.Options.MaskingOptions();

                                                                                                                                                                                                                                                                         // Use K-means clustering.
                                                                                                                                                                                                                                                                         // K-means clustering allows to split image into several independent clusters (segments).
                                                                                                                                                                                                                                                                         maskingOptions.Method = Masking.Options.SegmentationMethod.KMeans;
                                                                                                                                                                                                                                                                         maskingOptions.Decompose = true;
                                                                                                                                                                                                                                                                         maskingOptions.Args = args;

                                                                                                                                                                                                                                                                         // The backgroung color will be orange.
                                                                                                                                                                                                                                                                         maskingOptions.BackgroundReplacementColor = Aspose.Imaging.Color.Orange;
                                                                                                                                                                                                                                                                         maskingOptions.ExportOptions = exportOptions;

                                                                                                                                                                                                                                                                         // Create an instance of the ImageMasking class.
                                                                                                                                                                                                                                                                         Aspose.Imaging.Masking.ImageMasking masking = new Aspose.Imaging.Masking.ImageMasking(image);

                                                                                                                                                                                                                                                                         // Divide the source image into several clusters (segments).
                                                                                                                                                                                                                                                                         using (Aspose.Imaging.Masking.Result.MaskingResult maskingResult = masking.Decompose(maskingOptions))
                                                                                                                                                                                                                                                                         {
                                                                                                                                                                                                                                                                             // Obtain images from masking result and save them to PNG.
                                                                                                                                                                                                                                                                             for (int i = 0; i < maskingResult.Length; i++)
                                                                                                                                                                                                                                                                             {
                                                                                                                                                                                                                                                                                 string outputFileName = string.Format("Blue hills.Segment{0}.png", maskingResult[i].ObjectNumber);
                                                                                                                                                                                                                                                                                 using (Aspose.Imaging.Image resultImage = maskingResult[i].GetImage())
                                                                                                                                                                                                                                                                                 {
                                                                                                                                                                                                                                                                                     resultImage.Save(dir + outputFileName);
                                                                                                                                                                                                                                                                                 }
                                                                                                                                                                                                                                                                             }
                                                                                                                                                                                                                                                                         }
                                                                                                                                                                                                                                                                     }

Używanie maski segmentowej do przyspieszenia procesu segmentacji

// Masking export options
                                                                    Aspose.Imaging.ImageOptions.PngOptions exportOptions = new Aspose.Imaging.ImageOptions.PngOptions();
                                                                    exportOptions.ColorType = Aspose.Imaging.FileFormats.Png.PngColorType.TruecolorWithAlpha;
                                                                    exportOptions.Source = new Aspose.Imaging.Sources.StreamSource(new System.IO.MemoryStream());

                                                                    Aspose.Imaging.Masking.Options.MaskingOptions maskingOptions = new Aspose.Imaging.Masking.Options.MaskingOptions();

                                                                    // Use GraphCut clustering.
                                                                    maskingOptions.Method = Masking.Options.SegmentationMethod.GraphCut;
                                                                    maskingOptions.Decompose = false;
                                                                    maskingOptions.Args = new Aspose.Imaging.Masking.Options.AutoMaskingArgs();

                                                                    // The backgroung color will be transparent.
                                                                    maskingOptions.BackgroundReplacementColor = Aspose.Imaging.Color.Transparent;
                                                                    maskingOptions.ExportOptions = exportOptions;

                                                                    string dir = "c:\\temp\\";
                                                                    using (Aspose.Imaging.RasterImage image = (Aspose.Imaging.RasterImage)Aspose.Imaging.Image.Load(dir + "BigImage.jpg"))
                                                                    {
                                                                        Aspose.Imaging.Size imageSize = image.Size;

                                                                        // Reducing image size to speed up the segmentation process
                                                                        image.ResizeHeightProportionally(600, Aspose.Imaging.ResizeType.HighQualityResample);

                                                                        // Create an instance of the ImageMasking class.
                                                                        Aspose.Imaging.Masking.ImageMasking masking = new Aspose.Imaging.Masking.ImageMasking(image);

                                                                        // Divide the source image into several clusters (segments).
                                                                        using (Aspose.Imaging.Masking.Result.MaskingResult maskingResult = masking.Decompose(maskingOptions))
                                                                        {
                                                                            // Getting the foreground mask
                                                                            using (Aspose.Imaging.RasterImage foregroundMask = maskingResult[1].GetMask()) 
                                                                            {
                                                                                // Increase the size of the mask to the size of the original image
                                                                                foregroundMask.Resize(imageSize.Width, imageSize.Height, Aspose.Imaging.ResizeType.NearestNeighbourResample);

                                                                                // Applying the mask to the original image to obtain a foreground segment
                                                                                using (Aspose.Imaging.RasterImage originImage = (Aspose.Imaging.RasterImage)Aspose.Imaging.Image.Load(dir + "BigImage.jpg"))
                                                                                {
                                                                                    Aspose.Imaging.Masking.ImageMasking.ApplyMask(originImage, foregroundMask, maskingOptions);
                                                                                    originImage.Save(dir + "BigImage_foreground.png", exportOptions);
                                                                                }
                                                                            }
                                                                        }
                                                                    }

Ten przykład pokazuje, jak rozdzielić obraz rasterowy na kilka obrazów za pomocą maski obrazu i ręcznie. maska obraza jest techniką przetwarzania obrazy, która jest używana do podziału tła z przedmiotów obrazowych z przodu.

string dir = "c:\\temp\\";

                                                                                                                                                                                                                                                // Define a manual mask.
                                                                                                                                                                                                                                                Aspose.Imaging.GraphicsPath manualMask = new Aspose.Imaging.GraphicsPath();
                                                                                                                                                                                                                                                Aspose.Imaging.Figure figure = new Aspose.Imaging.Figure();
                                                                                                                                                                                                                                                figure.AddShape(new Aspose.Imaging.Shapes.EllipseShape(new RectangleF(50, 50, 40, 40)));
                                                                                                                                                                                                                                                figure.AddShape(new Aspose.Imaging.Shapes.RectangleShape(new RectangleF(10, 20, 50, 30)));
                                                                                                                                                                                                                                                manualMask.AddFigure(figure);

                                                                                                                                                                                                                                                // Each cluster (segment) will be stored to a separate PNG file.
                                                                                                                                                                                                                                                Aspose.Imaging.ImageOptions.PngOptions exportOptions = new Aspose.Imaging.ImageOptions.PngOptions();
                                                                                                                                                                                                                                                exportOptions.ColorType = Aspose.Imaging.FileFormats.Png.PngColorType.TruecolorWithAlpha;
                                                                                                                                                                                                                                                exportOptions.Source = new Aspose.Imaging.Sources.StreamSource(new System.IO.MemoryStream());

                                                                                                                                                                                                                                                // Set the manual mask.
                                                                                                                                                                                                                                                Aspose.Imaging.Masking.Options.ManualMaskingArgs args = new Aspose.Imaging.Masking.Options.ManualMaskingArgs();
                                                                                                                                                                                                                                                args.Mask = manualMask;

                                                                                                                                                                                                                                                using (RasterImage image = (RasterImage)Image.Load(dir + "Blue hills.png"))
                                                                                                                                                                                                                                                {
                                                                                                                                                                                                                                                    Aspose.Imaging.Masking.Options.MaskingOptions maskingOptions = new Aspose.Imaging.Masking.Options.MaskingOptions();

                                                                                                                                                                                                                                                    // Use manual clustering algorithm.
                                                                                                                                                                                                                                                    maskingOptions.Method = Masking.Options.SegmentationMethod.Manual;

                                                                                                                                                                                                                                                    // All shapes making up a mask will be combined into one. 
                                                                                                                                                                                                                                                    maskingOptions.Decompose = false;
                                                                                                                                                                                                                                                    maskingOptions.Args = args;

                                                                                                                                                                                                                                                    // The backgroung color will be orange.
                                                                                                                                                                                                                                                    maskingOptions.BackgroundReplacementColor = Aspose.Imaging.Color.Orange;
                                                                                                                                                                                                                                                    maskingOptions.ExportOptions = exportOptions;

                                                                                                                                                                                                                                                    // The area of the source image that masking will be applied to.
                                                                                                                                                                                                                                                    maskingOptions.MaskingArea = new Rectangle(50, 50, 120, 120);

                                                                                                                                                                                                                                                    // Create an instance of the ImageMasking class.
                                                                                                                                                                                                                                                    Aspose.Imaging.Masking.ImageMasking masking = new Aspose.Imaging.Masking.ImageMasking(image);

                                                                                                                                                                                                                                                    // Divide the source image into several clusters (segments).
                                                                                                                                                                                                                                                    using (Aspose.Imaging.Masking.Result.MaskingResult maskingResult = masking.Decompose(maskingOptions))
                                                                                                                                                                                                                                                    {
                                                                                                                                                                                                                                                        // Obtain images from masking result and save them to PNG.
                                                                                                                                                                                                                                                        for (int i = 0; i < maskingResult.Length; i++)
                                                                                                                                                                                                                                                        {
                                                                                                                                                                                                                                                            string outputFileName = string.Format("Blue hills.Segment{0}.png", maskingResult[i].ObjectNumber);
                                                                                                                                                                                                                                                            using (Aspose.Imaging.Image resultImage = maskingResult[i].GetImage())
                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                resultImage.Save(dir + outputFileName);
                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                }

Ten przykład pokazuje, jak określić sugestie dla algorytmu ukrywania obrazu w celu poprawy precyzji metody segmentacji (klastrowania).

string dir = "c:\\temp\\";

                                                                                                                                                                                                                                                                     using (Aspose.Imaging.RasterImage image = (Aspose.Imaging.RasterImage)Aspose.Imaging.Image.Load(dir + "Gorilla.bmp"))
                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                         Aspose.Imaging.Masking.Options.AutoMaskingArgs args = new Aspose.Imaging.Masking.Options.AutoMaskingArgs();

                                                                                                                                                                                                                                                                         // Suggestion #1.
                                                                                                                                                                                                                                                                         // Analyze the image visually and set the area of interest. The result of segmentation will include only objects that will be completely located within this area.
                                                                                                                                                                                                                                                                         args.ObjectsRectangles = new Rectangle[]
                                                                                                                                                                                                                                                                         {
                                                                                                                                                                                                                                                                             new Rectangle(86, 6, 270, 364),
                                                                                                                                                                                                                                                                         };

                                                                                                                                                                                                                                                                         // Suggestion #2.
                                                                                                                                                                                                                                                                         // Analyze the image visually and set the points that belong to separated objects.
                                                                                                                                                                                                                                                                         args.ObjectsPoints = new Point[][]
                                                                                                                                                                                                                                                                         {
                                                                                                                                                                                                                                                                             new Point[] { new Point(103, 326) },
                                                                                                                                                                                                                                                                             new Point[] { new Point(280, 43) },
                                                                                                                                                                                                                                                                             new Point[] { new Point(319, 86) },
                                                                                                                                                                                                                                                                         };

                                                                                                                                                                                                                                                                         // Each cluster (segment) will be stored to a separate PNG file.
                                                                                                                                                                                                                                                                         Aspose.Imaging.ImageOptions.PngOptions exportOptions = new Aspose.Imaging.ImageOptions.PngOptions();
                                                                                                                                                                                                                                                                         exportOptions.ColorType = Aspose.Imaging.FileFormats.Png.PngColorType.TruecolorWithAlpha;
                                                                                                                                                                                                                                                                         exportOptions.Source = new Aspose.Imaging.Sources.StreamSource(new System.IO.MemoryStream());

                                                                                                                                                                                                                                                                         Aspose.Imaging.Masking.Options.MaskingOptions maskingOptions = new Aspose.Imaging.Masking.Options.MaskingOptions();

                                                                                                                                                                                                                                                                         // Use GraphCut clustering.
                                                                                                                                                                                                                                                                         maskingOptions.Method = Masking.Options.SegmentationMethod.GraphCut;
                                                                                                                                                                                                                                                                         maskingOptions.Decompose = false;
                                                                                                                                                                                                                                                                         maskingOptions.Args = args;

                                                                                                                                                                                                                                                                         // The backgroung color will be orange.
                                                                                                                                                                                                                                                                         maskingOptions.BackgroundReplacementColor = Aspose.Imaging.Color.Orange;
                                                                                                                                                                                                                                                                         maskingOptions.ExportOptions = exportOptions;

                                                                                                                                                                                                                                                                         // Create an instance of the ImageMasking class.
                                                                                                                                                                                                                                                                         Aspose.Imaging.Masking.ImageMasking masking = new Aspose.Imaging.Masking.ImageMasking(image);

                                                                                                                                                                                                                                                                         // Divide the source image into several clusters (segments).
                                                                                                                                                                                                                                                                         using (Aspose.Imaging.Masking.Result.MaskingResult maskingResult = masking.Decompose(maskingOptions))
                                                                                                                                                                                                                                                                         {
                                                                                                                                                                                                                                                                             // Obtain images from masking result and save them to PNG.
                                                                                                                                                                                                                                                                             for (int i = 0; i < maskingResult.Length; i++)
                                                                                                                                                                                                                                                                             {
                                                                                                                                                                                                                                                                                 string outputFileName = string.Format("Gorilla.Segment{0}.png", maskingResult[i].ObjectNumber);
                                                                                                                                                                                                                                                                                 using (Aspose.Imaging.Image resultImage = maskingResult[i].GetImage())
                                                                                                                                                                                                                                                                                 {
                                                                                                                                                                                                                                                                                     resultImage.Save(dir + outputFileName);
                                                                                                                                                                                                                                                                                 }
                                                                                                                                                                                                                                                                             }
                                                                                                                                                                                                                                                                         }
                                                                                                                                                                                                                                                                     }

Oszczędzanie sesji masku do pliku na długie sesje, a także możliwość ponownego odtwarzania w innym środowisku.

string dir = "c:\\temp\\";
                                                                                                                                                 string sessionBackupFile = dir + "session.bak";

                                                                                                                                                 // Masking export options
                                                                                                                                                 Aspose.Imaging.ImageOptions.PngOptions exportOptions = new Aspose.Imaging.ImageOptions.PngOptions();
                                                                                                                                                 exportOptions.ColorType = Aspose.Imaging.FileFormats.Png.PngColorType.TruecolorWithAlpha;
                                                                                                                                                 exportOptions.Source = new Aspose.Imaging.Sources.StreamSource(new System.IO.MemoryStream());

                                                                                                                                                 Aspose.Imaging.Masking.Options.MaskingOptions maskingOptions = new Aspose.Imaging.Masking.Options.MaskingOptions();

                                                                                                                                                 // Use GraphCut clustering.
                                                                                                                                                 maskingOptions.Method = Masking.Options.SegmentationMethod.GraphCut;
                                                                                                                                                 maskingOptions.Decompose = false;
                                                                                                                                                 maskingOptions.Args = new Aspose.Imaging.Masking.Options.AutoMaskingArgs();

                                                                                                                                                 // The backgroung color will be orange.
                                                                                                                                                 maskingOptions.BackgroundReplacementColor = Aspose.Imaging.Color.Orange;
                                                                                                                                                 maskingOptions.ExportOptions = exportOptions;

                                                                                                                                                 // Starting a session for the first time and saving to a file
                                                                                                                                                 using (Aspose.Imaging.RasterImage image = (Aspose.Imaging.RasterImage)Aspose.Imaging.Image.Load(dir + "Gorilla.bmp"))
                                                                                                                                                 {
                                                                                                                                                     // Create an instance of the ImageMasking class.
                                                                                                                                                     Aspose.Imaging.Masking.ImageMasking masking = new Aspose.Imaging.Masking.ImageMasking(image);

                                                                                                                                                     using (Aspose.Imaging.Masking.IMaskingSession session = masking.CreateSession(maskingOptions))
                                                                                                                                                     {
                                                                                                                                                         using (Aspose.Imaging.Masking.Result.MaskingResult maskingResult = session.Decompose())
                                                                                                                                                         {
                                                                                                                                                             using (Aspose.Imaging.RasterImage segmentImage = maskingResult[1].GetImage())
                                                                                                                                                             {
                                                                                                                                                                 segmentImage.Save(dir + "step1.png");
                                                                                                                                                             }
                                                                                                                                                         }

                                                                                                                                                         session.Save(sessionBackupFile);
                                                                                                                                                     }
                                                                                                                                                 }

                                                                                                                                                 // Resuming a masking session from a file
                                                                                                                                                 using (Aspose.Imaging.RasterImage image = (Aspose.Imaging.RasterImage)Aspose.Imaging.Image.Load(dir + "Gorilla.bmp"))
                                                                                                                                                 {
                                                                                                                                                     // Create an instance of the ImageMasking class.
                                                                                                                                                     Aspose.Imaging.Masking.ImageMasking masking = new Aspose.Imaging.Masking.ImageMasking(image);

                                                                                                                                                     using (Aspose.Imaging.Masking.IMaskingSession session = masking.LoadSession(sessionBackupFile))
                                                                                                                                                     {
                                                                                                                                                         Aspose.Imaging.Masking.Options.AutoMaskingArgs args = new Aspose.Imaging.Masking.Options.AutoMaskingArgs();

                                                                                                                                                         // Analyze the image visually and set the points that belong to separated objects.
                                                                                                                                                         args.ObjectsPoints = new Point[][]
                                                                                                                                                                                      {
                                                                                                                                                                                          new Point[]
                                                                                                                                                                                              {
                                                                                                                                                                                                  new Point(0, 0), new Point(0, 1), new Point(1, 0),
                                                                                                                                                                                                  new Point(1, 1), new Point(2, 0), new Point(2, 1),
                                                                                                                                                                                                  new Point(3, 0), new Point(3, 1)
                                                                                                                                                                                              },
                                                                                                                                                                                      };
                                                                                                                                                         using (Aspose.Imaging.Masking.Result.MaskingResult maskingResult = session.ImproveDecomposition(args))
                                                                                                                                                         {
                                                                                                                                                             // Explicit transfer of export options, since it is not serializable
                                                                                                                                                             maskingResult.MaskingOptions.ExportOptions = exportOptions;

                                                                                                                                                             using (Aspose.Imaging.RasterImage segmentImage = maskingResult[1].GetImage())
                                                                                                                                                             {
                                                                                                                                                                 segmentImage.Save(dir + "step2.png");
                                                                                                                                                             }
                                                                                                                                                         }
                                                                                                                                                     }
                                                                                                                                                 }

MaskingArea

Zostaw lub ustaw obszar maski.

public Rectangle MaskingArea { get; set; }

Wartość nieruchomości

Rectangle

Examples

Ten przykład pokazuje, jak rozdzielić obraz rasterowy na kilka obrazów za pomocą maski obrazu i ręcznie. maska obraza jest techniką przetwarzania obrazy, która jest używana do podziału tła z przedmiotów obrazowych z przodu.

string dir = "c:\\temp\\";

                                                                                                                                                                                                                                                // Define a manual mask.
                                                                                                                                                                                                                                                Aspose.Imaging.GraphicsPath manualMask = new Aspose.Imaging.GraphicsPath();
                                                                                                                                                                                                                                                Aspose.Imaging.Figure figure = new Aspose.Imaging.Figure();
                                                                                                                                                                                                                                                figure.AddShape(new Aspose.Imaging.Shapes.EllipseShape(new RectangleF(50, 50, 40, 40)));
                                                                                                                                                                                                                                                figure.AddShape(new Aspose.Imaging.Shapes.RectangleShape(new RectangleF(10, 20, 50, 30)));
                                                                                                                                                                                                                                                manualMask.AddFigure(figure);

                                                                                                                                                                                                                                                // Each cluster (segment) will be stored to a separate PNG file.
                                                                                                                                                                                                                                                Aspose.Imaging.ImageOptions.PngOptions exportOptions = new Aspose.Imaging.ImageOptions.PngOptions();
                                                                                                                                                                                                                                                exportOptions.ColorType = Aspose.Imaging.FileFormats.Png.PngColorType.TruecolorWithAlpha;
                                                                                                                                                                                                                                                exportOptions.Source = new Aspose.Imaging.Sources.StreamSource(new System.IO.MemoryStream());

                                                                                                                                                                                                                                                // Set the manual mask.
                                                                                                                                                                                                                                                Aspose.Imaging.Masking.Options.ManualMaskingArgs args = new Aspose.Imaging.Masking.Options.ManualMaskingArgs();
                                                                                                                                                                                                                                                args.Mask = manualMask;

                                                                                                                                                                                                                                                using (RasterImage image = (RasterImage)Image.Load(dir + "Blue hills.png"))
                                                                                                                                                                                                                                                {
                                                                                                                                                                                                                                                    Aspose.Imaging.Masking.Options.MaskingOptions maskingOptions = new Aspose.Imaging.Masking.Options.MaskingOptions();

                                                                                                                                                                                                                                                    // Use manual clustering algorithm.
                                                                                                                                                                                                                                                    maskingOptions.Method = Masking.Options.SegmentationMethod.Manual;

                                                                                                                                                                                                                                                    // All shapes making up a mask will be combined into one. 
                                                                                                                                                                                                                                                    maskingOptions.Decompose = false;
                                                                                                                                                                                                                                                    maskingOptions.Args = args;

                                                                                                                                                                                                                                                    // The backgroung color will be orange.
                                                                                                                                                                                                                                                    maskingOptions.BackgroundReplacementColor = Aspose.Imaging.Color.Orange;
                                                                                                                                                                                                                                                    maskingOptions.ExportOptions = exportOptions;

                                                                                                                                                                                                                                                    // The area of the source image that masking will be applied to.
                                                                                                                                                                                                                                                    maskingOptions.MaskingArea = new Rectangle(50, 50, 120, 120);

                                                                                                                                                                                                                                                    // Create an instance of the ImageMasking class.
                                                                                                                                                                                                                                                    Aspose.Imaging.Masking.ImageMasking masking = new Aspose.Imaging.Masking.ImageMasking(image);

                                                                                                                                                                                                                                                    // Divide the source image into several clusters (segments).
                                                                                                                                                                                                                                                    using (Aspose.Imaging.Masking.Result.MaskingResult maskingResult = masking.Decompose(maskingOptions))
                                                                                                                                                                                                                                                    {
                                                                                                                                                                                                                                                        // Obtain images from masking result and save them to PNG.
                                                                                                                                                                                                                                                        for (int i = 0; i < maskingResult.Length; i++)
                                                                                                                                                                                                                                                        {
                                                                                                                                                                                                                                                            string outputFileName = string.Format("Blue hills.Segment{0}.png", maskingResult[i].ObjectNumber);
                                                                                                                                                                                                                                                            using (Aspose.Imaging.Image resultImage = maskingResult[i].GetImage())
                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                resultImage.Save(dir + outputFileName);
                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                }

Method

Dostęp lub ustawienie metody segmentacji.

public SegmentationMethod Method { get; set; }

Wartość nieruchomości

SegmentationMethod

Examples

Ten przykład pokazuje, jak rozdzielić obraz rasterowy na kilka obrazów za pomocą maski obrazu i algorytmu segmentacji K-means.

string dir = "c:\\temp\\";

                                                                                                                                                                                                                                                                     using (Aspose.Imaging.RasterImage image = (Aspose.Imaging.RasterImage)Aspose.Imaging.Image.Load(dir + "Blue hills.png"))
                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                         Aspose.Imaging.Masking.Options.AutoMaskingArgs args = new Aspose.Imaging.Masking.Options.AutoMaskingArgs();

                                                                                                                                                                                                                                                                         // Set the number of clusters (separated objects). The default value is 2, the foreground object and the background.
                                                                                                                                                                                                                                                                         args.NumberOfObjects = 3;

                                                                                                                                                                                                                                                                         // Set the maximum number of iterations.
                                                                                                                                                                                                                                                                         args.MaxIterationNumber = 50;

                                                                                                                                                                                                                                                                         // Set the precision of segmentation method (optional)
                                                                                                                                                                                                                                                                         args.Precision = 1;

                                                                                                                                                                                                                                                                         // Each cluster (segment) will be stored to a separate PNG file.
                                                                                                                                                                                                                                                                         Aspose.Imaging.ImageOptions.PngOptions exportOptions = new Aspose.Imaging.ImageOptions.PngOptions();
                                                                                                                                                                                                                                                                         exportOptions.ColorType = Aspose.Imaging.FileFormats.Png.PngColorType.TruecolorWithAlpha;
                                                                                                                                                                                                                                                                         exportOptions.Source = new Aspose.Imaging.Sources.StreamSource(new System.IO.MemoryStream());

                                                                                                                                                                                                                                                                         Aspose.Imaging.Masking.Options.MaskingOptions maskingOptions = new Aspose.Imaging.Masking.Options.MaskingOptions();

                                                                                                                                                                                                                                                                         // Use K-means clustering.
                                                                                                                                                                                                                                                                         // K-means clustering allows to split image into several independent clusters (segments).
                                                                                                                                                                                                                                                                         maskingOptions.Method = Masking.Options.SegmentationMethod.KMeans;
                                                                                                                                                                                                                                                                         maskingOptions.Decompose = true;
                                                                                                                                                                                                                                                                         maskingOptions.Args = args;

                                                                                                                                                                                                                                                                         // The backgroung color will be orange.
                                                                                                                                                                                                                                                                         maskingOptions.BackgroundReplacementColor = Aspose.Imaging.Color.Orange;
                                                                                                                                                                                                                                                                         maskingOptions.ExportOptions = exportOptions;

                                                                                                                                                                                                                                                                         // Create an instance of the ImageMasking class.
                                                                                                                                                                                                                                                                         Aspose.Imaging.Masking.ImageMasking masking = new Aspose.Imaging.Masking.ImageMasking(image);

                                                                                                                                                                                                                                                                         // Divide the source image into several clusters (segments).
                                                                                                                                                                                                                                                                         using (Aspose.Imaging.Masking.Result.MaskingResult maskingResult = masking.Decompose(maskingOptions))
                                                                                                                                                                                                                                                                         {
                                                                                                                                                                                                                                                                             // Obtain images from masking result and save them to PNG.
                                                                                                                                                                                                                                                                             for (int i = 0; i < maskingResult.Length; i++)
                                                                                                                                                                                                                                                                             {
                                                                                                                                                                                                                                                                                 string outputFileName = string.Format("Blue hills.Segment{0}.png", maskingResult[i].ObjectNumber);
                                                                                                                                                                                                                                                                                 using (Aspose.Imaging.Image resultImage = maskingResult[i].GetImage())
                                                                                                                                                                                                                                                                                 {
                                                                                                                                                                                                                                                                                     resultImage.Save(dir + outputFileName);
                                                                                                                                                                                                                                                                                 }
                                                                                                                                                                                                                                                                             }
                                                                                                                                                                                                                                                                         }
                                                                                                                                                                                                                                                                     }

Używanie maski segmentowej do przyspieszenia procesu segmentacji

// Masking export options
                                                                    Aspose.Imaging.ImageOptions.PngOptions exportOptions = new Aspose.Imaging.ImageOptions.PngOptions();
                                                                    exportOptions.ColorType = Aspose.Imaging.FileFormats.Png.PngColorType.TruecolorWithAlpha;
                                                                    exportOptions.Source = new Aspose.Imaging.Sources.StreamSource(new System.IO.MemoryStream());

                                                                    Aspose.Imaging.Masking.Options.MaskingOptions maskingOptions = new Aspose.Imaging.Masking.Options.MaskingOptions();

                                                                    // Use GraphCut clustering.
                                                                    maskingOptions.Method = Masking.Options.SegmentationMethod.GraphCut;
                                                                    maskingOptions.Decompose = false;
                                                                    maskingOptions.Args = new Aspose.Imaging.Masking.Options.AutoMaskingArgs();

                                                                    // The backgroung color will be transparent.
                                                                    maskingOptions.BackgroundReplacementColor = Aspose.Imaging.Color.Transparent;
                                                                    maskingOptions.ExportOptions = exportOptions;

                                                                    string dir = "c:\\temp\\";
                                                                    using (Aspose.Imaging.RasterImage image = (Aspose.Imaging.RasterImage)Aspose.Imaging.Image.Load(dir + "BigImage.jpg"))
                                                                    {
                                                                        Aspose.Imaging.Size imageSize = image.Size;

                                                                        // Reducing image size to speed up the segmentation process
                                                                        image.ResizeHeightProportionally(600, Aspose.Imaging.ResizeType.HighQualityResample);

                                                                        // Create an instance of the ImageMasking class.
                                                                        Aspose.Imaging.Masking.ImageMasking masking = new Aspose.Imaging.Masking.ImageMasking(image);

                                                                        // Divide the source image into several clusters (segments).
                                                                        using (Aspose.Imaging.Masking.Result.MaskingResult maskingResult = masking.Decompose(maskingOptions))
                                                                        {
                                                                            // Getting the foreground mask
                                                                            using (Aspose.Imaging.RasterImage foregroundMask = maskingResult[1].GetMask()) 
                                                                            {
                                                                                // Increase the size of the mask to the size of the original image
                                                                                foregroundMask.Resize(imageSize.Width, imageSize.Height, Aspose.Imaging.ResizeType.NearestNeighbourResample);

                                                                                // Applying the mask to the original image to obtain a foreground segment
                                                                                using (Aspose.Imaging.RasterImage originImage = (Aspose.Imaging.RasterImage)Aspose.Imaging.Image.Load(dir + "BigImage.jpg"))
                                                                                {
                                                                                    Aspose.Imaging.Masking.ImageMasking.ApplyMask(originImage, foregroundMask, maskingOptions);
                                                                                    originImage.Save(dir + "BigImage_foreground.png", exportOptions);
                                                                                }
                                                                            }
                                                                        }
                                                                    }

Ten przykład pokazuje, jak rozdzielić obraz rasterowy na kilka obrazów za pomocą maski obrazu i ręcznie. maska obraza jest techniką przetwarzania obrazy, która jest używana do podziału tła z przedmiotów obrazowych z przodu.

string dir = "c:\\temp\\";

                                                                                                                                                                                                                                                // Define a manual mask.
                                                                                                                                                                                                                                                Aspose.Imaging.GraphicsPath manualMask = new Aspose.Imaging.GraphicsPath();
                                                                                                                                                                                                                                                Aspose.Imaging.Figure figure = new Aspose.Imaging.Figure();
                                                                                                                                                                                                                                                figure.AddShape(new Aspose.Imaging.Shapes.EllipseShape(new RectangleF(50, 50, 40, 40)));
                                                                                                                                                                                                                                                figure.AddShape(new Aspose.Imaging.Shapes.RectangleShape(new RectangleF(10, 20, 50, 30)));
                                                                                                                                                                                                                                                manualMask.AddFigure(figure);

                                                                                                                                                                                                                                                // Each cluster (segment) will be stored to a separate PNG file.
                                                                                                                                                                                                                                                Aspose.Imaging.ImageOptions.PngOptions exportOptions = new Aspose.Imaging.ImageOptions.PngOptions();
                                                                                                                                                                                                                                                exportOptions.ColorType = Aspose.Imaging.FileFormats.Png.PngColorType.TruecolorWithAlpha;
                                                                                                                                                                                                                                                exportOptions.Source = new Aspose.Imaging.Sources.StreamSource(new System.IO.MemoryStream());

                                                                                                                                                                                                                                                // Set the manual mask.
                                                                                                                                                                                                                                                Aspose.Imaging.Masking.Options.ManualMaskingArgs args = new Aspose.Imaging.Masking.Options.ManualMaskingArgs();
                                                                                                                                                                                                                                                args.Mask = manualMask;

                                                                                                                                                                                                                                                using (RasterImage image = (RasterImage)Image.Load(dir + "Blue hills.png"))
                                                                                                                                                                                                                                                {
                                                                                                                                                                                                                                                    Aspose.Imaging.Masking.Options.MaskingOptions maskingOptions = new Aspose.Imaging.Masking.Options.MaskingOptions();

                                                                                                                                                                                                                                                    // Use manual clustering algorithm.
                                                                                                                                                                                                                                                    maskingOptions.Method = Masking.Options.SegmentationMethod.Manual;

                                                                                                                                                                                                                                                    // All shapes making up a mask will be combined into one. 
                                                                                                                                                                                                                                                    maskingOptions.Decompose = false;
                                                                                                                                                                                                                                                    maskingOptions.Args = args;

                                                                                                                                                                                                                                                    // The backgroung color will be orange.
                                                                                                                                                                                                                                                    maskingOptions.BackgroundReplacementColor = Aspose.Imaging.Color.Orange;
                                                                                                                                                                                                                                                    maskingOptions.ExportOptions = exportOptions;

                                                                                                                                                                                                                                                    // The area of the source image that masking will be applied to.
                                                                                                                                                                                                                                                    maskingOptions.MaskingArea = new Rectangle(50, 50, 120, 120);

                                                                                                                                                                                                                                                    // Create an instance of the ImageMasking class.
                                                                                                                                                                                                                                                    Aspose.Imaging.Masking.ImageMasking masking = new Aspose.Imaging.Masking.ImageMasking(image);

                                                                                                                                                                                                                                                    // Divide the source image into several clusters (segments).
                                                                                                                                                                                                                                                    using (Aspose.Imaging.Masking.Result.MaskingResult maskingResult = masking.Decompose(maskingOptions))
                                                                                                                                                                                                                                                    {
                                                                                                                                                                                                                                                        // Obtain images from masking result and save them to PNG.
                                                                                                                                                                                                                                                        for (int i = 0; i < maskingResult.Length; i++)
                                                                                                                                                                                                                                                        {
                                                                                                                                                                                                                                                            string outputFileName = string.Format("Blue hills.Segment{0}.png", maskingResult[i].ObjectNumber);
                                                                                                                                                                                                                                                            using (Aspose.Imaging.Image resultImage = maskingResult[i].GetImage())
                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                resultImage.Save(dir + outputFileName);
                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                }

Ten przykład pokazuje, jak określić sugestie dla algorytmu ukrywania obrazu w celu poprawy precyzji metody segmentacji (klastrowania).

string dir = "c:\\temp\\";

                                                                                                                                                                                                                                                                     using (Aspose.Imaging.RasterImage image = (Aspose.Imaging.RasterImage)Aspose.Imaging.Image.Load(dir + "Gorilla.bmp"))
                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                         Aspose.Imaging.Masking.Options.AutoMaskingArgs args = new Aspose.Imaging.Masking.Options.AutoMaskingArgs();

                                                                                                                                                                                                                                                                         // Suggestion #1.
                                                                                                                                                                                                                                                                         // Analyze the image visually and set the area of interest. The result of segmentation will include only objects that will be completely located within this area.
                                                                                                                                                                                                                                                                         args.ObjectsRectangles = new Rectangle[]
                                                                                                                                                                                                                                                                         {
                                                                                                                                                                                                                                                                             new Rectangle(86, 6, 270, 364),
                                                                                                                                                                                                                                                                         };

                                                                                                                                                                                                                                                                         // Suggestion #2.
                                                                                                                                                                                                                                                                         // Analyze the image visually and set the points that belong to separated objects.
                                                                                                                                                                                                                                                                         args.ObjectsPoints = new Point[][]
                                                                                                                                                                                                                                                                         {
                                                                                                                                                                                                                                                                             new Point[] { new Point(103, 326) },
                                                                                                                                                                                                                                                                             new Point[] { new Point(280, 43) },
                                                                                                                                                                                                                                                                             new Point[] { new Point(319, 86) },
                                                                                                                                                                                                                                                                         };

                                                                                                                                                                                                                                                                         // Each cluster (segment) will be stored to a separate PNG file.
                                                                                                                                                                                                                                                                         Aspose.Imaging.ImageOptions.PngOptions exportOptions = new Aspose.Imaging.ImageOptions.PngOptions();
                                                                                                                                                                                                                                                                         exportOptions.ColorType = Aspose.Imaging.FileFormats.Png.PngColorType.TruecolorWithAlpha;
                                                                                                                                                                                                                                                                         exportOptions.Source = new Aspose.Imaging.Sources.StreamSource(new System.IO.MemoryStream());

                                                                                                                                                                                                                                                                         Aspose.Imaging.Masking.Options.MaskingOptions maskingOptions = new Aspose.Imaging.Masking.Options.MaskingOptions();

                                                                                                                                                                                                                                                                         // Use GraphCut clustering.
                                                                                                                                                                                                                                                                         maskingOptions.Method = Masking.Options.SegmentationMethod.GraphCut;
                                                                                                                                                                                                                                                                         maskingOptions.Decompose = false;
                                                                                                                                                                                                                                                                         maskingOptions.Args = args;

                                                                                                                                                                                                                                                                         // The backgroung color will be orange.
                                                                                                                                                                                                                                                                         maskingOptions.BackgroundReplacementColor = Aspose.Imaging.Color.Orange;
                                                                                                                                                                                                                                                                         maskingOptions.ExportOptions = exportOptions;

                                                                                                                                                                                                                                                                         // Create an instance of the ImageMasking class.
                                                                                                                                                                                                                                                                         Aspose.Imaging.Masking.ImageMasking masking = new Aspose.Imaging.Masking.ImageMasking(image);

                                                                                                                                                                                                                                                                         // Divide the source image into several clusters (segments).
                                                                                                                                                                                                                                                                         using (Aspose.Imaging.Masking.Result.MaskingResult maskingResult = masking.Decompose(maskingOptions))
                                                                                                                                                                                                                                                                         {
                                                                                                                                                                                                                                                                             // Obtain images from masking result and save them to PNG.
                                                                                                                                                                                                                                                                             for (int i = 0; i < maskingResult.Length; i++)
                                                                                                                                                                                                                                                                             {
                                                                                                                                                                                                                                                                                 string outputFileName = string.Format("Gorilla.Segment{0}.png", maskingResult[i].ObjectNumber);
                                                                                                                                                                                                                                                                                 using (Aspose.Imaging.Image resultImage = maskingResult[i].GetImage())
                                                                                                                                                                                                                                                                                 {
                                                                                                                                                                                                                                                                                     resultImage.Save(dir + outputFileName);
                                                                                                                                                                                                                                                                                 }
                                                                                                                                                                                                                                                                             }
                                                                                                                                                                                                                                                                         }
                                                                                                                                                                                                                                                                     }

Oszczędzanie sesji masku do pliku na długie sesje, a także możliwość ponownego odtwarzania w innym środowisku.

string dir = "c:\\temp\\";
                                                                                                                                                 string sessionBackupFile = dir + "session.bak";

                                                                                                                                                 // Masking export options
                                                                                                                                                 Aspose.Imaging.ImageOptions.PngOptions exportOptions = new Aspose.Imaging.ImageOptions.PngOptions();
                                                                                                                                                 exportOptions.ColorType = Aspose.Imaging.FileFormats.Png.PngColorType.TruecolorWithAlpha;
                                                                                                                                                 exportOptions.Source = new Aspose.Imaging.Sources.StreamSource(new System.IO.MemoryStream());

                                                                                                                                                 Aspose.Imaging.Masking.Options.MaskingOptions maskingOptions = new Aspose.Imaging.Masking.Options.MaskingOptions();

                                                                                                                                                 // Use GraphCut clustering.
                                                                                                                                                 maskingOptions.Method = Masking.Options.SegmentationMethod.GraphCut;
                                                                                                                                                 maskingOptions.Decompose = false;
                                                                                                                                                 maskingOptions.Args = new Aspose.Imaging.Masking.Options.AutoMaskingArgs();

                                                                                                                                                 // The backgroung color will be orange.
                                                                                                                                                 maskingOptions.BackgroundReplacementColor = Aspose.Imaging.Color.Orange;
                                                                                                                                                 maskingOptions.ExportOptions = exportOptions;

                                                                                                                                                 // Starting a session for the first time and saving to a file
                                                                                                                                                 using (Aspose.Imaging.RasterImage image = (Aspose.Imaging.RasterImage)Aspose.Imaging.Image.Load(dir + "Gorilla.bmp"))
                                                                                                                                                 {
                                                                                                                                                     // Create an instance of the ImageMasking class.
                                                                                                                                                     Aspose.Imaging.Masking.ImageMasking masking = new Aspose.Imaging.Masking.ImageMasking(image);

                                                                                                                                                     using (Aspose.Imaging.Masking.IMaskingSession session = masking.CreateSession(maskingOptions))
                                                                                                                                                     {
                                                                                                                                                         using (Aspose.Imaging.Masking.Result.MaskingResult maskingResult = session.Decompose())
                                                                                                                                                         {
                                                                                                                                                             using (Aspose.Imaging.RasterImage segmentImage = maskingResult[1].GetImage())
                                                                                                                                                             {
                                                                                                                                                                 segmentImage.Save(dir + "step1.png");
                                                                                                                                                             }
                                                                                                                                                         }

                                                                                                                                                         session.Save(sessionBackupFile);
                                                                                                                                                     }
                                                                                                                                                 }

                                                                                                                                                 // Resuming a masking session from a file
                                                                                                                                                 using (Aspose.Imaging.RasterImage image = (Aspose.Imaging.RasterImage)Aspose.Imaging.Image.Load(dir + "Gorilla.bmp"))
                                                                                                                                                 {
                                                                                                                                                     // Create an instance of the ImageMasking class.
                                                                                                                                                     Aspose.Imaging.Masking.ImageMasking masking = new Aspose.Imaging.Masking.ImageMasking(image);

                                                                                                                                                     using (Aspose.Imaging.Masking.IMaskingSession session = masking.LoadSession(sessionBackupFile))
                                                                                                                                                     {
                                                                                                                                                         Aspose.Imaging.Masking.Options.AutoMaskingArgs args = new Aspose.Imaging.Masking.Options.AutoMaskingArgs();

                                                                                                                                                         // Analyze the image visually and set the points that belong to separated objects.
                                                                                                                                                         args.ObjectsPoints = new Point[][]
                                                                                                                                                                                      {
                                                                                                                                                                                          new Point[]
                                                                                                                                                                                              {
                                                                                                                                                                                                  new Point(0, 0), new Point(0, 1), new Point(1, 0),
                                                                                                                                                                                                  new Point(1, 1), new Point(2, 0), new Point(2, 1),
                                                                                                                                                                                                  new Point(3, 0), new Point(3, 1)
                                                                                                                                                                                              },
                                                                                                                                                                                      };
                                                                                                                                                         using (Aspose.Imaging.Masking.Result.MaskingResult maskingResult = session.ImproveDecomposition(args))
                                                                                                                                                         {
                                                                                                                                                             // Explicit transfer of export options, since it is not serializable
                                                                                                                                                             maskingResult.MaskingOptions.ExportOptions = exportOptions;

                                                                                                                                                             using (Aspose.Imaging.RasterImage segmentImage = maskingResult[1].GetImage())
                                                                                                                                                             {
                                                                                                                                                                 segmentImage.Save(dir + "step2.png");
                                                                                                                                                             }
                                                                                                                                                         }
                                                                                                                                                     }
                                                                                                                                                 }
 Polski