【问题标题】:Rotate raw pixel data of an image 180 degrees将图像的原始像素数据旋转 180 度
【发布时间】:2023-11-16 20:32:01
【问题描述】:

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

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

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

        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 / 镜像。

我们将不胜感激。我已尽力搜索 SO,但无济于事。

【问题讨论】:

  • 拿一张小图作为测试,看看它实际移动了哪些字节
  • 您的图像是正确的形状并且在正确的位置具有正确的元素这一事实告诉我您的数学是正确的,您正在将像素从正确的位置移动到正确的位置。所以我不得不怀疑你没有移动整个像素 - 如果是彩色图像,你可能只是移动红色或绿色或蓝色通道,或者你没有正确掩盖透明度并将其重新添加。我是说你的数学为您提供了正确的位置,但您要么没有从原始图像中拾取整个像素,要么没有将整个像素写回翻转的图像。
  • bitsPerPixel 的值是多少(以及图像的 PhotometricInterpretation 是什么)?如果它大于 8,那么你需要一个 int 数组而不是 byte 数组

标签: c# image dicom image-rotation clearcanvas


【解决方案1】:

花了一些时间,但我最终通过使用 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);

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

谢谢大家的指点。

【讨论】:

    最近更新 更多