วิธีการแปลงไฟล์ DICOM Multiple ไปยัง JSON Array แบบเดียว

วิธีการแปลงไฟล์ DICOM Multiple ไปยัง JSON Array แบบเดียว

การสอนนี้แสดงให้เห็นว่าวิธีการแปลงไฟล์ DICOM หลายไฟล์เป็นชุด JSON เดียวโดยใช้ C# วิธีนี้เหมาะสําหรับนักวิศวกรข้อมูลที่ต้องการส่งออกข้อมูลโลหะ DIKOM สําหรับเครื่องมือวิเคราะห์ฐานข้อมูลหรือท่อข้อมูล

ประโยชน์ของ JSON Array Export

  • การประมวลผลข้อมูลบล็อก:- นําเข้าบันทึก DICOM หลายรายการไปยังฐานข้อมูลในขั้นตอนเดียว

  • **การวิเคราะห์พร้อม *:- รั้ว JSON สามารถโหลดได้โดยตรงไปยัง Python, Spark หรือเก็บข้อมูล

  • การส่งออกแบบกะทัดรัด:- ไฟล์เดียวที่มี metadata ทั้งหมดทําให้การจัดการข้อมูลง่ายขึ้น

ข้อกําหนด: การเตรียมสิ่งแวดล้อม

  • ติดตั้ง Visual Studio หรือ IDE .NET ที่เข้ากันได้ใด ๆ.
  • สร้างโครงการการใช้งาน .NET 8 ใหม่
  • ติดตั้ง Aspose.Medical จาก NuGet Package Manager
  • สร้างโฟลเดอร์ที่มีไฟล์ DICOM มากมาย

คู่มือขั้นตอนเพื่อแปลงไฟล์ DICOM มากมายไปยัง JSON Array

ขั้นตอน 1: ติดตั้ง Aspose.Medical

เพิ่มห้องสมุด Aspose.Medical ไปยังโครงการของคุณโดยใช้ NuGet

Install-Package Aspose.Medical

ขั้นตอนที่ 2: รวมพื้นที่ชื่อที่จําเป็น

เพิ่มอ้างอิงไปยังพื้นที่ชื่อที่ต้องการในรหัสของคุณ.

using Aspose.Medical.Dicom;
using Aspose.Medical.Dicom.Serialization;

ขั้นตอนที่ 3: ดาวน์โหลดไฟล์ DICOM Multiple

ดาวน์โหลดไฟล์ DICOM จากโฟลเดอร์ไปยังคอลเลกชัน

string inputFolder = @"C:\DicomStudies";
string[] dicomPaths = Directory.GetFiles(inputFolder, "*.dcm");

List<DicomFile> dicomFiles = new();
foreach (string path in dicomPaths)
{
    dicomFiles.Add(DicomFile.Open(path));
}

ขั้นตอนที่ 4: extract Dataset Array

สร้างชุดของรายการ Dataset จากไฟล์ที่โหลด

Dataset[] datasets = dicomFiles
    .Select(dcm => dcm.Dataset)
    .ToArray();

ขั้นตอน 5: Serialize ไปยัง JSON Array

ใช DicomJsonSerializer.Serialize กับ Dataset array

string jsonArray = DicomJsonSerializer.Serialize(datasets, writeIndented: true);

ขั้นตอน 6: Save the JSON Array

เก็บ JSON array ไปยังไฟล์

File.WriteAllText("dicom_studies.json", jsonArray);
Console.WriteLine($"Exported {datasets.Length} DICOM datasets to JSON array.");

ตัวอย่างรหัสสมบูรณ

นี่คือตัวอย่างที่สมบูรณ์แบบที่แสดงให้เห็นว่าวิธีการแปลงไฟล์ DICOM หลายไฟล์ไปยังชุด JSON:

using Aspose.Medical.Dicom;
using Aspose.Medical.Dicom.Serialization;

string inputFolder = @"C:\DicomStudies";
string outputFile = "dicom_studies.json";

// Get all DICOM files
string[] dicomPaths = Directory.GetFiles(inputFolder, "*.dcm");
Console.WriteLine($"Found {dicomPaths.Length} DICOM files.");

// Load all files
List<DicomFile> dicomFiles = new();
foreach (string path in dicomPaths)
{
    try
    {
        dicomFiles.Add(DicomFile.Open(path));
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Skipping invalid file: {Path.GetFileName(path)}");
    }
}

// Build dataset array
Dataset[] datasets = dicomFiles
    .Select(dcm => dcm.Dataset)
    .ToArray();

// Serialize to JSON array
string jsonArray = DicomJsonSerializer.Serialize(datasets, writeIndented: true);

// Save to file
File.WriteAllText(outputFile, jsonArray);

Console.WriteLine($"Successfully exported {datasets.Length} datasets to {outputFile}");

ตัวอย่าง JSON Array Output

การผลิต JSON array ดูเหมือนเช่นนี้:

