ImageMasking
内容
[
隐藏
]ImageMasking class
提供图像遮罩操作
public class ImageMasking
构造函数
姓名 | 描述 |
---|---|
ImageMasking(RasterImage) | 初始化ImageMasking 类. |
方法
姓名 | 描述 |
---|---|
CreateSession(MaskingOptions) | 创建可以执行再训练分解操作的屏蔽会话。 |
Decompose(MaskingOptions) | 使用指定的掩码选项执行分解操作 |
DecomposeAsync(MaskingOptions) | 使用指定的屏蔽选项创建异步分解任务。 |
LoadSession(Stream) | 从指定的流中加载会话。 |
LoadSession(string) | 从指定文件加载会话。 |
static ApplyMask(RasterImage, RasterImage, MaskingOptions) | 将遮罩应用到指定的源图像。 |
例子
使用分段掩码加快分段过程
[C#]
// 屏蔽导出选项
Aspose.Imaging.ImageOptions.PngOptions exportOptions = new Aspose.Imaging.ImageOptions.PngOptions();
exportOptions.ColorType = Aspose.Imaging.FileFormats.Png.PngColorType.TruecolorWithAlpha;
exportOptions.Source = new Aspose.Imaging.Sources.StreamSource(new System.IO.MemoryStream());
Aspose.Imaging.Masking.Options.MaskingOptions maskingOptions = new Aspose.Imaging.Masking.Options.MaskingOptions();
// 使用 GraphCut 聚类。
maskingOptions.Method = Masking.Options.SegmentationMethod.GraphCut;
maskingOptions.Decompose = false;
maskingOptions.Args = new Aspose.Imaging.Masking.Options.AutoMaskingArgs();
// 背景颜色将是透明的。
maskingOptions.BackgroundReplacementColor = Aspose.Imaging.Color.Transparent;
maskingOptions.ExportOptions = exportOptions;
string dir = "c:\\temp\\";
using (Aspose.Imaging.RasterImage image = (Aspose.Imaging.RasterImage)Aspose.Imaging.Image.Load(dir + "BigImage.jpg"))
{
Aspose.Imaging.Size imageSize = image.Size;
// 减小图像大小以加快分割过程
image.ResizeHeightProportionally(600, Aspose.Imaging.ResizeType.HighQualityResample);
// 创建 ImageMasking 类的实例。
Aspose.Imaging.Masking.ImageMasking masking = new Aspose.Imaging.Masking.ImageMasking(image);
// 将源图像分成几个簇(段)。
using (Aspose.Imaging.Masking.Result.MaskingResult maskingResult = masking.Decompose(maskingOptions))
{
// 获取前景蒙版
using (Aspose.Imaging.RasterImage foregroundMask = maskingResult[1].GetMask())
{
// 将蒙版的大小增加到原始图像的大小
foregroundMask.Resize(imageSize.Width, imageSize.Height, Aspose.Imaging.ResizeType.NearestNeighbourResample);
// 将掩码应用于原始图像以获得前景段
using (Aspose.Imaging.RasterImage originImage = (Aspose.Imaging.RasterImage)Aspose.Imaging.Image.Load(dir + "BigImage.jpg"))
{
Aspose.Imaging.Masking.ImageMasking.ApplyMask(originImage, foregroundMask, maskingOptions);
originImage.Save(dir + "BigImage_foreground.png", exportOptions);
}
}
}
}
将屏蔽会话保存到文件以用于长时间会话,以及在另一个环境中恢复会话的可能性。
[C#]
string dir = "c:\\temp\\";
string sessionBackupFile = dir + "session.bak";
// 屏蔽导出选项
Aspose.Imaging.ImageOptions.PngOptions exportOptions = new Aspose.Imaging.ImageOptions.PngOptions();
exportOptions.ColorType = Aspose.Imaging.FileFormats.Png.PngColorType.TruecolorWithAlpha;
exportOptions.Source = new Aspose.Imaging.Sources.StreamSource(new System.IO.MemoryStream());
Aspose.Imaging.Masking.Options.MaskingOptions maskingOptions = new Aspose.Imaging.Masking.Options.MaskingOptions();
// 使用 GraphCut 聚类。
maskingOptions.Method = Masking.Options.SegmentationMethod.GraphCut;
maskingOptions.Decompose = false;
maskingOptions.Args = new Aspose.Imaging.Masking.Options.AutoMaskingArgs();
// 背景颜色为橙色。
maskingOptions.BackgroundReplacementColor = Aspose.Imaging.Color.Orange;
maskingOptions.ExportOptions = exportOptions;
// 首次启动会话并保存到文件
using (Aspose.Imaging.RasterImage image = (Aspose.Imaging.RasterImage)Aspose.Imaging.Image.Load(dir + "Gorilla.bmp"))
{
// 创建 ImageMasking 类的实例。
Aspose.Imaging.Masking.ImageMasking masking = new Aspose.Imaging.Masking.ImageMasking(image);
using (Aspose.Imaging.Masking.IMaskingSession session = masking.CreateSession(maskingOptions))
{
using (Aspose.Imaging.Masking.Result.MaskingResult maskingResult = session.Decompose())
{
using (Aspose.Imaging.RasterImage segmentImage = maskingResult[1].GetImage())
{
segmentImage.Save(dir + "step1.png");
}
}
session.Save(sessionBackupFile);
}
}
// 从文件恢复屏蔽会话
using (Aspose.Imaging.RasterImage image = (Aspose.Imaging.RasterImage)Aspose.Imaging.Image.Load(dir + "Gorilla.bmp"))
{
// 创建 ImageMasking 类的实例。
Aspose.Imaging.Masking.ImageMasking masking = new Aspose.Imaging.Masking.ImageMasking(image);
using (Aspose.Imaging.Masking.IMaskingSession session = masking.LoadSession(sessionBackupFile))
{
Aspose.Imaging.Masking.Options.AutoMaskingArgs args = new Aspose.Imaging.Masking.Options.AutoMaskingArgs();
// 对图像进行可视化分析,并设置属于分离对象的点。
args.ObjectsPoints = new Point[][]
{
new Point[]
{
new Point(0, 0), new Point(0, 1), new Point(1, 0),
new Point(1, 1), new Point(2, 0), new Point(2, 1),
new Point(3, 0), new Point(3, 1)
},
};
using (Aspose.Imaging.Masking.Result.MaskingResult maskingResult = session.ImproveDecomposition(args))
{
// 导出选项的显式传输,因为它不可序列化
maskingResult.MaskingOptions.ExportOptions = exportOptions;
using (Aspose.Imaging.RasterImage segmentImage = maskingResult[1].GetImage())
{
segmentImage.Save(dir + "step2.png");
}
}
}
}