AutoMaskingGraphCutOptions
Inheritance: java.lang.Object, com.aspose.imaging.masking.options.MaskingOptions, com.aspose.imaging.masking.options.GraphCutMaskingOptions
public class AutoMaskingGraphCutOptions extends GraphCutMaskingOptions
The GraphCut auto masking options.
Constructors
Constructor | Description |
---|---|
AutoMaskingGraphCutOptions() | Initializes a new instance of the AutoMaskingGraphCutOptions class. |
Methods
Method | Description |
---|---|
getDefaultForegroundStrokes() | Gets the pre-calculated default foreground strokes. |
getDefaultBackgroundStrokes() | Gets the default background strokes. |
getDefaultObjectsRectangles() | Gets the default objects rectangles. |
getAssumedObjects() | Gets the assumed objects. |
setAssumedObjects(List | Sets the assumed objects. |
getCalculateDefaultStrokes() | Gets a value indicating whether default strokes should be calculated. |
setCalculateDefaultStrokes(boolean value) | Sets a value indicating whether default strokes should be calculated. |
getPrecalculationProgressEventHandler() | Gets the default points pre-calculation process progress event handler. |
setPrecalculationProgressEventHandler(ProgressEventHandler value) | Sets the default points pre-calculation process progress event handler. |
Example: Saving image masking result with feathering based on image size.
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();
}
Example: Saving image masking result with feathering based on image size.
Saving image masking result with feathering based on image size. 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.
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;
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.setAssumedObjects(assumedObjects);
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();
}
Example: Saving image masking result with feathering based on image size and re-using masking options for the new masking iteration.
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();
}
Example: Saving image masking result with feathering based on image size, modifying obtained default strokes and using it for the new masking iteration.
Saving image masking result with feathering based on image size, modifying obtained default strokes and using it 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 using new GraphCutMaskingOptions instance.
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();
}
appliedBackgroundStrokes[5] = new Point(100, 100);
appliedBackgroundStrokes[15] = new Point(150, 100);
appliedForegroundStrokes[1] = new Point(500, 200);
appliedObjectRectangles[0] = new Rectangle(100, 100, 300, 300);
try (RasterImage image = (RasterImage)Image.load("input.jpg"))
{
try (PngOptions pngOptions = new PngOptions())
{
pngOptions.setColorType(PngColorType.TruecolorWithAlpha);
pngOptions.setSource(new FileCreateSource("tempFile"));
AutoMaskingArgs args = new AutoMaskingArgs();
args.setObjectsPoints(new Point[][]
{
appliedBackgroundStrokes,
appliedForegroundStrokes
});
args.setObjectsRectangles(appliedObjectRectangles);
GraphCutMaskingOptions graphCutOptions = new GraphCutMaskingOptions();
graphCutOptions.setFeatheringRadius(3);
graphCutOptions.setMethod(SegmentationMethod.GraphCut);
graphCutOptions.setDecompose(false);
graphCutOptions.setExportOptions(pngOptions);
graphCutOptions.setBackgroundReplacementColor(Color.getTransparent());
graphCutOptions.setArgs(args);
results = new ImageMasking(image).decompose(graphCutOptions);
}
}
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();
}
AutoMaskingGraphCutOptions()
public AutoMaskingGraphCutOptions()
Initializes a new instance of the AutoMaskingGraphCutOptions class.
getDefaultForegroundStrokes()
public final Point[] getDefaultForegroundStrokes()
Gets the pre-calculated default foreground strokes.
Returns: com.aspose.imaging.Point[] - the pre-calculated default foreground strokes.
Example: Saving image masking result with feathering based on image size and re-using masking options for the new masking iteration. 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();
}
Example: Saving image masking result with feathering based on image size, modifying obtained default strokes and using it for the new masking iteration. Saving image masking result with feathering based on image size, modifying obtained default strokes and using it 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 using new GraphCutMaskingOptions instance.
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();
}
appliedBackgroundStrokes[5] = new Point(100, 100);
appliedBackgroundStrokes[15] = new Point(150, 100);
appliedForegroundStrokes[1] = new Point(500, 200);
appliedObjectRectangles[0] = new Rectangle(100, 100, 300, 300);
try (RasterImage image = (RasterImage)Image.load("input.jpg"))
{
try (PngOptions pngOptions = new PngOptions())
{
pngOptions.setColorType(PngColorType.TruecolorWithAlpha);
pngOptions.setSource(new FileCreateSource("tempFile"));
AutoMaskingArgs args = new AutoMaskingArgs();
args.setObjectsPoints(new Point[][]
{
appliedBackgroundStrokes,
appliedForegroundStrokes
});
args.setObjectsRectangles(appliedObjectRectangles);
GraphCutMaskingOptions graphCutOptions = new GraphCutMaskingOptions();
graphCutOptions.setFeatheringRadius(3);
graphCutOptions.setMethod(SegmentationMethod.GraphCut);
graphCutOptions.setDecompose(false);
graphCutOptions.setExportOptions(pngOptions);
graphCutOptions.setBackgroundReplacementColor(Color.getTransparent());
graphCutOptions.setArgs(args);
results = new ImageMasking(image).decompose(graphCutOptions);
}
}
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: com.aspose.imaging.Point[] - the default background strokes.
Example: Saving image masking result with feathering based on image size and re-using masking options for the new masking iteration. 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();
}
Example: Saving image masking result with feathering based on image size, modifying obtained default strokes and using it for the new masking iteration. Saving image masking result with feathering based on image size, modifying obtained default strokes and using it 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 using new GraphCutMaskingOptions instance.
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();
}
appliedBackgroundStrokes[5] = new Point(100, 100);
appliedBackgroundStrokes[15] = new Point(150, 100);
appliedForegroundStrokes[1] = new Point(500, 200);
appliedObjectRectangles[0] = new Rectangle(100, 100, 300, 300);
try (RasterImage image = (RasterImage)Image.load("input.jpg"))
{
try (PngOptions pngOptions = new PngOptions())
{
pngOptions.setColorType(PngColorType.TruecolorWithAlpha);
pngOptions.setSource(new FileCreateSource("tempFile"));
AutoMaskingArgs args = new AutoMaskingArgs();
args.setObjectsPoints(new Point[][]
{
appliedBackgroundStrokes,
appliedForegroundStrokes
});
args.setObjectsRectangles(appliedObjectRectangles);
GraphCutMaskingOptions graphCutOptions = new GraphCutMaskingOptions();
graphCutOptions.setFeatheringRadius(3);
graphCutOptions.setMethod(SegmentationMethod.GraphCut);
graphCutOptions.setDecompose(false);
graphCutOptions.setExportOptions(pngOptions);
graphCutOptions.setBackgroundReplacementColor(Color.getTransparent());
graphCutOptions.setArgs(args);
results = new ImageMasking(image).decompose(graphCutOptions);
}
}
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: com.aspose.imaging.Rectangle[] - the default objects rectangles.
Example: Saving image masking result with feathering based on image size and re-using masking options for the new masking iteration. 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();
}
Example: Saving image masking result with feathering based on image size, modifying obtained default strokes and using it for the new masking iteration. Saving image masking result with feathering based on image size, modifying obtained default strokes and using it 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 using new GraphCutMaskingOptions instance.
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();
}
appliedBackgroundStrokes[5] = new Point(100, 100);
appliedBackgroundStrokes[15] = new Point(150, 100);
appliedForegroundStrokes[1] = new Point(500, 200);
appliedObjectRectangles[0] = new Rectangle(100, 100, 300, 300);
try (RasterImage image = (RasterImage)Image.load("input.jpg"))
{
try (PngOptions pngOptions = new PngOptions())
{
pngOptions.setColorType(PngColorType.TruecolorWithAlpha);
pngOptions.setSource(new FileCreateSource("tempFile"));
AutoMaskingArgs args = new AutoMaskingArgs();
args.setObjectsPoints(new Point[][]
{
appliedBackgroundStrokes,
appliedForegroundStrokes
});
args.setObjectsRectangles(appliedObjectRectangles);
GraphCutMaskingOptions graphCutOptions = new GraphCutMaskingOptions();
graphCutOptions.setFeatheringRadius(3);
graphCutOptions.setMethod(SegmentationMethod.GraphCut);
graphCutOptions.setDecompose(false);
graphCutOptions.setExportOptions(pngOptions);
graphCutOptions.setBackgroundReplacementColor(Color.getTransparent());
graphCutOptions.setArgs(args);
results = new ImageMasking(image).decompose(graphCutOptions);
}
}
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: java.util.List<com.aspose.imaging.masking.options.AssumedObjectData> - the assumed objects.
setAssumedObjects(List value)
public final void setAssumedObjects(List<AssumedObjectData> value)
Sets the assumed objects.
Parameters:
Parameter | Type | Description |
---|---|---|
value | java.util.List<com.aspose.imaging.masking.options.AssumedObjectData> | the assumed objects. |
getCalculateDefaultStrokes()
public final boolean getCalculateDefaultStrokes()
Gets a value indicating whether default strokes should be calculated.
Returns: boolean - a value indicating whether default strokes should be calculated.
setCalculateDefaultStrokes(boolean value)
public final void setCalculateDefaultStrokes(boolean value)
Sets a value indicating whether default strokes should be calculated.
Parameters:
Parameter | Type | Description |
---|---|---|
value | boolean | a value indicating whether default strokes should be calculated. |
Example: Saving image masking result with feathering based on image size. 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();
}
Example: Saving image masking result with feathering based on image size. Saving image masking result with feathering based on image size. 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.
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;
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.setAssumedObjects(assumedObjects);
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();
}
Example: Saving image masking result with feathering based on image size and re-using masking options for the new masking iteration. 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();
}
Example: Saving image masking result with feathering based on image size, modifying obtained default strokes and using it for the new masking iteration. Saving image masking result with feathering based on image size, modifying obtained default strokes and using it 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 using new GraphCutMaskingOptions instance.
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();
}
appliedBackgroundStrokes[5] = new Point(100, 100);
appliedBackgroundStrokes[15] = new Point(150, 100);
appliedForegroundStrokes[1] = new Point(500, 200);
appliedObjectRectangles[0] = new Rectangle(100, 100, 300, 300);
try (RasterImage image = (RasterImage)Image.load("input.jpg"))
{
try (PngOptions pngOptions = new PngOptions())
{
pngOptions.setColorType(PngColorType.TruecolorWithAlpha);
pngOptions.setSource(new FileCreateSource("tempFile"));
AutoMaskingArgs args = new AutoMaskingArgs();
args.setObjectsPoints(new Point[][]
{
appliedBackgroundStrokes,
appliedForegroundStrokes
});
args.setObjectsRectangles(appliedObjectRectangles);
GraphCutMaskingOptions graphCutOptions = new GraphCutMaskingOptions();
graphCutOptions.setFeatheringRadius(3);
graphCutOptions.setMethod(SegmentationMethod.GraphCut);
graphCutOptions.setDecompose(false);
graphCutOptions.setExportOptions(pngOptions);
graphCutOptions.setBackgroundReplacementColor(Color.getTransparent());
graphCutOptions.setArgs(args);
results = new ImageMasking(image).decompose(graphCutOptions);
}
}
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: ProgressEventHandler - the default points pre-calculation process progress event handler.
setPrecalculationProgressEventHandler(ProgressEventHandler value)
public final void setPrecalculationProgressEventHandler(ProgressEventHandler value)
Sets the default points pre-calculation process progress event handler.
Value: The progress event handler.
Parameters:
Parameter | Type | Description |
---|---|---|
value | ProgressEventHandler | the default points pre-calculation process progress event handler. |