2014-05-01 98 views
0

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

下面是我试图翻转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 /镜像。

一些援助将不胜感激。我试过我最好的搜索,但无济于事。

+0

拍一个小图像作为测试,看看它实际移动的是什么字节 – csharpwinphonexaml

+0

事实上,你的图像是正确的形状,并在正确的位置有正确的元素告诉我你的数学是正确的,你正在移动像素正确的地方到正确的地方。所以我不得不怀疑你没有移动整个像素 - 也许你只是移动红色或绿色或蓝色通道,如果是彩色图像,或者你没有正确遮盖透明度并将其添加回来。我说你的数学会给你正确的位置,但是你不是从原点拾取整个像素,或者不是将整个像素写回翻转的图像。 –

+0

“bitsPerPixel”的值是什么(以及图像的PhotometricInterpretation是什么)?如果它大于8,那么你将需要一个int数组而不是字节数组 –

回答

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

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

谢谢大家的指点。