0

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.

Joshua H
  • 839
  • 11
  • 24
  • Take a small image as test to see what bytes it moves actually – csharpwinphonexaml May 01 '14 at 09:22
  • The fact that your image is the right shape and and has the right elements in the right places tells me your maths is correct, you are moving the pixels from the right place to the right place. So I have to suspect you are not moving the entire pixels - maybe you are only moving the red or green or blue channel if a colour image, or maybe you are not masking the transparency out properly and adding it back in. I am saying your maths is giving you the correct locations but you are either not picking up the entire pixel from the originl or not writing the entire pixel back to the flipped image. – Mark Setchell May 01 '14 at 09:25
  • What is the value of `bitsPerPixel` (and what is the PhotometricInterpretation of the image)? If it's greater then 8, then you'll need an int array instead of byte array – Chris O May 01 '14 at 18:36

1 Answers1

0

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.

Joshua H
  • 839
  • 11
  • 24