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

3

为了进行一些测试,我试图操作以dicom格式存储的CT图像中的PixelData元素,并使用C#中的Fellow Oak Dicom将其写回文件。经过一些研究,我发现我想要处理的矩阵位于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表示其他字节字符串。但到目前为止,我还无法将操作后的byte数组转换为OB。当我尝试使用以下代码片段时:
DicomOtherByte dob = new DicomOtherByte(DicomTag.PixelData, pixDat);
ctFile.Dataset.AddOrUpdate<DicomOtherByte>(DicomTag.PixelData, dob);
Exception仍然在AddOrUpdate处调用,因为无法将项目转换为OB。 在stackoverflow、git中查找fo-dicom文档或使用谷歌搜索,我仍然没有弄清楚如何处理它。 因此,我想知道如何将我操作过的矩阵转换为OB,因为我认为DicomOtherByte是OB。

编辑:异常是“使用Dicom.DicomOtherByte类型的值无法创建类型为OB的DICOM元素”- System.InvalidOperationException

提前致谢。


感谢您的评论。我已编辑并添加了VS为异常显示给我的简短描述。而且,我也将foreach循环改成了for循环。您是对的,foreach循环看起来有点奇怪。 - Booma
1个回答

8

在Dicom数据集中,像素数据是非常特殊的。它不能像单个标签那样轻易地读取或写入。Fo-Dicom具有专门处理像素数据的功能和类。

以下是一个例子:

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

请注意,还有更多的辅助类可直接处理特定格式的像素数据,如PixelDataConverter和PixelDataFactory。
此外,如果您想使用实际图像,请使用DicomImage类。
DicomImage image = new DicomImage(ctFile.Dataset);

那个解决方案非常好,谢谢。但是我有关于PixelData的另一个问题。不同的文件中是否有不同类型的PixelData?对我来说,CT图像的PixelData似乎与RTDOSE PixelData不同。 - Booma
请查看https://dicom.innolitics.com/ciods/rt-dose/image-pixel。这些属性将告诉您拥有哪些像素数据。 - g_uint

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接