AutoMaskingArgs
内容
[
隐藏
]AutoMaskingArgs class
表示为自动屏蔽方法指定的参数
public class AutoMaskingArgs : IMaskingArgs
构造函数
姓名 | 描述 |
---|---|
AutoMaskingArgs() | 默认构造函数。 |
特性
姓名 | 描述 |
---|---|
MaxIterationNumber { get; set; } | 获取或设置最大迭代次数。 |
NumberOfObjects { get; set; } | 获取或设置对象的数量 将初始图像分隔为(可选),默认值为2(对象和背景)。 |
ObjectsPoints { get; set; } | 获取或设置属于分离对象的点(可选) NumberOfObjects 属于初始图像的 NumberOfObjects 个对象的坐标。 该参数用于提高分割方法的精度。 |
ObjectsRectangles { get; set; } | 获取或设置属于分离对象的对象矩形(可选)。 该参数用于提高分割方法的精度。 |
OrphanedPoints { get; set; } | 获取或设置不再属于任何对象的点(可选)。 该参数仅用于重新分割的情况。 |
Precision { get; set; } | 获取或设置分割方法的精度(可选)。 |
例子
此示例说明如何使用图像掩蔽和 K-means 分割算法将光栅图像分解为多个图像。图像遮罩是一种图像处理技术,用于将背景与前景图像对象分开。
[C#]
string dir = "c:\\temp\\";
using (Aspose.Imaging.RasterImage image = (Aspose.Imaging.RasterImage)Aspose.Imaging.Image.Load(dir + "Blue hills.png"))
{
Aspose.Imaging.Masking.Options.AutoMaskingArgs args = new Aspose.Imaging.Masking.Options.AutoMaskingArgs();
// 设置簇(分离对象)的数量。默认值为 2,前景对象和背景。
args.NumberOfObjects = 3;
// 设置最大迭代次数。
args.MaxIterationNumber = 50;
// 设置分割方法的精度(可选)
args.Precision = 1;
// 每个簇(段)将被存储到一个单独的 PNG 文件中。
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();
// 使用 K-means 聚类。
// K-means 聚类允许将图像分割成几个独立的簇(段)。
maskingOptions.Method = Masking.Options.SegmentationMethod.KMeans;
maskingOptions.Decompose = true;
maskingOptions.Args = args;
// 背景颜色为橙色。
maskingOptions.BackgroundReplacementColor = Aspose.Imaging.Color.Orange;
maskingOptions.ExportOptions = exportOptions;
// 创建 ImageMasking 类的实例。
Aspose.Imaging.Masking.ImageMasking masking = new Aspose.Imaging.Masking.ImageMasking(image);
// 将源图像分成几个簇(段)。
using (Aspose.Imaging.Masking.Result.MaskingResult maskingResult = masking.Decompose(maskingOptions))
{
// 从遮罩结果中获取图像并将其保存为 PNG。
for (int i = 0; i < maskingResult.Length; i++)
{
string outputFileName = string.Format("Blue hills.Segment{0}.png", maskingResult[i].ObjectNumber);
using (Aspose.Imaging.Image resultImage = maskingResult[i].GetImage())
{
resultImage.Save(dir + outputFileName);
}
}
}
}
使用分段掩码加快分段过程
[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\\";
using (Aspose.Imaging.RasterImage image = (Aspose.Imaging.RasterImage)Aspose.Imaging.Image.Load(dir + "Gorilla.bmp"))
{
Aspose.Imaging.Masking.Options.AutoMaskingArgs args = new Aspose.Imaging.Masking.Options.AutoMaskingArgs();
// 建议 #1。
// 对图像进行可视化分析并设置感兴趣的区域。分割结果将仅包括将完全位于该区域内的对象。
args.ObjectsRectangles = new Rectangle[]
{
new Rectangle(86, 6, 270, 364),
};
// 建议 #2。
// 对图像进行可视化分析,并设置属于分离对象的点。
args.ObjectsPoints = new Point[][]
{
new Point[] { new Point(103, 326) },
new Point[] { new Point(280, 43) },
new Point[] { new Point(319, 86) },
};
// 每个簇(段)将被存储到一个单独的 PNG 文件中。
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 = args;
// 背景颜色为橙色。
maskingOptions.BackgroundReplacementColor = Aspose.Imaging.Color.Orange;
maskingOptions.ExportOptions = exportOptions;
// 创建 ImageMasking 类的实例。
Aspose.Imaging.Masking.ImageMasking masking = new Aspose.Imaging.Masking.ImageMasking(image);
// 将源图像分成几个簇(段)。
using (Aspose.Imaging.Masking.Result.MaskingResult maskingResult = masking.Decompose(maskingOptions))
{
// 从遮罩结果中获取图像并将其保存为 PNG。
for (int i = 0; i < maskingResult.Length; i++)
{
string outputFileName = string.Format("Gorilla.Segment{0}.png", maskingResult[i].ObjectNumber);
using (Aspose.Imaging.Image resultImage = maskingResult[i].GetImage())
{
resultImage.Save(dir + outputFileName);
}
}
}
}
将屏蔽会话保存到文件以用于长时间会话,以及在另一个环境中恢复会话的可能性。
[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");
}
}
}
}
也可以看看
- interface IMaskingArgs
- 命名空间 Aspose.Imaging.Masking.Options
- 部件 Aspose.Imaging