【发布时间】: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