Question

I'm trying to rotate raw pixel data from a DICOM file by 180 degrees (or flipped). I've successfully flipped the image correctly, however, upon writing the pixel data back to the file (in this case it's a DICOM file) and displaying it. The final output of the image is not correct.

Below is the sample a sample of the image I'm trying to flip 180 /mirror.

enter image description here

Here's the code I'm using to perform the flipping:

        string file = @"adicomfile.dcm";
        DicomFile df = new DicomFile();
        df.Load(file);

            // Get the amount of bits per pixel from the DICOM header.
        int bitsPerPixel = df.DataSet[DicomTags.BitsAllocated].GetInt32(0, 0);

            // Get the raw pixel data from the DICOM file.
        byte[] bytes = df.DataSet[DicomTags.PixelData].Values as byte[];

                    // Get the width and height of the image.
        int width = df.DataSet[DicomTags.Columns].GetInt32(0, 0);
        int height = df.DataSet[DicomTags.Rows].GetInt32(0, 0);

        byte[] original = bytes;
        byte[] mirroredPixels = new byte[width * height * (bitsPerPixel / 8)];

        width *= (bitsPerPixel / 8);

                    // The mirroring / image flipping.
        for (int i = 0; i < original.Length; i++)
        {
            int mod = i % width;
            int x = ((width - mod - 1) + i) - mod;

            mirroredPixels[i] = original[x];
        }

        df.DataSet[DicomTags.PixelData].Values = mirroredPixels;

        df.Save(@"flippedicom.dcm", DicomWriteOptions.Default);

And here's my output (incorrect). The white and distortion is not the desired output.

enter image description here

I'm using ClearCanvas DICOM library, however this shouldn't matter as I'm only trying to manipulate the raw pixel data contained within the file itself.

The desired output would preferably look like the original, but flipped 180 / mirrored.

Some assistance would be greatly appreciated. I've tried my best searching SO, but to no avail.

Was it helpful?

Solution

It took a while, but I ended up solving my problem by using a method from a Java library. You can see the class here.

string file = @"adicomfile.dcm";
DicomFile df = new DicomFile();
df.Load(file);

// Get the amount of bits per pixel from the DICOM header.
int bitsPerPixel = df.DataSet[DicomTags.BitsAllocated].GetInt32(0, 0);

// Get the raw pixel data from the DICOM file.
byte[] bytes = df.DataSet[DicomTags.PixelData].Values as byte[];

// Get the width and height of the image.
int width = df.DataSet[DicomTags.Columns].GetInt32(0, 0);
int height = df.DataSet[DicomTags.Rows].GetInt32(0, 0);

byte[] newBytes = new byte[height * width * (bitsPerPixel / 8)];
int stride = bitsPerPixel / 8;

for (int y = 0; y < height; y++)
{
      for (int x = 0; x < width * stride; x++)
      {
        newBytes[((height - y - 1) * (width * stride)) + x] = bytes[(y * (width * stride)) + x];
    }
}

// Set patient orientation.
df.DataSet[DicomTags.PatientOrientation].Values = @"A\L";

// The pixel data of the DICOM file to the flipped/mirrored data.
df.DataSet[DicomTags.PixelData].Values = mirroredPixels;

// Save the DICOM file.
df.Save(@"flippedicom.dcm", DicomWriteOptions.Default);

The output was correct and I was able to continue other modifications to the raw pixel data.

Thank you all for the pointers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top