0

我正在尝试将 DICOM 文件中的原始像素数据旋转 180 度(或翻转)。但是,在将像素数据写回文件(在本例中为 DICOM 文件)并显示它时,我已经成功地正确翻转了图像。图像的最终输出不正确。

下面是我尝试翻转 180 / 镜像的图像示例。

在此处输入图像描述

这是我用来执行翻转的代码:

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

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

在此处输入图像描述

我正在使用 ClearCanvas DICOM 库,但这无关紧要,因为我只是想操纵文件本身中包含的原始像素数据。

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

一些帮助将不胜感激。我已经尽力搜索了,但无济于事。

4

1 回答 1

0

花了一段时间,但我最终通过使用 Java 库中的方法解决了我的问题。你可以在这里看到课程。

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

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

谢谢大家的指点。

于 2014-05-05T04:05:05.587 回答