Creating DICOM Images from Raw Pixel Data
Sometimes we are asked the question of how to create a new DICOM image from imaging source (such as a detector) that outputs raw pixel data.
With the help of DicomImage.SetPixelFile method, this becomes fairly simple.
The following C# code shows how to load a single frame CT pixel data file from disk and create a new DicomImage object for display purposes only:
DicomObjects.NET
SetPixelFile method
DicomImage img = new DicomImage();
// add all relevant group 28 attributes to image to tell DicomObjects how to display
img.DataSet.Add(Keyword.SamplesPerPixel, 1);
img.DataSet.Add(Keyword.PhotometricInterpretation, "MONOCHROME2");
img.DataSet.Add(Keyword.Rows, 512);
img.DataSet.Add(Keyword.Columns, 512);
img.DataSet.Add(Keyword.PixelSpacing, new float[] { 0.684f, 0.684f });
img.DataSet.Add(Keyword.BitsAllocated, 16);
img.DataSet.Add(Keyword.BitsStored, 12);
img.DataSet.Add(Keyword.HighBit, 11);
img.DataSet.Add(Keyword.PixelRepresentation, 0);
img.DataSet.Add(Keyword.WindowCenter, new int[] { 35, 50 });
img.DataSet.Add(Keyword.WindowWidth, new int[] { 350, 150 });
img.DataSet.Add(Keyword.RescaleIntercept, -1200);
img.DataSet.Add(Keyword.RescaleSlope, 1);
// Load pixel data from file
img.SetPixelFile("CT.raw", 0, 0);
// Display the CT Image
Viewer.Images.Add(img);
SetPixelData method
With the latest version of DicomObjects, we also introduced DicomImage.SetPixelData routine, where users decalres a function that returns the Stream object containing the pixel data and let DicomObjects do the rest for you. This is particularly useful when you only have the Raw pixel data held in memory and not on file system.
// Load pixel data from file
img.SetPixelData(TransferSyntaxes.ExplicitVRLittleEndian, getData);
Viewer.Images.Add(img);
// return Stream object containing pixel data
private Stream getData(int arg)
{
return new FileStream(@"CT.raw", FileMode.Open);
}