Packages

 

com.aspose.imaging.masking.options

Class AutoMaskingArgs

  • All Implemented Interfaces:
    IMaskingArgs


    public class AutoMaskingArgs
    extends Object
    implements IMaskingArgs

    Represents the arguments that are specified for automated masking methods

    See Also:
    IMaskingArgs
    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

      • AutoMaskingArgs

        public AutoMaskingArgs()
    • Method Detail

      • getNumberOfObjects

        public final int getNumberOfObjects()

        Gets the number of objects to separate initial image to (optional), default value is 2 (object and background).

        Value: The number of objects.
        Returns:
        the number of objects to separate initial image to (optional), default value is 2 (object and background).
      • setNumberOfObjects

        public final void setNumberOfObjects(int value)

        Sets the number of objects to separate initial image to (optional), default value is 2 (object and background).

        Value: The number of objects.
        Parameters:
        value - the number of objects to separate initial image to (optional), default value is 2 (object and background).
      • getObjectsRectangles

        public final Rectangle[] getObjectsRectangles()

        Gets the objects rectangles that belong to separated objects (optional). This parameter is used to increase segmentation method precision.

        Value: The objects rectangles.
        Returns:
        the objects rectangles that belong to separated objects (optional).
      • setObjectsRectangles

        public final void setObjectsRectangles(Rectangle[] value)

        Sets the objects rectangles that belong to separated objects (optional). This parameter is used to increase segmentation method precision.

        Value: The objects rectangles.
        Parameters:
        value - the objects rectangles that belong to separated objects (optional).
      • getObjectsPoints

        public final Point[][] getObjectsPoints()

        Gets the points that belong to separated objects (optional) NumberOfObjects coordinates that belong to NumberOfObjects objects of initial image. This parameter is used to increase segmentation method precision.

        Value: The objects points.
        Returns:
        the points that belong to separated objects (optional) NumberOfObjects coordinates that belong to NumberOfObjects objects of initial image.
      • setObjectsPoints

        public final void setObjectsPoints(Point[][] value)

        Sets the points that belong to separated objects (optional) NumberOfObjects coordinates that belong to NumberOfObjects objects of initial image. This parameter is used to increase segmentation method precision.

        Value: The objects points.
        Parameters:
        value - the points that belong to separated objects (optional) NumberOfObjects coordinates that belong to NumberOfObjects objects of initial image.
      • getOrphanedPoints

        public final Point[] getOrphanedPoints()

        Gets the points that no longer belong to any object (optional). This parameter is used only in case of re-segmentation.

        Value: The orphaned points.
        Returns:
        the points that no longer belong to any object (optional).
      • setOrphanedPoints

        public final void setOrphanedPoints(Point[] value)

        Sets the points that no longer belong to any object (optional). This parameter is used only in case of re-segmentation.

        Value: The orphaned points.
        Parameters:
        value - the points that no longer belong to any object (optional).
      • getPrecision

        public final double getPrecision()

        Gets the precision of segmentation method (optional).

        Value: The precision of segmentation method (optional).
        Returns:
        the precision of segmentation method (optional).
      • setPrecision

        public final void setPrecision(double value)

        Sets the precision of segmentation method (optional).

        Value: The precision of segmentation method (optional).
        Parameters:
        value - the precision of segmentation method (optional).
      • getMaxIterationNumber

        public final int getMaxIterationNumber()

        Gets the maximum number of iterations.

        Value: The maximum maximum number of iterations.
        Returns:
        the maximum number of iterations.
      • setMaxIterationNumber

        public final void setMaxIterationNumber(int value)

        Sets the maximum number of iterations.

        Value: The maximum maximum number of iterations.
        Parameters:
        value - the maximum number of iterations.