通過 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#,可以輕鬆地從不同格式中刪除背景,包括。