LEADTOOLS -提取、编辑和替换DICOM图像 - C# .NET 6

LEADTOOLS -提取、编辑和替换 DICOM 图像 - C# .NET 6

本教程介绍如何将 DICOM 图像提取为 PNG 文件,然后使用 LEADTOOLS SDK 替换 C# .NET 6 应用程序中的 DICOM 图像。

概述 
概括本教程介绍如何在 C# .NET 6 应用程序中提取和替换 DICOM 图像。
完成时间20分钟
Visual Studio 项目下载教程项目 (2 KB)
平台C# .NET 6 控制台应用程序
集成开发环境Visual Studio 2022
运行时目标.NET 6 或更高版本
开发许可证下载 LEADTOOLS

所需知识

在开始本教程之前,请先查看添加引用和设置许可证教程,熟悉创建项目的基本步骤。

创建项目并添加 LEADTOOLS 引用

从“添加引用”和“设置许可证”教程中创建的项目副本开始。如果您没有该项目,请按照该教程中的步骤创建它。

所需参考文献取决于项目的目的。可以通过以下两种方法之一添加参考文献(但不能同时使用)。本项目需要以下参考文献:

如果使用 NuGet 引用,本教程需要以下 NuGet 包:

  • Leadtools.Dicom.Pacs.Scu

如果使用本地 DLL 引用,则需要以下 DLL。这些 DLL 位于<INSTALL_DIR>\LEADTOOLS23\Bin\net

  • Leadtools.dll
  • Leadtools.Codecs.dll
  • Leadtools.Dicom.dll
  • Leadtools.Dicom.Tables.dll
  • Leadtools.ImageProcessing.Core.dll

有关您的应用程序所需的 DLL 文件的完整列表,请参阅应用程序中要包含的文件

设置许可证文件

许可证用于解锁项目所需的功能。必须在调用任何工具包函数之前设置许可证。有关详细信息(包括不同平台的教程),请参阅设置运行时许可证

运行时许可证有两种类型:

  • 评估许可证,在下载评估工具包时获得。它允许对工具包进行评估。
  • 部署许可证。如果需要部署许可证文件和开发者密钥,请参阅获取许可证

添加 DICOM 图像提取代码

创建项目、添加参考并设置许可证后,就可以开始编码了。

解决方案资源管理器中,打开Program.cs。将以下语句添加到using顶部的块中Program.cs

C#
using System; 
using System.Collections.Generic; 
using System.IO; 
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Dicom; 

RasterCodecs在 Program 类中添加以下全局变量。

C#
static RasterCodecs codecs = new RasterCodecs(); 

ExtractPngFromDicom(string dicomFile, string outDir)在类中添加一个名为 的新方法Program。该方法将在方法内部调用Main(),如下所示,以返回提取为 PNG 文件的 DICOM 图像的字符串数组。

确保该Main()方法到目前为止包含以下代码。

C#
static void Main(string[] args) 
{ 
   string dicomFile = @"C:\LEADTOOLS23\Resources\Images\DICOM\image3.dcm"; 
   // Create a local output directory for the files to be saved 
   string outDir = @"OUTPUT DIRECTORY"; 
 
   InitLEAD(); 
   DicomEngine.Startup(); 
   var extractedFrames = ExtractPngFromDicom(dicomFile, outDir); 
} 

添加以下代码将 DICOM 图像提取为 PNG 文件。

