简体   繁体   English

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

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

For some tests I'm trying to manipulate the PixelData element of a CT image stored in dicom format and write it back in the file with Fellow Oak Dicom in C#. 对于某些测试,我试图操纵以dicom格式存储的CT图像的PixelData元素,然后使用C#中的Fellow Oak Dicom将其写回到文件中。 After some research I found out that the matrix I want to deal with is in the Buffer of PixelData stored in a byte -array. 经过一些研究,我发现我要处理的矩阵PixelData存储在byte PixelData中的PixelDataBuffer中。 So I wrote following code: 所以我写了下面的代码:

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

This was my first try and I got an Exception at the AddOrUpdate command because it was unable to convert a byte -array to OB. 这是我的第一次尝试,在AddOrUpdate命令中遇到Exception ,因为它无法将byte AddOrUpdate转换为OB。 Reading eg Pianykh's book about DICOM, OB means Other Byte String . 读例如Pianykh关于DICOM的书,OB表示Other Byte String But up to now I'm unable to convert the manipulated byte -array to OB. 但是到目前为止,我仍无法将操作后的byte数组转换为OB。 When I tried this code snippet: 当我尝试以下代码片段时:

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

the Exception still is calling at AddOrUpdate for being unable to convert the item to OB. Exception仍在AddOrUpdate上调用,因为无法将项目转换为OB。 Searching here at stackoverflow, fo-dicom documentation in git or with google I still didn't get a clue how to deal with it. 在这里在stackoverflow上搜索,或者在git或google中使用fo-dicom文档,我仍然不知道如何处理它。 So I'm wondering how to convert my manipulated matrix to OB, because I thought DicomOtherByte is OB. 所以我想知道如何将操纵矩阵转换为OB,因为我认为DicomOtherByte是OB。

EDIT: The Exception is "Unable to create DICOM element of type OB with values of type Dicom.DicomOtherByte" - System.InvalidOperationException 编辑: Exception是“无法创建类型为Dicom.DicomOtherByte类型的值的OB类型的DICOM元素”-System.InvalidOperationException

Thanks in advance. 提前致谢。

The pixel data in a Dicom Dataset is something very special. Dicom数据集中的像素数据非常特殊。 It cannot easily be read or written as a single Tag. 它不能轻易地作为单个标签读取或写入。 Fo-Dicom has special functions and classes to work with pixel data. Fo-Dicom具有用于处理像素数据的特殊功能和类。

Here is an example: 这是一个例子:

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

Note that there more helper classes to work with pixel data directly in a specific format like PixelDataConverter and PixelDataFactory . 请注意,还有更多的帮助程序类可以直接以特定格式(例如PixelDataConverterPixelDataFactory处理像素数据。

Also, if you want to work with actual images, use the DicomImage class. 另外,如果要使用实际图像,请使用DicomImage类。

DicomImage image = new DicomImage(ctFile.Dataset);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM