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

…衆ロ難τιáo~ 提交于 2020-01-03 19:15:52

问题


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#. 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. 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. Reading e.g. Pianykh's book about DICOM, OB means Other Byte String. But up to now I'm unable to convert the manipulated byte-array to 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. Searching here at stackoverflow, fo-dicom documentation in git or with google I still didn't get a clue how to deal with it. So I'm wondering how to convert my manipulated matrix to OB, because I thought DicomOtherByte is OB.

EDIT: The Exception is "Unable to create DICOM element of type OB with values of type Dicom.DicomOtherByte" - System.InvalidOperationException

Thanks in advance.


回答1:


The pixel data in a Dicom Dataset is something very special. It cannot easily be read or written as a single Tag. Fo-Dicom has special functions and classes to work with pixel data.

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.

Also, if you want to work with actual images, use the DicomImage class.

DicomImage image = new DicomImage(ctFile.Dataset);


来源:https://stackoverflow.com/questions/52292288/manipulating-and-converting-pixeldata-of-ct-image-with-fo-dicom-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!