C#
private static string[] ExtractPngFromDicom(string dicomFile, string outDir) 
{ 
   // Create a list of output files 
   var outputFiles = new List<string>(); 
   if(!Directory.Exists(outDir)) 
      Directory.CreateDirectory(outDir); 
 
   // Load the DICOM pixel data 
   var ds = new DicomDataSet(); 
   ds.Load(dicomFile, DicomDataSetLoadFlags.None); 
   var pixelDataElement = ds.FindFirstElement(null, DicomTag.PixelData, true); 
   if (pixelDataElement != null) throw new Exception("This dataset is missing the pixel data element"); 
 
   var imageCount = ds.GetImageCount(pixelDataElement); 
   var imageInformation = ds.GetImageInformation(pixelDataElement, 0); 
 
   if (imageCount == 0) throw new Exception("This dataset has no images"); 
   if (imageInformation == null) throw new Exception("Can't retrieve image information"); 
   // Get all the images based on pixel data and image information 
   using var image = ds.GetImages(pixelDataElement, 0, imageInformation.FrameCount, 0, RasterByteOrder.Gray, DicomGetImageFlags.AllowRangeExpansion | DicomGetImageFlags.AutoApplyModalityLut | DicomGetImageFlags.AutoApplyVoiLut); 
   for (int i = 1; i <= image.PageCount; i++) 
   { 
      var outFileName = Path.Combine(outDir, Path.GetFileNameWithoutExtension(dicomFile) + ".png"); 
      // Save the image as a PNG file 
      codecs.Save(image, outFileName, RasterImageFormat.Png, 0, i, i, 1, CodecsSavePageMode.Overwrite); 
      // Add the output file name to the list of output files 
      outputFiles.Add(outFileName); 
   } 
   return outputFiles.ToArray(); 
} 

添加替换 DICOM 图像代码

在类中添加一个名为“ ReplaceDicomImages(string dicomFile, string outDir, params string[] imageFiles)inside”的新方法Program。此方法将在方法内部调用Main(),位于方法调用ExtractPngFromDicom()下方,如下所示。同时在方法底部添加一行代码Main()来关闭DicomEngine

C#
static void Main(string[] args) 
{ 
   string dicomFile = @"C:\LEADTOOLS23\Resources\Images\DICOM\image3.dcm"; 
   // Create a local output directory for the files to be saved 
   string outDir = @"OUTPUT DIRECTORY"; 
 
   InitLEAD(); 
   DicomEngine.Startup(); 
   var extractedFrames = ExtractPngFromDicom(dicomFile, outDir); 
 
   // Apply Redaction, anonymization, etc. to PNG 
   ReplaceDicomImages(dicomFile, outDir, extractedFrames); 
   DicomEngine.Shutdown(); 
} 

添加以下代码以获取ExtractPngFromDicom()方法中创建的 PNG 并将其添加回 DICOM 图像。

C#
private static string ReplaceDicomImages(string dicomFile, string outDir, params string[] imageFiles) 
{ 
   // Load the DICOM pixel data 
   var ds = new DicomDataSet(); 
   ds.Load(dicomFile, DicomDataSetLoadFlags.None); 
 
   // Changing ImageType to DERIVED to meet DICOM standards, since we are changing the image 
   List<string> it = ds.GetValue<List<string>>(DicomTag.ImageType, null); 
   if (it.Contains("ORIGINAL")) 
   { 
      it.Remove("ORIGINAL"); 
      it.Insert(0, "DERIVED"); 
   } 
   ds.InsertElementAndSetValue(DicomTag.ImageType, it.ToArray()); 
 
   var pixelDataElement = ds.FindFirstElement(null, DicomTag.PixelData, true); 
   if (pixelDataElement == null) 
      pixelDataElement = ds.InsertElement(null, false, DicomTag.PixelData, DicomVRType.UN, false, 0); 
   // Create a new RasterImageset to null 
   RasterImage img = null; 
   foreach (var imageFile in imageFiles) 
   { 
      // Load the image file 
      if (img == null) 
         img = codecs.Load(imageFile); 
      else 
         img.AddPage(codecs.Load(imageFile)); 
   } 
   // Set the image back to a DICOM file 
   ds.SetImages(pixelDataElement, img, DicomImageCompressionType.J2kLossless, DicomImagePhotometricInterpretationType.Monochrome2, 0, 2, DicomSetImageFlags.AutoSetVoiLut); 
   var outFileName = Path.Combine(outDir, Path.GetFileNameWithoutExtension(dicomFile) + "_replaced.dcm"); 
   ds.Save(outFileName, DicomDataSetSaveFlags.None); 
   return outFileName; 
} 

运行项目

按F5或选择“调试”->“开始调试”来运行项目。

如果正确执行了这些步骤,控制台将会出现并确认许可证已正确设置。然后,应用程序将从 DICOM 图像中提取 PNG 文件,替换它,并将其保存到指定位置。

本教程展示了如何创建一个简单的基于控制台的图像提取应用程序,该应用程序初始化 LEAD DICOM 引擎,从 DICOM 文件中提取 PNG 并替换它。

参见

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值