通过 C# 从 DICOM 中删除背景
构建您自己的 .NET 应用程序以使用服务器端 API 从 DICOM 文件中删除背景。
如何使用 C# 删除 DICOM 文件中的背景
从图像中去除背景涉及隔离前景对象,这是一项需要对象识别的任务。存在多种方法来识别 DICOM 格式照片中的对象。对于具有统一颜色背景的简单图像,自动方法就足够了。然而,对于具有多个或部分合并人物的照片,预先标记对象就变得必要。这涉及指定要删除的矩形区域和对象类型。在复杂的情况下,Cloud API 可以实现自动对象检测。 Cloud API 提供了一个云应用程序,能够识别照片中的对象,利用生成的轮廓来去除背景。去除后,可以平滑图形留下的边缘以提高图像质量。为了删除 DICOM 文件中的背景,我们将使用 Aspose.Imaging for .NET API 是一个功能丰富、功能强大 易于使用的 C# 平台图像处理和转换 API。打开 NuGet 包管理器,搜索 Aspose.Imaging 并安装。您还可以从包管理器控制台使用以下命令。
包管理器控制台命令
PM> Install-Package Aspose.Imaging
通过 C# 从 DICOM 中删除背景的步骤
你需要 aspose.imaging.dll 在您自己的环境中尝试以下工作流程。
- 使用 Image.Load 方法加载 DICOM 文件
- 移除背景;
- 以 Aspose.Imaging 支持的格式将图像保存到光盘
系统要求
所有主要操作系统都支持 .NET 的 Aspose.Imaging。只需确保您具有以下先决条件。
- Microsoft Windows 或具有 .NET Framework、.NET Core、Windows 应用程序、ASP.NET Web 应用程序的兼容操作系统。
- Microsoft Visual Studio 等开发环境。
- 项目中引用的 Aspose.Imaging for .NET。
删除 DICOM 图像中的背景 - .NET
using Aspose.Imaging; | |
using Aspose.Imaging.FileFormats.Emf; | |
using Aspose.Imaging.FileFormats.Png; | |
using Aspose.Imaging.FileFormats.Tiff.Enums; | |
using Aspose.Imaging.ImageOptions; | |
using Aspose.Imaging.Masking; | |
using Aspose.Imaging.Masking.Options; | |
using Aspose.Imaging.Masking.Result; | |
using Aspose.Imaging.Sources; | |
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
string templatesFolder = @"c:\Users\USER\Downloads"; | |
RemoveBackgroundGenericExample(); | |
void RemoveBackgroundProcessingWithManualRectangles() | |
{ | |
List<string> rasterFormats = new List<string>() { "jpg", "png", "bmp", "apng", "dicom", | |
"jp2", "j2k", "tga", "webp", "tif", "gif","ico" }; | |
List<string> vectorFormats = new List<string>() { "svg", "otg", "odg", "wmf", "emf", "wmz", "emz", "cmx", "cdr" }; | |
List<string> allFormats = new List<string>(rasterFormats); | |
allFormats.AddRange(vectorFormats); | |
allFormats.ForEach( | |
formatExt => | |
{ | |
var inputFile = Path.Combine(templatesFolder, $"couple.{formatExt}"); | |
bool isVectorFormat = vectorFormats.IndexOf(formatExt) > -1; | |
//Need to rasterize vector formats before background remove | |
if (isVectorFormat) | |
{ | |
inputFile = RasterizeVectorImage(formatExt, inputFile); | |
} | |
var outputFile = Path.Combine(templatesFolder, $"remove_background_manual_rectangles.{formatExt}"); | |
Console.WriteLine($"Processing {formatExt}"); | |
using (var image = (RasterImage)Image.Load(inputFile)) | |
{ | |
//Additional code examples can be found at | |
//https://docs.aspose.com/imaging/net/remove-background-from-images/#graph-cut-auto-masking-using-imagingcloud-api | |
var maskingOptions = new AutoMaskingGraphCutOptions | |
{ | |
FeatheringRadius = 2, | |
Method = SegmentationMethod.GraphCut, | |
Args = new AutoMaskingArgs() | |
{ | |
ObjectsRectangles = new Aspose.Imaging.Rectangle[] | |
{ | |
// girl's bound box | |
new Aspose.Imaging.Rectangle(87, 47, 123, 308), | |
// boy's bound box | |
new Aspose.Imaging.Rectangle(180, 24, 126, 224) | |
} | |
}, | |
ExportOptions = new PngOptions | |
{ | |
ColorType = PngColorType.TruecolorWithAlpha, | |
Source = new FileCreateSource(outputFile, false) | |
} | |
}; | |
using (var maskingSession = new ImageMasking(image).CreateSession(maskingOptions)) | |
{ | |
// first run of segmentation | |
using (maskingSession.Decompose()) { } | |
var argsWithUserMarkers = new AutoMaskingArgs() | |
{ | |
ObjectsPoints = new Point[][] | |
{ | |
// background markers | |
null, | |
// foreground markers | |
new UserMarker() | |
// boy's head | |
.AddPoint(218, 48, 10) | |
// girl's head | |
.AddPoint(399, 66, 10) | |
// girs's body | |
.AddPoint(158, 141, 10) | |
.AddPoint(158, 209, 20) | |
.AddPoint(115, 225, 5) | |
.GetPoints() | |
} | |
}; | |
using (var maskingResult = maskingSession.ImproveDecomposition(argsWithUserMarkers)) | |
{ | |
using (var resultImage = maskingResult[1].GetImage()) | |
{ | |
resultImage.Save(); | |
} | |
} | |
} | |
} | |
File.Delete(outputFile); | |
//Remove rasterized vector image | |
if (isVectorFormat) | |
{ | |
File.Delete(inputFile); | |
} | |
} | |
); | |
} | |
void RemoveBackgroundAutoProcessingWithAssumedObjects() | |
{ | |
List<string> rasterFormats = new List<string>() { "jpg", "png", "bmp", "apng", "dicom", | |
"jp2", "j2k", "tga", "webp", "tif", "gif" }; | |
List<string> vectorFormats = new List<string>() { "svg", "otg", "odg", "eps", "wmf", "emf", "wmz", "emz", "cmx", "cdr" }; | |
List<string> allFormats = new List<string>(rasterFormats); | |
allFormats.AddRange(vectorFormats); | |
allFormats.ForEach( | |
formatExt => | |
{ | |
var inputFile = Path.Combine(templatesFolder, $"couple.{formatExt}"); | |
bool isVectorFormat = vectorFormats.IndexOf(formatExt) > -1; | |
//Need to rasterize vector formats before background remove | |
if (isVectorFormat) | |
{ | |
inputFile = RasterizeVectorImage(formatExt, inputFile); | |
} | |
var outputFile = Path.Combine(templatesFolder, $"remove_background_auto_assumed_objects.{formatExt}"); | |
Console.WriteLine($"Processing {formatExt}"); | |
using (var image = (RasterImage)Image.Load(inputFile)) | |
{ | |
//Additional code examples can be found at | |
//https://docs.aspose.com/imaging/net/remove-background-from-images/#graph-cut-auto-masking-using-imagingcloud-api | |
var maskingOptions = new AutoMaskingGraphCutOptions | |
{ | |
AssumedObjects = new List<AssumedObjectData> | |
{ | |
// girl's bound box | |
new AssumedObjectData(DetectedObjectType.Human, new Aspose.Imaging.Rectangle(87, 47, 123, 308)), | |
// boy's bound box | |
new AssumedObjectData(DetectedObjectType.Human, new Aspose.Imaging.Rectangle(180, 24, 126, 224)), | |
}, | |
CalculateDefaultStrokes = true, | |
FeatheringRadius = 1, | |
Method = SegmentationMethod.GraphCut, | |
ExportOptions = new PngOptions | |
{ | |
ColorType = PngColorType.TruecolorWithAlpha, | |
Source = new FileCreateSource(outputFile, false) | |
}, | |
BackgroundReplacementColor = Color.Green | |
}; | |
using (var maskingResult = new ImageMasking(image).Decompose(maskingOptions)) | |
{ | |
using (var resultImage = maskingResult[1].GetImage()) | |
{ | |
resultImage.Save(); | |
} | |
} | |
} | |
//Remove rasterized vector image | |
if (isVectorFormat) | |
{ | |
File.Delete(inputFile); | |
} | |
File.Delete(outputFile); | |
} | |
); | |
} | |
void RemoveBackgroundAutoProcessing() | |
{ | |
List<string> rasterFormats = new List<string>() { "jpg", "png", "bmp", "apng", "dicom", | |
"jp2", "j2k", "tga", "webp", "tif", "gif" }; | |
List<string> vectorFormats = new List<string>() { "svg", "otg", "odg", "eps", "wmf", "emf", "wmz", "emz", "cmx", "cdr" }; | |
List<string> allFormats = new List<string>(rasterFormats); | |
allFormats.AddRange(vectorFormats); | |
allFormats.ForEach( | |
formatExt => | |
{ | |
var inputFile = Path.Combine(templatesFolder, $"couple.{formatExt}"); | |
bool isVectorFormat = vectorFormats.IndexOf(formatExt) > -1; | |
//Need to rasterize vector formats before background remove | |
if (isVectorFormat) | |
{ | |
inputFile = RasterizeVectorImage(formatExt, inputFile); | |
} | |
var outputFile = Path.Combine(templatesFolder, $"remove_background_auto.{formatExt}"); | |
Console.WriteLine($"Processing {formatExt}"); | |
using (var image = (RasterImage)Image.Load(inputFile)) | |
{ | |
//Additional code examples can be found at | |
//https://docs.aspose.com/imaging/net/remove-background-from-images/#graph-cut-auto-masking-using-imagingcloud-api | |
var maskingOptions = new AutoMaskingGraphCutOptions | |
{ | |
FeatheringRadius = 1, | |
Method = SegmentationMethod.GraphCut, | |
ExportOptions = new PngOptions | |
{ | |
ColorType = PngColorType.TruecolorWithAlpha, | |
Source = new FileCreateSource(outputFile, false) | |
}, | |
BackgroundReplacementColor = Color.Green | |
}; | |
using (var maskingResult = new ImageMasking(image).Decompose(maskingOptions)) | |
{ | |
using (var resultImage = maskingResult[1].GetImage()) | |
{ | |
resultImage.Save(); | |
} | |
} | |
} | |
//Remove rasterized vector image | |
if (isVectorFormat) | |
{ | |
File.Delete(inputFile); | |
} | |
File.Delete(outputFile); | |
} | |
); | |
} | |
void RemoveBackgroundGenericExample() | |
{ | |
List<string> rasterFormats = new List<string>() { "jpg", "png", "bmp", "apng", "dicom", | |
"jp2", "j2k", "tga", "webp", "tif", "gif" }; | |
List<string> vectorFormats = new List<string>() { "svg", "otg", "odg", "wmf", "emf", "wmz", "emz", "cmx", "cdr" }; | |
List<string> allFormats = new List<string>(rasterFormats); | |
allFormats.AddRange(vectorFormats); | |
allFormats.ForEach( | |
formatExt => | |
{ | |
var inputFile = Path.Combine(templatesFolder, $"couple.{formatExt}"); | |
bool isVectorFormat = vectorFormats.IndexOf(formatExt) > -1; | |
//Need to rasterize vector formats before background remove | |
if (isVectorFormat) | |
{ | |
inputFile = RasterizeVectorImage(formatExt, inputFile); | |
} | |
var outputFile = Path.Combine(templatesFolder, $"remove_background.{formatExt}"); | |
Console.WriteLine($"Processing {formatExt}"); | |
using (var image = (RasterImage)Image.Load(inputFile)) | |
{ | |
//Additional code examples can be found at | |
//https://docs.aspose.com/imaging/net/remove-background-from-images/#graph-cut-auto-masking-using-imagingcloud-api | |
var maskingOptions = new AutoMaskingGraphCutOptions | |
{ | |
CalculateDefaultStrokes = true, | |
FeatheringRadius = 1, | |
Method = SegmentationMethod.GraphCut, | |
ExportOptions = new PngOptions | |
{ | |
ColorType = PngColorType.TruecolorWithAlpha, | |
Source = new FileCreateSource(outputFile, false) | |
}, | |
BackgroundReplacementColor = Color.Green | |
}; | |
using (var maskingResult = new ImageMasking(image).Decompose(maskingOptions)) | |
{ | |
using (var resultImage = maskingResult[1].GetImage()) | |
{ | |
resultImage.Save(); | |
} | |
} | |
} | |
//Remove rasterized vector image | |
if (isVectorFormat) | |
{ | |
File.Delete(inputFile); | |
} | |
File.Delete(outputFile); | |
} | |
); | |
} | |
string RasterizeVectorImage(string formatExt, string inputFile) | |
{ | |
string outputFile = Path.Combine(templatesFolder, $"rasterized.{formatExt}.png"); | |
using (var image = Image.Load(inputFile)) | |
{ | |
image.Save(outputFile, new PngOptions()); | |
} | |
return outputFile; | |
} | |
class UserMarker | |
{ | |
private readonly List<Point> _list = new List<Point>(); | |
public UserMarker AddPoint(int left, int top, int radius) | |
{ | |
for (var y = top - radius; y <= top + radius; y++) | |
{ | |
for (var x = left - radius; x <= left + radius; x++) | |
{ | |
this._list.Add(new Point(x, y)); | |
} | |
} | |
return this; | |
} | |
public Point[] GetPoints() | |
{ | |
return this._list.ToArray(); | |
} | |
} |
关于 .NET API 的 Aspose.Imaging
Aspose.Imaging API 是一种图像处理解决方案,用于在应用程序中创建、修改、绘制或转换图像(照片)。它提供:跨平台的图像处理,包括但不限于各种图像格式之间的转换(包括统一的多页或多帧图像处理)、绘图等修改、使用图形基元、转换(调整大小、裁剪、翻转和旋转) 、二值化、灰度、调整)、高级图像处理功能(过滤、抖动、遮罩、去偏斜)和内存优化策略。它是一个独立的库,不依赖任何软件进行图像操作。可以在项目中使用原生 API 轻松添加高性能图像转换功能。这些是 100% 私有的本地 API,图像在您的服务器上处理。通过在线应用删除 DICOM 中的背景
通过访问我们的 Live Demos 网站 移除 DICOM 文档中的背景。 现场演示有以下好处
DICOM 什么是 DICOM 文件格式
DICOM 是 Digital Imaging and Communications in Medicine 的首字母缩写词,属于医学信息学领域。 DICOM 是文件格式定义和网络通信协议的结合。 DICOM 使用 .DCM 扩展名。 .DCM 以两种不同的格式存在,即格式 1.x 和格式 2.x。 DCM Format 1.x 还提供了两个普通版本和扩展版本。 DICOM 用于集成来自不同供应商的打印机、服务器、扫描仪等医疗成像设备,还包含每个患者的唯一识别数据。如果 DICOM 文件能够接收 DICOM 格式的图像数据,则它们可以在两方之间共享。 DICOM的通信部分是应用层协议,实体之间使用TCP/IP进行通信。 HTTP 和 HTTPS 协议用于 DICOM 的 Web 服务。 Web 服务支持的版本是 1.0、1.1、2 或更高版本。
阅读更多其他支持的删除背景格式
使用 C#,可以轻松地从不同格式中删除背景,包括。