Packages

 

com.aspose.imaging.masking.options

Class AutoMaskingGraphCutOptions



  • public class AutoMaskingGraphCutOptions
    extends GraphCutMaskingOptions

    The GraphCut auto masking options.

    Code example:

    Saving image masking result with feathering based on image size. Image masking is performed using auto calculated default strokes. The Args property of AutoMaskingGraphCutOptions can be omitted since default strokes are placed there in the end.MaskingResult[] results;


    
    MaskingResult[] results; 
                
    try (RasterImage image = (RasterImage)Image.load("input.jpg"))
    {
        try (PngOptions pngOptions = new PngOptions())
        {
            pngOptions.setColorType(PngColorType.TruecolorWithAlpha);
            pngOptions.setSource(new FileCreateSource("tempFile"));
    
            AutoMaskingGraphCutOptions options = new AutoMaskingGraphCutOptions();
            options.setCalculateDefaultStrokes(true);
            options.setFeatheringRadius((Math.max(image.getWidth(), image.getHeight()) / 500) + 1);
            options.setMethod(SegmentationMethod.GraphCut);
            options.setDecompose(false);
            options.setExportOptions(pngOptions);
            options.setBackgroundReplacementColor(Color.getTransparent());
    
            results = new ImageMasking(image).decompose(options);
        }
    }
    
    try (RasterImage resultImage = (RasterImage)results[1].getImage())
    {
        PngOptions pngOptions = new PngOptions();
        pngOptions.setColorType(PngColorType.TruecolorWithAlpha);
        resultImage.save("output.png", pngOptions);
    }
    
    // release resources
    for (MaskingResult res : results)
    {
        res.close();
    }
                
    

    • Constructor Detail

      • AutoMaskingGraphCutOptions

        public AutoMaskingGraphCutOptions()

        Initializes a new instance of the AutoMaskingGraphCutOptions class.

    • Method Detail

      • getDefaultForegroundStrokes

        public final Point[] getDefaultForegroundStrokes()

        Gets the pre-calculated default foreground strokes.

        Returns:
        the pre-calculated default foreground strokes.
        Code example:

        Saving image masking result with feathering based on image size and re-using masking options for the new masking iteration. Image masking is performed using auto calculated default strokes. Additionally the data of the two assumed objects is also specified in the AssumedObjects property of the AutoMaskingGraphCutOptions. After getting initial masking result, applied background/foreground strokes are modified and another masking iteration is performed.


        List<AssumedObjectData> assumedObjects = new LinkedList<AssumedObjectData>();
        assumedObjects.add(new AssumedObjectData(DetectedObjectType.Human, new Rectangle(100, 100, 150, 300)));
        assumedObjects.add(new AssumedObjectData(DetectedObjectType.Dog, new Rectangle(300, 100, 50, 30)));
        
        MaskingResult[] results;
        AutoMaskingGraphCutOptions options = new AutoMaskingGraphCutOptions();
        try (RasterImage image = (RasterImage)Image.load("input.jpg"))
        {
            try (PngOptions pngOptions = new PngOptions())
            {
                pngOptions.setColorType(PngColorType.TruecolorWithAlpha);
                pngOptions.setSource(new FileCreateSource("tempFile"));
        
                options.setAssumedObjects(assumedObjects);
                options.setCalculateDefaultStrokes(true);
                options.setFeatheringRadius(3);
                options.setMethod(SegmentationMethod.GraphCut);
                options.setDecompose(false);
                options.setExportOptions(pngOptions);
                options.setBackgroundReplacementColor(Color.getTransparent());
        
                results = new ImageMasking(image).decompose(options);
            }
        }
        
        // At this point applied foreground/background strokes can be analyzed and based on it additional
        // foreground/background strokes can be manually provided.
        Point[] appliedBackgroundStrokes = options.getDefaultBackgroundStrokes();
        Point[] appliedForegroundStrokes = options.getDefaultForegroundStrokes();
        Rectangle[] appliedObjectRectangles = options.getDefaultObjectsRectangles();
        try (RasterImage resultImage = (RasterImage)results[1].getImage())
        {
            PngOptions pngOptions = new PngOptions();
            pngOptions.setColorType(PngColorType.TruecolorWithAlpha);
            resultImage.save("output.png", pngOptions);
        }
        
        // release resources
        for (MaskingResult res : results)
        {
            res.close();
        }
        
        try (RasterImage image = (RasterImage)Image.load("input.jpg"))
        {
            // Re-using AutoMaskingGraphCutOptions there is no need to perform default strokes calculations second time.
            options.setCalculateDefaultStrokes(false);
            // When both default strokes and ObjectsPoints in the Args property of AutoMaskingArgs are provided, Point arrays are end up combined.
            // The first ObjectsPoints array is considered to be a background points array and
            // the second ObjectsPoints array is considered to be a foreground points array.
            // When both DefaultObjectsRectangles and ObjectsRectangles in the Args property of AutoMaskingArgs are provided,
            // only the array from the Args is being used.
            AutoMaskingArgs args = new AutoMaskingArgs();
            args.setObjectsPoints(new Point[][]
                    {
                            new Point[] { new Point(100, 100), new Point(150, 100) },
                            new Point[] { new Point(500, 200) },
                    });
        
            args.setObjectsRectangles( new Rectangle[] { new Rectangle(100, 100, 300, 300) });
            options.setArgs(args);
            results = new ImageMasking(image).decompose(options);
        }
        
        try (RasterImage resultImage = (RasterImage)results[1].getImage())
        {
            PngOptions pngOptions = new PngOptions();
            pngOptions.setColorType(PngColorType.TruecolorWithAlpha);
            resultImage.save("output.png", pngOptions);
        }
        
        // release resources
        for (MaskingResult res : results)
        {
            res.close();
        }
        
        

      • getDefaultBackgroundStrokes

        public final Point[] getDefaultBackgroundStrokes()

        Gets the default background strokes.

        Returns:
        the default background strokes.
        Code example:

        Saving image masking result with feathering based on image size and re-using masking options for the new masking iteration. Image masking is performed using auto calculated default strokes. Additionally the data of the two assumed objects is also specified in the AssumedObjects property of the AutoMaskingGraphCutOptions. After getting initial masking result, applied background/foreground strokes are modified and another masking iteration is performed.


        List<AssumedObjectData> assumedObjects = new LinkedList<AssumedObjectData>();
        assumedObjects.add(new AssumedObjectData(DetectedObjectType.Human, new Rectangle(100, 100, 150, 300)));
        assumedObjects.add(new AssumedObjectData(DetectedObjectType.Dog, new Rectangle(300, 100, 50, 30)));
        
        MaskingResult[] results;
        AutoMaskingGraphCutOptions options = new AutoMaskingGraphCutOptions();
        try (RasterImage image = (RasterImage)Image.load("input.jpg"))
        {
            try (PngOptions pngOptions = new PngOptions())
            {
                pngOptions.setColorType(PngColorType.TruecolorWithAlpha);
                pngOptions.setSource(new FileCreateSource("tempFile"));
        
                options.setAssumedObjects(assumedObjects);
                options.setCalculateDefaultStrokes(true);
                options.setFeatheringRadius(3);
                options.setMethod(SegmentationMethod.GraphCut);
                options.setDecompose(false);
                options.setExportOptions(pngOptions);
                options.setBackgroundReplacementColor(Color.getTransparent());
        
                results = new ImageMasking(image).decompose(options);
            }
        }
        
        // At this point applied foreground/background strokes can be analyzed and based on it additional
        // foreground/background strokes can be manually provided.
        Point[] appliedBackgroundStrokes = options.getDefaultBackgroundStrokes();
        Point[] appliedForegroundStrokes = options.getDefaultForegroundStrokes();
        Rectangle[] appliedObjectRectangles = options.getDefaultObjectsRectangles();
        try (RasterImage resultImage = (RasterImage)results[1].getImage())
        {
            PngOptions pngOptions = new PngOptions();
            pngOptions.setColorType(PngColorType.TruecolorWithAlpha);
            resultImage.save("output.png", pngOptions);
        }
        
        // release resources
        for (MaskingResult res : results)
        {
            res.close();
        }
        
        try (RasterImage image = (RasterImage)Image.load("input.jpg"))
        {
            // Re-using AutoMaskingGraphCutOptions there is no need to perform default strokes calculations second time.
            options.setCalculateDefaultStrokes(false);
            // When both default strokes and ObjectsPoints in the Args property of AutoMaskingArgs are provided, Point arrays are end up combined.
            // The first ObjectsPoints array is considered to be a background points array and
            // the second ObjectsPoints array is considered to be a foreground points array.
            // When both DefaultObjectsRectangles and ObjectsRectangles in the Args property of AutoMaskingArgs are provided,
            // only the array from the Args is being used.
            AutoMaskingArgs args = new AutoMaskingArgs();
            args.setObjectsPoints(new Point[][]
                    {
                            new Point[] { new Point(100, 100), new Point(150, 100) },
                            new Point[] { new Point(500, 200) },
                    });
        
            args.setObjectsRectangles( new Rectangle[] { new Rectangle(100, 100, 300, 300) });
            options.setArgs(args);
            results = new ImageMasking(image).decompose(options);
        }
        
        try (RasterImage resultImage = (RasterImage)results[1].getImage())
        {
            PngOptions pngOptions = new PngOptions();
            pngOptions.setColorType(PngColorType.TruecolorWithAlpha);
            resultImage.save("output.png", pngOptions);
        }
        
        // release resources
        for (MaskingResult res : results)
        {
            res.close();
        }
        
        

      • getDefaultObjectsRectangles

        public final Rectangle[] getDefaultObjectsRectangles()

        Gets the default objects rectangles.

        Returns:
        the default objects rectangles.
        Code example:

        Saving image masking result with feathering based on image size and re-using masking options for the new masking iteration. Image masking is performed using auto calculated default strokes. Additionally the data of the two assumed objects is also specified in the AssumedObjects property of the AutoMaskingGraphCutOptions. After getting initial masking result, applied background/foreground strokes are modified and another masking iteration is performed.


        List<AssumedObjectData> assumedObjects = new LinkedList<AssumedObjectData>();
        assumedObjects.add(new AssumedObjectData(DetectedObjectType.Human, new Rectangle(100, 100, 150, 300)));
        assumedObjects.add(new AssumedObjectData(DetectedObjectType.Dog, new Rectangle(300, 100, 50, 30)));
        
        MaskingResult[] results;
        AutoMaskingGraphCutOptions options = new AutoMaskingGraphCutOptions();
        try (RasterImage image = (RasterImage)Image.load("input.jpg"))
        {
            try (PngOptions pngOptions = new PngOptions())
            {
                pngOptions.setColorType(PngColorType.TruecolorWithAlpha);
                pngOptions.setSource(new FileCreateSource("tempFile"));
        
                options.setAssumedObjects(assumedObjects);
                options.setCalculateDefaultStrokes(true);
                options.setFeatheringRadius(3);
                options.setMethod(SegmentationMethod.GraphCut);
                options.setDecompose(false);
                options.setExportOptions(pngOptions);
                options.setBackgroundReplacementColor(Color.getTransparent());
        
                results = new ImageMasking(image).decompose(options);
            }
        }
        
        // At this point applied foreground/background strokes can be analyzed and based on it additional
        // foreground/background strokes can be manually provided.
        Point[] appliedBackgroundStrokes = options.getDefaultBackgroundStrokes();
        Point[] appliedForegroundStrokes = options.getDefaultForegroundStrokes();
        Rectangle[] appliedObjectRectangles = options.getDefaultObjectsRectangles();
        try (RasterImage resultImage = (RasterImage)results[1].getImage())
        {
            PngOptions pngOptions = new PngOptions();
            pngOptions.setColorType(PngColorType.TruecolorWithAlpha);
            resultImage.save("output.png", pngOptions);
        }
        
        // release resources
        for (MaskingResult res : results)
        {
            res.close();
        }
        
        try (RasterImage image = (RasterImage)Image.load("input.jpg"))
        {
            // Re-using AutoMaskingGraphCutOptions there is no need to perform default strokes calculations second time.
            options.setCalculateDefaultStrokes(false);
            // When both default strokes and ObjectsPoints in the Args property of AutoMaskingArgs are provided, Point arrays are end up combined.
            // The first ObjectsPoints array is considered to be a background points array and
            // the second ObjectsPoints array is considered to be a foreground points array.
            // When both DefaultObjectsRectangles and ObjectsRectangles in the Args property of AutoMaskingArgs are provided,
            // only the array from the Args is being used.
            AutoMaskingArgs args = new AutoMaskingArgs();
            args.setObjectsPoints(new Point[][]
                    {
                            new Point[] { new Point(100, 100), new Point(150, 100) },
                            new Point[] { new Point(500, 200) },
                    });
        
            args.setObjectsRectangles( new Rectangle[] { new Rectangle(100, 100, 300, 300) });
            options.setArgs(args);
            results = new ImageMasking(image).decompose(options);
        }
        
        try (RasterImage resultImage = (RasterImage)results[1].getImage())
        {
            PngOptions pngOptions = new PngOptions();
            pngOptions.setColorType(PngColorType.TruecolorWithAlpha);
            resultImage.save("output.png", pngOptions);
        }
        
        // release resources
        for (MaskingResult res : results)
        {
            res.close();
        }
        
        

      • getAssumedObjects

        public final List<AssumedObjectData> getAssumedObjects()

        Gets the assumed objects.

        Returns:
        the assumed objects.
      • setAssumedObjects

        public final void setAssumedObjects(List<AssumedObjectData> value)

        Sets the assumed objects.

        Parameters:
        value - the assumed objects.
      • getCalculateDefaultStrokes

        public final boolean getCalculateDefaultStrokes()

        Gets a value indicating whether default strokes should be calculated.

        Returns:
        a value indicating whether default strokes should be calculated.
      • setCalculateDefaultStrokes

        public final void setCalculateDefaultStrokes(boolean value)

        Sets a value indicating whether default strokes should be calculated.

        Parameters:
        value - a value indicating whether default strokes should be calculated.
        Code example:

        Saving image masking result with feathering based on image size. Image masking is performed using auto calculated default strokes. The Args property of AutoMaskingGraphCutOptions can be omitted since default strokes are placed there in the end.MaskingResult[] results;


        
        MaskingResult[] results; 
                    
        try (RasterImage image = (RasterImage)Image.load("input.jpg"))
        {
            try (PngOptions pngOptions = new PngOptions())
            {
                pngOptions.setColorType(PngColorType.TruecolorWithAlpha);
                pngOptions.setSource(new FileCreateSource("tempFile"));
        
                AutoMaskingGraphCutOptions options = new AutoMaskingGraphCutOptions();
                options.setCalculateDefaultStrokes(true);
                options.setFeatheringRadius((Math.max(image.getWidth(), image.getHeight()) / 500) + 1);
                options.setMethod(SegmentationMethod.GraphCut);
                options.setDecompose(false);
                options.setExportOptions(pngOptions);
                options.setBackgroundReplacementColor(Color.getTransparent());
        
                results = new ImageMasking(image).decompose(options);
            }
        }
        
        try (RasterImage resultImage = (RasterImage)results[1].getImage())
        {
            PngOptions pngOptions = new PngOptions();
            pngOptions.setColorType(PngColorType.TruecolorWithAlpha);
            resultImage.save("output.png", pngOptions);
        }
        
        // release resources
        for (MaskingResult res : results)
        {
            res.close();
        }
                    
        

      • getPrecalculationProgressEventHandler

        public final ProgressEventHandler getPrecalculationProgressEventHandler()

        Gets the default points pre-calculation process progress event handler.

        Value: The progress event handler.
        Returns:
        the default points pre-calculation process progress event handler.
      • setPrecalculationProgressEventHandler

        public final void setPrecalculationProgressEventHandler(ProgressEventHandler value)

        Sets the default points pre-calculation process progress event handler.

        Value: The progress event handler.
        Parameters:
        value - the default points pre-calculation process progress event handler.