3
votes

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
Thanks for your comments. I edited and added the short description VS shows me for the exception. And I changed the foreach loop to a for loop. You were right. Looked a bit strange.Booma

1 Answers

8
votes

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