Packages

 

com.aspose.imaging.masking

Class ImageMasking



  • public class ImageMasking
    extends Object

    Provides image masking operations

    Code example:

    This example shows how to decompose a raster image into multiple images using image masking and the K-means segmentation algorithm. Image masking is an image processing technique that is used to split the background from the foreground image objects.


    String dir = "c:\\temp\\";
    
    com.aspose.imaging.RasterImage image = (com.aspose.imaging.RasterImage) com.aspose.imaging.Image.load(dir + "Blue hills.png");
    try {
        com.aspose.imaging.masking.options.AutoMaskingArgs args = new com.aspose.imaging.masking.options.AutoMaskingArgs();
    
        // Set the number of clusters (separated objects). The default value is 2, the foreground object and the background.
        args.setNumberOfObjects(3);
    
        // Set the maximum number of iterations.
        args.setMaxIterationNumber(50);
    
        // Set the precision of segmentation method (optional)
        args.setPrecision(1);
    
        // Each cluster (segment) will be stored to a separate PNG file.
        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(new java.io.ByteArrayInputStream(new byte[0])));
    
        com.aspose.imaging.masking.options.MaskingOptions maskingOptions = new com.aspose.imaging.masking.options.MaskingOptions();
    
        // Use K-means clustering.
        // K-means clustering allows to split image into several independent clusters (segments).
        maskingOptions.setMethod(com.aspose.imaging.masking.options.SegmentationMethod.KMeans);
        maskingOptions.setDecompose(true);
        maskingOptions.setArgs(args);
    
        // The backgroung color will be orange.
        maskingOptions.setBackgroundReplacementColor(com.aspose.imaging.Color.getOrange());
        maskingOptions.setExportOptions(exportOptions);
    
        // Create an instance of the ImageMasking class.
        com.aspose.imaging.masking.ImageMasking masking = new com.aspose.imaging.masking.ImageMasking(image);
    
        // Divide the source image into several clusters (segments).
        com.aspose.imaging.masking.result.MaskingResult[] maskingResults = masking.decompose(maskingOptions);
    
        // Obtain images from masking result and save them to PNG.
        for (int i = 0; i < maskingResults.length; i++) {
            String outputFileName = String.format("Blue hills.Segment%s.png", maskingResults[i].getObjectNumber());
            com.aspose.imaging.Image resultImage = maskingResults[i].getImage();
            try {
                resultImage.save(dir + outputFileName);
            } finally {
                resultImage.dispose();
            }
        }
    } finally {
        image.dispose();
    }
    

    • Constructor Detail

      • ImageMasking

        public ImageMasking(RasterImage sourceImage)

        Initializes a new instance of the ImageMasking class.

        Parameters:
        sourceImage - The source image.
        Throws:
        com.aspose.ms.System.ArgumentNullException - source image is null.
    • Method Detail

      • decompose

        public final MaskingResult[] decompose(MaskingOptions options)

        Performs the decompose operation using specified masking options

        Parameters:
        options - The masking options.
        Returns:
        Result of masking operation as array of segment image providers.
      • decomposeAsync

        public final IAsyncTask decomposeAsync(MaskingOptions options)

        Creates the asynchronous decompose task using specified masking options.

        Parameters:
        options - The masking options.
        Returns:
        The asynchronous decompose task
      • createSession

        public final IMaskingSession createSession(MaskingOptions options)

        Creates the masking session which can perform retraining decompose operations.

        Parameters:
        options - The options.
        Returns:
        the masking session which can perform retraining decompose operations.