Packages

 

com.aspose.imaging.masking.options

Class MaskingOptions



  • public class MaskingOptions
    extends Object

    Represents the common image masking options.

    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();
    }
    

    • Field Detail

      • BACKGROUND_OBJECT_NUMBER

        public static final int BACKGROUND_OBJECT_NUMBER

        The background object number

        See Also:
        Constant Field Values
    • Constructor Detail

      • MaskingOptions

        public MaskingOptions()
    • Method Detail

      • getMethod

        public final int getMethod()

        Gets the segmentation method.

        Value: The segmentation method.
        Returns:
        the segmentation method.
      • setMethod

        public final void setMethod(int value)

        Sets the segmentation method.

        Value: The segmentation method.
        Parameters:
        value - the segmentation method.
        Code example:

        This example shows how to specify suggestions for image masking algorithm to improve precision of segmentation (clustering) method. 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 + "Gorilla.bmp");
        try {
            com.aspose.imaging.masking.options.AutoMaskingArgs args = new com.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.setObjectsRectangles(new com.aspose.imaging.Rectangle[]
                    {
                            new com.aspose.imaging.Rectangle(86, 6, 270, 364),
                    });
        
            // Suggestion #2.
            // Analyze the image visually and set the points that belong to separated objects.
            args.setObjectsPoints(new com.aspose.imaging.Point[][]
                    {
                            new com.aspose.imaging.Point[]{new com.aspose.imaging.Point(103, 326)},
                            new com.aspose.imaging.Point[]{new com.aspose.imaging.Point(280, 43)},
                            new com.aspose.imaging.Point[]{new com.aspose.imaging.Point(319, 86)},
                    });
        
            // 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 GraphCut clustering.
            maskingOptions.setMethod(com.aspose.imaging.masking.options.SegmentationMethod.GraphCut);
            maskingOptions.setDecompose(false);
            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("Gorilla.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();
        }
        

      • getArgs

        public final IMaskingArgs getArgs()

        Gets the arguments for segmentation algorithm.

        Value: The arguments for segmentation algorithm.
        Returns:
        the arguments for segmentation algorithm.
      • setArgs

        public final void setArgs(IMaskingArgs value)

        Sets the arguments for segmentation algorithm.

        Value: The arguments for segmentation algorithm.
        Parameters:
        value - the arguments for segmentation algorithm.
      • getExportOptions

        public final ImageOptionsBase getExportOptions()

        Gets the image export options.

        Value: The image export options that will be used to create the resulting images.
        Returns:
        the image export options.
      • setExportOptions

        public final void setExportOptions(ImageOptionsBase value)

        Sets the image export options.

        Value: The image export options that will be used to create the resulting images.
        Parameters:
        value - the image export options.
      • getMaskingArea

        public final Rectangle getMaskingArea()

        Gets the masking area.

        Value: The masking area which is a partial area of the source image. Rectangle.Empty value means full source image area.
        Returns:
        the masking area.
      • setMaskingArea

        public final void setMaskingArea(Rectangle value)

        Sets the masking area.

        Value: The masking area which is a partial area of the source image. Rectangle.Empty value means full source image area.
        Parameters:
        value - the masking area.
      • getDecompose

        public final boolean getDecompose()

        Gets a value indicating whether needless to separate each Shape from mask as individual object or as united object from mask separated from background.

        Value: true if decompose; otherwise, false.
        Returns:
        a value indicating whether needless to separate each Shape from mask as individual object or as united object from mask separated from background.
      • setDecompose

        public final void setDecompose(boolean value)

        Sets a value indicating whether needless to separate each Shape from mask as individual object or as united object from mask separated from background.

        Value: true if decompose; otherwise, false.
        Parameters:
        value - a value indicating whether needless to separate each Shape from mask as individual object or as united object from mask separated from background.
      • getBackgroundReplacementColor

        public final Color getBackgroundReplacementColor()

        Gets the background replacement color.

        Value: The background replacement color. This color will be used as background color in resulting images.
        Returns:
        the background replacement color.
      • setBackgroundReplacementColor

        public final void setBackgroundReplacementColor(Color value)

        Sets the background replacement color.

        Value: The background replacement color. This color will be used as background color in resulting images.
        Parameters:
        value - the background replacement color.