[
  {
    "00080005": { "vr": "CS", "Value": ["ISO_IR 100"] },
    "00100010": { "vr": "PN", "Value": [{ "Alphabetic": "DOE^JOHN" }] },
    "00100020": { "vr": "LO", "Value": ["12345"] }
  },
  {
    "00080005": { "vr": "CS", "Value": ["ISO_IR 100"] },
    "00100010": { "vr": "PN", "Value": [{ "Alphabetic": "SMITH^JANE" }] },
    "00100020": { "vr": "LO", "Value": ["67890"] }
  }
]

การประมวลผลข้อมูลขนาดใหญ่ด้วย LINQ

สําหรับการจัดการหน่วยความจําที่ดีขึ้นด้วยชุดข้อมูลขนาดใหญ่ใช้ projections LINQ:

using Aspose.Medical.Dicom;
using Aspose.Medical.Dicom.Serialization;

string inputFolder = @"C:\LargeDicomArchive";
string outputFile = "large_export.json";

// Process files lazily to manage memory
Dataset[] datasets = Directory.GetFiles(inputFolder, "*.dcm")
    .Select(path => 
    {
        try
        {
            return DicomFile.Open(path).Dataset;
        }
        catch
        {
            return null;
        }
    })
    .Where(ds => ds != null)
    .ToArray()!;

string jsonArray = DicomJsonSerializer.Serialize(datasets, writeIndented: true);
File.WriteAllText(outputFile, jsonArray);

Console.WriteLine($"Exported {datasets.Length} datasets.");

เพิ่มรายงานขั้นสูง

สําหรับการแข่งขันขนาดใหญ่เพิ่มรายงานขั้นสูง:

using Aspose.Medical.Dicom;
using Aspose.Medical.Dicom.Serialization;

string inputFolder = @"C:\DicomStudies";
string[] dicomPaths = Directory.GetFiles(inputFolder, "*.dcm");

List<Dataset> datasets = new();
int processed = 0;
int total = dicomPaths.Length;

foreach (string path in dicomPaths)
{
    try
    {
        DicomFile dcm = DicomFile.Open(path);
        datasets.Add(dcm.Dataset);
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Error: {Path.GetFileName(path)} - {ex.Message}");
    }
    
    processed++;
    if (processed % 100 == 0 || processed == total)
    {
        Console.WriteLine($"Progress: {processed}/{total} ({processed * 100 / total}%)");
    }
}

string jsonArray = DicomJsonSerializer.Serialize(datasets.ToArray(), writeIndented: true);
File.WriteAllText("export.json", jsonArray);

Console.WriteLine($"Export complete: {datasets.Count} datasets.");

การนําเข้า JSON Array into Analytics Tools

ตัวอย่าง Python

import json
import pandas as pd

# Load the exported JSON array
with open('dicom_studies.json', 'r') as f:
    dicom_data = json.load(f)

# Convert to DataFrame for analysis
df = pd.json_normalize(dicom_data)
print(df.head())

ดาวน์โหลดไปยัง MongoDB

// Using mongoimport
// mongoimport --db medical --collection studies --jsonArray --file dicom_studies.json

การพิจารณาหน่วยความจํา

เมื่อทํางานกับชุดข้อมูลขนาดใหญ่มาก:

  • กระบวนการในแพทช์: ส่วนแบ่งไฟล์เป็นแพตเกจของไฟล์ 100-500
  • ** Stream Output**: ใช้การจัดเรียงตาม stream สําหรับแร่ขนาดใหญ่มาก
  • จัดเก็บไฟล์: ให้แน่ใจว่า DicomFile วัตถุจะถูกกําจัดหลังจากการสกัดชุดข้อมูล
// Batch processing example
int batchSize = 100;
string[] allFiles = Directory.GetFiles(inputFolder, "*.dcm");
int batchNumber = 0;

for (int i = 0; i < allFiles.Length; i += batchSize)
{
    string[] batch = allFiles.Skip(i).Take(batchSize).ToArray();
    Dataset[] datasets = batch
        .Select(path => DicomFile.Open(path).Dataset)
        .ToArray();
    
    string batchJson = DicomJsonSerializer.Serialize(datasets, writeIndented: true);
    File.WriteAllText($"batch_{batchNumber++}.json", batchJson);
}

ข้อมูลเพิ่มเติม

  • รูปแบบ JSON Array เหมาะสําหรับการนําเข้าจํานวนมากไปยังฐานข้อมูล NoSQL
  • โปรดพิจารณาการบีบอัดไฟล์ JSON ขนาดใหญ่เพื่อประสิทธิภาพการจัดเก็บ
  • สําหรับสถานการณ์การสตรีมมิ่งให้พิจารณาการใช้รูปแบบ NDJSON (Newline-Delimited JSON) แทน

ข้อสรุป

กวดวิชานี้ได้แสดงให้เห็นว่าวิธีการแปลงไฟล์ DICOM หลายไฟล์เป็นชุด JSON เดียวใน C# โดยใช้ Aspose.Medical วิธีนี้ช่วยให้การส่งออกข้อมูลจํานวนมากที่มีประสิทธิภาพสําหรับการวิเคราะห์การนําเข้าฐานข้อมูลและการบูรณาการท่อข้อมูล

 แบบไทย