在 C# 中使用 fo-dicom 操作和转换 CT 图像的像素数据

Manipulating and Converting PixelData of CT Image with fo-dicom in C#

对于某些测试,我正在尝试操作以 dicom 格式存储的 CT 图像的 PixelData 元素,并使用 Fellow Oak Dicom 将其写回文件中在 C# 中。经过一番研究,我发现我要处理的矩阵位于 PixelDataBuffer 中,存储在 byte 数组中。所以我写了下面的代码:

DicomFile ctFile = DicomFile.Open(image);
var pixDat = ctFile.Dataset.Get<byte[]>(DicomTag.PixelData);

for (int i = 0; i < pixData.Length; i++)
{
    pixDat[i] = Convert.ToByte(200);
}

ctFile.Dataset.AddOrUpdate<byte[]>(DicomTag.PixelData, pixDat);
ctFile.Save("new file folder");

这是我的第一次尝试,我在 AddOrUpdate 命令中得到了一个 Exception,因为它无法将 byte 数组转换为 OB。 阅读例如Pianykh关于DICOM的书,OB的意思是Other Byte String。但到目前为止,我无法将操纵的 byte-array 转换为 OB。当我尝试此代码片段时:

DicomOtherByte dob = new DicomOtherByte(DicomTag.PixelData, pixDat);
ctFile.Dataset.AddOrUpdate<DicomOtherByte>(DicomTag.PixelData, dob);

Exception 仍在调用 AddOrUpdate,因为无法将项目转换为 OB。在 Whosebug 上搜索 git 或 google 中的 fo-dicom 文档,我仍然不知道如何处理它。所以我想知道如何将我的操作矩阵转换为 OB,因为我认为 DicomOtherByte 是 OB。

编辑:Exception 是 "Unable to create DICOM element of type OB with values of type Dicom.DicomOtherByte" - System.InvalidOperationException

提前致谢。

Dicom 数据集中的像素数据非常特别。它不能作为单个标签轻松读取或写入。 Fo-Dicom 具有特殊功能和 classes 来处理像素数据。

这是一个例子:

DicomFile ctFile = DicomFile.Open(@"C:\Temp\original.dcm");

// Create PixelData object to represent pixel data in dataset
DicomPixelData pixelData = DicomPixelData.Create(ctFile.Dataset);
// Get Raw Data
byte[] originalRawBytes = pixelData.GetFrame(0).Data;

// Create new array with modified data
byte[] modifiedRawBytes = new byte[originalRawBytes.Length];
for (int i = 0; i < originalRawBytes.Length; i++)
{
    modifiedRawBytes[i] = (byte)(originalRawBytes[i] + 100);
}

// Create new buffer supporting IByteBuffer to contain the modified data
MemoryByteBuffer modified = new MemoryByteBuffer(modifiedRawBytes);

// Write back modified pixel data
ctFile.Dataset.AddOrUpdatePixelData(DicomVR.OB, modified);

ctFile.Save(@"C:\Temp\Modified.dcm");

请注意,有更多帮助程序 class 可以直接以特定格式(如 PixelDataConverterPixelDataFactory 处理像素数据。

此外,如果您想使用实际图像,请使用 DicomImage class。

DicomImage image = new DicomImage(ctFile.Dataset);