如何将 DICOM 文件转换为 JSON 使用 DicomJsonSerializer

如何将 DICOM 文件转换为 JSON 使用 DicomJsonSerializer

此教程展示了如何将 DICOM 文件转换为 JSON 格式,使用 C#。

将DICOM转换为JSON的好处

    • 网页集成*:- JSON 是 REST API 和 Web 服务的标准格式。
  • 数据分析:- 在数据分析管道中轻松处理DICOM甲数据。

    • 可互动性*:- 与不支持本地DICOM格式的系统共享代数据。

原标题:准备环境

  • 设置 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

您还可以单独转换 Dataset 部分:

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!");

集成示例:将 JSON 发送到 Web API

以下是如何将 DICOM 转换为 JSON 与 HTTP 客户端集成:

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 Metadata

使用 JSON 转换来检查 DICOM 标签值:

DicomFile dcm = DicomFile.Open("unknown_scan.dcm");
string json = DicomJsonSerializer.Serialize(dcm, writeIndented: true);
Console.WriteLine(json);

向 Web Front-End 发送

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 规格。
  • 大型二进制值(如像素数据)通常提到而不是嵌入。
  • 考虑使用自定义的序列化选项,以满足具体的集成要求。

结论

此教程向您展示了如何将 DICOM 文件转换为 C# 格式,使用 Aspose.Medical. JSON 输出允许与现代医疗保健 API、网页浏览器和数据分析系统无缝集成。

 中文