C#を介してDICOMの背景を変更します
サーバー側のAPIを使用して、独自の.NETアプリを作成し、DICOMファイルの背景を変更します。
C#を使用してDICOMファイルの背景を変更する方法
多くの場合、理想的な画像を実現するには、背景を変更する必要があります。目的の DICOM 形式の画像効果を得るには、前景オブジェクトを画像の残りの部分から分離する必要があります。背景が均一であれば自動物体検出が可能です。写真の背景が不均一である場合、またはオブジェクトの分離が難しい場合は、画像に事前にマークを付けることをお勧めします。これには、目的のオブジェクトが存在する写真内の長方形領域を特定し、そのタイプを指定することが含まれます。これは手動で行うことも、Cloud API のオブジェクト認識機能を使用して自動的に行うこともできます。オブジェクトの選択と元の背景の削除に続いて、新しい背景を適用したり、透明度を実装したりできます。 DICOM ファイルの背景を変更するには、次を使用します。 Aspose.Imaging for .NET 機能が豊富で強力で使いやすいC#プラットフォーム用の画像操作および変換APIであるAPI。開ける NuGet パッケージマネージャー、検索 ** Aspose.Imaging ** とインストールします。パッケージマネージャーコンソールから次のコマンドを使用することもできます。
パッケージマネージャーコンソールコマンド
PM> Install-Package Aspose.Imaging
C#を介してDICOMの背景を変更する手順
あなたは aspose.imaging.dll 自分の環境で次のワークフローを試してください。
+Image.Loadメソッドを使用してDICOMファイルをロードします +背景を変更します。 +Aspose.Imaging形式でサポートされているディスクに画像を保存します
システム要求
Aspose.Imaging for .NETは、すべての主要なオペレーティングシステムでサポートされています。次の前提条件があることを確認してください。
-Microsoft Windows、または.NET Framework、.NET Core、Windowsアプリケーション、ASP.NETWebアプリケーションと互換性のあるOS。 -Microsoft VisualStudioのような開発環境。 -プロジェクトで参照されているAspose.Imagingfor.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(); | |
} | |
} |
Aspose.Imaging for .NET APIについて
Aspose.Imaging APIは、アプリケーション内で画像(写真)を作成、変更、描画、または変換するための画像処理ソリューションです。クロスプラットフォームの画像処理(さまざまな画像形式間の変換(均一なマルチページまたはマルチフレームの画像処理を含む)、描画などの変更、グラフィックプリミティブの操作、変換(サイズ変更、トリミング、反転、回転)を含むがこれらに限定されない) 、2値化、グレースケール、調整)、高度な画像操作機能(フィルタリング、ディザリング、マスキング、デスキュー)、およびメモリ最適化戦略。これはスタンドアロンライブラリであり、画像操作をソフトウェアに依存しません。プロジェクト内のネイティブAPIを使用して、高性能の画像変換機能を簡単に追加できます。これらは100%プライベートのオンプレミスAPIであり、画像はサーバーで処理されます。オンラインアプリを介してDICOMの背景を変更する
[Live Demos Webサイト](https://products.aspose.app/imaging/remove-background)にアクセスして、DICOMドキュメントの背景を変更してください。 ライブデモには次の利点があります
DICOM とは DICOM ファイル形式
DICOMは、Medical Imaging and Communications in Medicineの頭字語であり、医療情報学の分野に関係しています。 DICOMは、ファイル形式の定義とネットワーク通信プロトコルを組み合わせたものです。 DICOMは.DCM拡張子を使用します。 .DCMは、フォーマット1.xとフォーマット2.xの2つの異なるフォーマットで存在します。 DCMフォーマット1.xは、通常と拡張の2つのバージョンでさらに利用できます。 DICOMは、さまざまなベンダーのプリンター、サーバー、スキャナーなどの医用画像装置の統合に使用され、一意性のために各患者の識別データも含まれています。 DICOMファイルは、DICOM形式の画像データを受信できる場合、2者間で共有できます。 DICOMの通信部分はアプリケーション層プロトコルであり、TCP / IPを使用してエンティティ間で通信します。 HTTPおよびHTTPSプロトコルは、DICOMのWebサービスに使用されます。 Webサービスでサポートされているバージョンは、1.0、1.1、2以降です。
続きを読むその他のサポートされている変更の背景形式
C#を使用すると、を含むさまざまな形式で背景を簡単に変更できます。