将图像的原始像素数据旋转180度

时间:2014-05-01 07:12:26

标签: c# image dicom image-rotation clearcanvas

我试图将原始像素数据从DICOM文件旋转180度(或翻转)。然而,在将像素数据写回文件(在这种情况下它是一个DICOM文件)并显示它之后,我已经成功地正确地翻转了图像。图像的最终输出不正确。

以下是我尝试翻转180 /镜像的图像样本。

enter image description 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[] 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);

这是我的输出(不正确)。白色和失真不是理想的输出。

enter image description here

我使用的是ClearCanvas DICOM库,但这并不重要,因为我只是试图操纵文件本身中包含的原始像素数据。

所需的输出最好看起来像原始的,但翻转180 /镜像。

非常感谢一些帮助。我尝试过最好的搜索,但无济于事。

1 个答案:

答案 0 :(得分:0)

花了一段时间,但我最终通过使用Java库中的方法解决了我的问题。您可以看到课程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);

输出正确,我能够继续对原始像素数据进行其他修改。

谢谢大家的指示。

相关问题