DicomJsonSerializerを使用してDICOMファイルをJSONに変換する方法
このチュートリアルでは、DICOM ファイルを C# を使用して JSON フォーマットに変換する方法を示しています。
ディコムをJSONに変換する利点
「Webインテグレーション」:- JSON は REST API と Web サービスの標準形式です。
データ分析(データ分析:- データ分析パイプラインでDICOMメタデータを簡単に処理します。
●「互換性」:- 地元のDICOMフォーマットをサポートしていないシステムとメタデータを共有します。
原題: Preparing the Environment
- Visual Studio または 互換性のある .NET IDE を設定します.
- 新しい .NET 8 コンソールアプリケーションプロジェクトを作成します。
- NuGet Package Manager から Aspose.Medical をインストールします。
DICOMをJSONに変換するためのステップアップガイド
ステップ1:インストール Aspose.Medical
NuGet を使用してプロジェクトに Aspose.Medical ライブラリを追加します。
Install-Package Aspose.Medicalステップ2:必要な名称スペースを含む
あなたのコードに必要な名称スペースへの参照を追加します.
using Aspose.Medical.Dicom;
using Aspose.Medical.Dicom.Serialization;ステップ3:DICOMファイルをダウンロード
変換したいDICOMファイルをダウンロードします。
DicomFile dcm = DicomFile.Open("patient_scan.dcm");ステップ4:JSONにシリアル化する
使用する The DicomJsonSerializer.Serialize DICOM ファイルを JSON に変換する方法
string json = DicomJsonSerializer.Serialize(dcm);ステップ5: JSON 出力を保存または使用する
JSON をファイルに保存するか、アプリケーションに直接使用します。
// Save to file
File.WriteAllText("patient_scan.json", json);
// Or use directly
Console.WriteLine(json);DICOMをJSONに変換するための完全なコードサンプル
ここでは、DICOMファイルをJSONに変換する方法を示す完全な例です。
using Aspose.Medical.Dicom;
using Aspose.Medical.Dicom.Serialization;
// Load the DICOM file
DicomFile dcm = DicomFile.Open("patient_scan.dcm");
// Convert to JSON
string json = DicomJsonSerializer.Serialize(dcm);
// Save to file
File.WriteAllText("patient_scan.json", json);
Console.WriteLine("DICOM file converted to JSON successfully!");
Console.WriteLine($"Output saved to: patient_scan.json");JSON プリント出力
人間に読みやすいJSONとインデンターションのために、使用してください。 writeIndented パラメータ:
using Aspose.Medical.Dicom;
using Aspose.Medical.Dicom.Serialization;
DicomFile dcm = DicomFile.Open("patient_scan.dcm");
// Serialize with indentation for readability
string prettyJson = DicomJsonSerializer.Serialize(dcm, writeIndented: true);
File.WriteAllText("patient_scan_pretty.json", prettyJson);
Console.WriteLine("Pretty-printed JSON saved successfully!");データセットをDicomFile代わりに変換する
データセットの部分だけを変換することもできます:
using Aspose.Medical.Dicom;
using Aspose.Medical.Dicom.Serialization;
DicomFile dcm = DicomFile.Open("patient_scan.dcm");
// Serialize only the dataset (without file meta information)
string datasetJson = DicomJsonSerializer.Serialize(dcm.Dataset);
File.WriteAllText("patient_dataset.json", datasetJson);JSON 出力構造
JSON 出力は DICOM PS3.18 標準に準拠しています。
{
"00080005": {
"vr": "CS",
"Value": ["ISO_IR 100"]
},
"00080020": {
"vr": "DA",
"Value": ["20240115"]
},
"00080030": {
"vr": "TM",
"Value": ["143022"]
},
"00100010": {
"vr": "PN",
"Value": [
{
"Alphabetic": "DOE^JOHN"
}
]
},
"00100020": {
"vr": "LO",
"Value": ["12345"]
}
}ストリームベースのシリアリズム
大型ファイルやウェブアプリケーションでは、ストリームベースのシリアリズムを使用します。
using Aspose.Medical.Dicom;
using Aspose.Medical.Dicom.Serialization;
DicomFile dcm = DicomFile.Open("large_scan.dcm");
// Write directly to a file stream
using (FileStream fs = File.Create("large_scan.json"))
{
DicomJsonSerializer.Serialize(fs, dcm.Dataset);
}
Console.WriteLine("Large DICOM file serialized to JSON stream!");統合例: Web API に JSON を送信
以下は、HTTP クライアントと DICOM を JSON に変換する方法です。
using Aspose.Medical.Dicom;
using Aspose.Medical.Dicom.Serialization;
using System.Net.Http;
using System.Text;
DicomFile dcm = DicomFile.Open("patient_scan.dcm");
string json = DicomJsonSerializer.Serialize(dcm);
// Send to web API
using HttpClient client = new();
var content = new StringContent(json, Encoding.UTF8, "application/json");
HttpResponseMessage response = await client.PostAsync(
"https://api.example.com/dicom/metadata",
content
);
if (response.IsSuccessStatusCode)
{
Console.WriteLine("DICOM metadata successfully sent to API!");
}使用ヒント
デビューDICOMメタデータ
JSON 変換を使用して DICOM タグ値を検証します。
DicomFile dcm = DicomFile.Open("unknown_scan.dcm");
string json = DicomJsonSerializer.Serialize(dcm, writeIndented: true);
Console.WriteLine(json);ウェブフロントエンドへの送信
JSON は、ブラウザベースの視聴者で DICOM メタデータを表示するのに最適です:
// In ASP.NET Core controller
[HttpGet("dicom/{id}/metadata")]
public IActionResult GetMetadata(string id)
{
DicomFile dcm = DicomFile.Open($"storage/{id}.dcm");
string json = DicomJsonSerializer.Serialize(dcm);
return Content(json, "application/json");
}JSON 出力に含まれているもの
JSONの出力は以下のとおりです。
- すべてのDICOMタグとその価値観
- 各タグの価値プレゼンテーション(VR)
- JSON オブジェクトとしての順序アイテム
- ピクセルデータのためのバイナリーデータ参照(BulkData)
追加情報
- JSON フォーマットは、DICOM PS3.18 Web Services の仕様に従います。
- 大型バイナリー値(ピクセルデータのような)は、通常、組み込まれたよりも参照されます。
- 特定の統合要件のためにカスタマイズされたシリアリゼーションオプションを使用することを検討します。
結論
このチュートリアルは、Aspose.Medical を使用して C# で JSON フォーマットに DICOM ファイルを変換する方法を示しています。