C# 将图像的原始像素数据旋转180度
我正在尝试将DICOM文件中的原始像素数据旋转180度或翻转。我已经成功地正确翻转了图像,但是,在将像素数据写回文件时,在本例中,它是一个DICOM文件并显示它。图像的最终输出不正确 下面是我试图翻转180度/镜像的图像示例 下面是我用来执行翻转的代码:C# 将图像的原始像素数据旋转180度,c#,image,dicom,image-rotation,clearcanvas,C#,Image,Dicom,Image Rotation,Clearcanvas,我正在尝试将DICOM文件中的原始像素数据旋转180度或翻转。我已经成功地正确翻转了图像,但是,在将像素数据写回文件时,在本例中,它是一个DICOM文件并显示它。图像的最终输出不正确 下面是我试图翻转180度/镜像的图像示例 下面是我用来执行翻转的代码: string file = @"adicomfile.dcm"; DicomFile df = new DicomFile(); df.Load(file); // Ge
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/镜像
我们将非常感谢您的帮助。我已经尽了最大努力进行搜索,但没有结果。这花了一段时间,但我最终使用Java库中的方法解决了我的问题。你可以看到教室 输出是正确的,我能够继续对原始像素数据进行其他修改
谢谢大家的指点。拿一个小图像作为测试,看看它实际移动了多少字节。你的图像形状正确,在正确的位置有正确的元素这一事实告诉我你的数学是正确的,你正在将像素从正确的位置移动到正确的位置。因此,我不得不怀疑你没有移动整个像素-可能你只是移动了红色、绿色或蓝色通道,如果是彩色图像,或者你没有正确掩盖透明度并将其添加回。我是说你的数学给出了正确的位置,但你要么没有从原始图像中提取整个像素,要么没有将整个像素写回翻转的图像。bitsPerPixel的值是多少,图像的光度解释是什么?如果大于8,则需要整数数组而不是字节数组
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);