Packages

 

com.aspose.imaging.masking

Interface IMaskingSession

  • All Superinterfaces:
    com.aspose.ms.System.IDisposable


    public interface IMaskingSession
    extends com.aspose.ms.System.IDisposable

    The masking session

    See Also:
    IDisposable
    Code example:

    Saving the masking session to a file for long sessions, as well as for the possibility of resuming the session in another environment.


    String dir = "c:\\temp\\";
    String sessionBackupFile = dir + "session.bak";
    
    // Masking export options
    com.aspose.imaging.imageoptions.PngOptions exportOptions = new com.aspose.imaging.imageoptions.PngOptions();
    exportOptions.setColorType(com.aspose.imaging.fileformats.png.PngColorType.TruecolorWithAlpha);
    exportOptions.setSource(new com.aspose.imaging.sources.StreamSource());
    
    com.aspose.imaging.masking.options.MaskingOptions maskingOptions = new com.aspose.imaging.masking.options.MaskingOptions();
    
    // Use GraphCut clustering.
    maskingOptions.setMethod(com.aspose.imaging.masking.options.SegmentationMethod.GraphCut);
    maskingOptions.setDecompose(false);
    maskingOptions.setArgs(new com.aspose.imaging.masking.options.AutoMaskingArgs());
    
    // The background color will be orange.
    maskingOptions.setBackgroundReplacementColor(com.aspose.imaging.Color.getOrange());
    maskingOptions.setExportOptions(exportOptions);
    
    // Starting a session for the first time and saving to a file
    com.aspose.imaging.RasterImage image = (com.aspose.imaging.RasterImage)com.aspose.imaging.Image.load(dir + "Gorilla.bmp");
    try
    {
        // Create an instance of the ImageMasking class.
        com.aspose.imaging.masking.ImageMasking masking = new com.aspose.imaging.masking.ImageMasking(image);
    
        com.aspose.imaging.masking.IMaskingSession session = masking.createSession(maskingOptions);
        try
        {
            com.aspose.imaging.masking.result.MaskingResult maskingResult = session.decompose();
            try
            {
                com.aspose.imaging.RasterImage segmentImage = maskingResult.get_Item(1).getImage();
                try
                {
                    segmentImage.save(dir + "step1.png");
                }
                finally
                {
                    segmentImage.close();
                }
            }
            finally
            {
                maskingResult.close();
            }
    
            session.save(sessionBackupFile);
        }
        finally
        {
            session.dispose();
        }
    }
    finally
    {
        image.close();
    }
    
    // Resuming a masking session from a file
    com.aspose.imaging.RasterImage image2 = (com.aspose.imaging.RasterImage)com.aspose.imaging.Image.load(dir + "Gorilla.bmp");
    try
    {
        // Create an instance of the ImageMasking class.
        com.aspose.imaging.masking.ImageMasking masking = new com.aspose.imaging.masking.ImageMasking(image2);
    
        com.aspose.imaging.masking.IMaskingSession session = masking.loadSession(sessionBackupFile);
        try
        {
            com.aspose.imaging.masking.options.AutoMaskingArgs args = new com.aspose.imaging.masking.options.AutoMaskingArgs();
    
            // Analyze the image visually and set the points that belong to separated objects.
            args.setObjectsPoints(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)
                                    },
                    });
            com.aspose.imaging.masking.result.MaskingResult maskingResult = session.improveDecomposition(args);
            try
            {
                // Explicit transfer of export options, since it is not serializable
                maskingResult.MaskingOptions.setExportOptions(exportOptions);
    
                com.aspose.imaging.RasterImage segmentImage = maskingResult.get_Item(1).getImage();
                try
                {
                    segmentImage.save(dir + "step2.png");
                }
                finally
                {
                    segmentImage.close();
                }
            }
            finally
            {
                maskingResult.close();
            }
        }
        finally
        {
            session.dispose();
        }
    }
    finally
    {
        image2.close();
    }
    

    • Method Detail

      • decompose

        MaskingResult decompose()

        Performs first rough decompose operation

        Returns:
        Result of masking operation as array of segment image providers.
        Code example:

        Saving the masking session to a file for long sessions, as well as for the possibility of resuming the session in another environment.


        String dir = "c:\\temp\\";
        String sessionBackupFile = dir + "session.bak";
        
        // Masking export options
        com.aspose.imaging.imageoptions.PngOptions exportOptions = new com.aspose.imaging.imageoptions.PngOptions();
        exportOptions.setColorType(com.aspose.imaging.fileformats.png.PngColorType.TruecolorWithAlpha);
        exportOptions.setSource(new com.aspose.imaging.sources.StreamSource());
        
        com.aspose.imaging.masking.options.MaskingOptions maskingOptions = new com.aspose.imaging.masking.options.MaskingOptions();
        
        // Use GraphCut clustering.
        maskingOptions.setMethod(com.aspose.imaging.masking.options.SegmentationMethod.GraphCut);
        maskingOptions.setDecompose(false);
        maskingOptions.setArgs(new com.aspose.imaging.masking.options.AutoMaskingArgs());
        
        // The background color will be orange.
        maskingOptions.setBackgroundReplacementColor(com.aspose.imaging.Color.getOrange());
        maskingOptions.setExportOptions(exportOptions);
        
        // Starting a session for the first time and saving to a file
        com.aspose.imaging.RasterImage image = (com.aspose.imaging.RasterImage)com.aspose.imaging.Image.load(dir + "Gorilla.bmp");
        try
        {
            // Create an instance of the ImageMasking class.
            com.aspose.imaging.masking.ImageMasking masking = new com.aspose.imaging.masking.ImageMasking(image);
        
            com.aspose.imaging.masking.IMaskingSession session = masking.createSession(maskingOptions);
            try
            {
                com.aspose.imaging.masking.result.MaskingResult maskingResult = session.decompose();
                try
                {
                    com.aspose.imaging.RasterImage segmentImage = maskingResult.get_Item(1).getImage();
                    try
                    {
                        segmentImage.save(dir + "step1.png");
                    }
                    finally
                    {
                        segmentImage.close();
                    }
                }
                finally
                {
                    maskingResult.close();
                }
        
                session.save(sessionBackupFile);
            }
            finally
            {
                session.dispose();
            }
        }
        finally
        {
            image.close();
        }
        
        // Resuming a masking session from a file
        com.aspose.imaging.RasterImage image2 = (com.aspose.imaging.RasterImage)com.aspose.imaging.Image.load(dir + "Gorilla.bmp");
        try
        {
            // Create an instance of the ImageMasking class.
            com.aspose.imaging.masking.ImageMasking masking = new com.aspose.imaging.masking.ImageMasking(image2);
        
            com.aspose.imaging.masking.IMaskingSession session = masking.loadSession(sessionBackupFile);
            try
            {
                com.aspose.imaging.masking.options.AutoMaskingArgs args = new com.aspose.imaging.masking.options.AutoMaskingArgs();
        
                // Analyze the image visually and set the points that belong to separated objects.
                args.setObjectsPoints(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)
                                        },
                        });
                com.aspose.imaging.masking.result.MaskingResult maskingResult = session.improveDecomposition(args);
                try
                {
                    // Explicit transfer of export options, since it is not serializable
                    maskingResult.MaskingOptions.setExportOptions(exportOptions);
        
                    com.aspose.imaging.RasterImage segmentImage = maskingResult.get_Item(1).getImage();
                    try
                    {
                        segmentImage.save(dir + "step2.png");
                    }
                    finally
                    {
                        segmentImage.close();
                    }
                }
                finally
                {
                    maskingResult.close();
                }
            }
            finally
            {
                session.dispose();
            }
        }
        finally
        {
            image2.close();
        }
        

      • decomposeAsync

        IAsyncTask decomposeAsync()

        Creates the asynchronous task which can perform first rough decompose operation

        Returns:
        The asynchronous decompose task
      • improveDecomposition

        MaskingResult improveDecomposition(IMaskingArgs maskingArguments)

        Performs retraining decompose operation

        Parameters:
        maskingArguments - The masking arguments.
        Returns:
        Result of masking operation as array of segment image providers.
        Code example:

        Saving the masking session to a file for long sessions, as well as for the possibility of resuming the session in another environment.


        String dir = "c:\\temp\\";
        String sessionBackupFile = dir + "session.bak";
        
        // Masking export options
        com.aspose.imaging.imageoptions.PngOptions exportOptions = new com.aspose.imaging.imageoptions.PngOptions();
        exportOptions.setColorType(com.aspose.imaging.fileformats.png.PngColorType.TruecolorWithAlpha);
        exportOptions.setSource(new com.aspose.imaging.sources.StreamSource());
        
        com.aspose.imaging.masking.options.MaskingOptions maskingOptions = new com.aspose.imaging.masking.options.MaskingOptions();
        
        // Use GraphCut clustering.
        maskingOptions.setMethod(com.aspose.imaging.masking.options.SegmentationMethod.GraphCut);
        maskingOptions.setDecompose(false);
        maskingOptions.setArgs(new com.aspose.imaging.masking.options.AutoMaskingArgs());
        
        // The background color will be orange.
        maskingOptions.setBackgroundReplacementColor(com.aspose.imaging.Color.getOrange());
        maskingOptions.setExportOptions(exportOptions);
        
        // Starting a session for the first time and saving to a file
        com.aspose.imaging.RasterImage image = (com.aspose.imaging.RasterImage)com.aspose.imaging.Image.load(dir + "Gorilla.bmp");
        try
        {
            // Create an instance of the ImageMasking class.
            com.aspose.imaging.masking.ImageMasking masking = new com.aspose.imaging.masking.ImageMasking(image);
        
            com.aspose.imaging.masking.IMaskingSession session = masking.createSession(maskingOptions);
            try
            {
                com.aspose.imaging.masking.result.MaskingResult maskingResult = session.decompose();
                try
                {
                    com.aspose.imaging.RasterImage segmentImage = maskingResult.get_Item(1).getImage();
                    try
                    {
                        segmentImage.save(dir + "step1.png");
                    }
                    finally
                    {
                        segmentImage.close();
                    }
                }
                finally
                {
                    maskingResult.close();
                }
        
                session.save(sessionBackupFile);
            }
            finally
            {
                session.dispose();
            }
        }
        finally
        {
            image.close();
        }
        
        // Resuming a masking session from a file
        com.aspose.imaging.RasterImage image2 = (com.aspose.imaging.RasterImage)com.aspose.imaging.Image.load(dir + "Gorilla.bmp");
        try
        {
            // Create an instance of the ImageMasking class.
            com.aspose.imaging.masking.ImageMasking masking = new com.aspose.imaging.masking.ImageMasking(image2);
        
            com.aspose.imaging.masking.IMaskingSession session = masking.loadSession(sessionBackupFile);
            try
            {
                com.aspose.imaging.masking.options.AutoMaskingArgs args = new com.aspose.imaging.masking.options.AutoMaskingArgs();
        
                // Analyze the image visually and set the points that belong to separated objects.
                args.setObjectsPoints(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)
                                        },
                        });
                com.aspose.imaging.masking.result.MaskingResult maskingResult = session.improveDecomposition(args);
                try
                {
                    // Explicit transfer of export options, since it is not serializable
                    maskingResult.MaskingOptions.setExportOptions(exportOptions);
        
                    com.aspose.imaging.RasterImage segmentImage = maskingResult.get_Item(1).getImage();
                    try
                    {
                        segmentImage.save(dir + "step2.png");
                    }
                    finally
                    {
                        segmentImage.close();
                    }
                }
                finally
                {
                    maskingResult.close();
                }
            }
            finally
            {
                session.dispose();
            }
        }
        finally
        {
            image2.close();
        }
        

      • improveDecompositionAsync

        IAsyncTask improveDecompositionAsync(IMaskingArgs maskingArguments)

        Creates the asynchronous task which can perform retraining decompose operation

        Parameters:
        maskingArguments - The masking arguments.
        Returns:
        The asynchronous decompose task
      • save

        void save(OutputStream stream)

        Save the session state to the specified stream.

        Parameters:
        stream - The stream.
      • save

        void save(com.aspose.ms.System.IO.Stream stream)

        Save the session state to the specified stream.

        Parameters:
        stream - The stream.
      • save

        void save(String filePath)

        Saves the session state to the specified file.

        Parameters:
        filePath - The file path.