2010-03-29 30 views
0

我正在使用mDCM和C#來查看dicom標籤,但我試圖將像素數據轉換爲位圖並最終轉換爲JPG文件。我已閱讀了mDCM Google Group上有關該主題的所有帖子,並且所有代碼示例都不起作用或缺少重要的代碼行。我正在使用的圖像是一個16位monochrome1(這是提到的格式,但它實際上是16位灰度)。我嘗試使用LockBits,SetPixel和不安全的代碼來將像素數據轉換爲位圖,但所有嘗試均失敗。有沒有人有任何可以使這項工作的代碼。使用mDCM將JPG導出爲Dicom文件

下面是使用SetPixel我的最新嘗試:

DcmElement pixelData = this.currentFileFormat.Dataset.GetElement(new DcmTag(DcmConstTags.PixelData)); 
ushort[] pixels = this.currentPixelData.GetFrameDataU16(0); 
this.currentImage = new Bitmap(this.currentPixelData.ImageWidth, this.currentPixelData.ImageHeight, PixelFormat.Format16bppRgb555); 
int resample = (int)Math.Pow(2, (this.currentPixelData.BitsStored - 8)); 

int pxCounter = 0; 
int min = ushort.MaxValue; 
int max = 0; 

for (int c = 0; c < this.currentPixelData.ImageHeight; c++) 
{ 
    for (int r = 0; r < this.currentPixelData.ImageWidth; r++) 
    { 
     ushort pxColor = pixels[pxCounter]; 
     int temp = 255 - (pxColor/resample); 
     if (temp < min) min = temp; 
     if (temp > max) max = temp; 
     this.currentImage.SetPixel(r, c, Color.FromArgb(temp, temp, temp)); 
     pxCounter++; 
    } 
} 

更新: 我已經得到了使用LockBits和Marshal.Copy接近,但圖像出來,在顏色的灰度,而不是彩虹。所以我猜我需要一種方法來灰度數據轉換爲RBG格式:

byte[] pixels = this.currentPixelData.GetFrameDataU8(frameIndex); 
this.currentImage = new Bitmap(this.currentPixelData.ImageWidth, this.currentPixelData.ImageHeight, PixelFormat.Format16bppRgb555); 
BitmapData bitmapData = this.currentImage.LockBits(new Rectangle(0, 0, this.currentImage.Width, this.currentImage.Height), ImageLockMode.ReadWrite, this.currentImage.PixelFormat); 
System.Runtime.InteropServices.Marshal.Copy(pixels, 0, bitmapData.Scan0, this.currentImage.Width * this.currentImage.Height * 2); 
this.currentImage.UnlockBits(bitmapData); 

在此先感謝

附:在任何人建議嘗試像ClearCanvas之類的其他東西之前,知道mDCM是唯一符合我需求的庫,而ClearCanvas對於我需要做的事情太過臃腫。

+0

添加了關於灰度的部分。對購買LeadTools不感興趣。需要代碼。 – 2010-03-29 20:31:13

+0

我已經能夠進一步使用LockBits和Marshal.Copy使用下面的代碼,但它出現在顏色彩虹而不是灰度: byte [] pixels = currentPixelData.GetFrameDataU8(frameIndex); currentImage =新的位圖(寬度,高度,PixelFormat.Format16bppRgb555); BitmapData bitmapData = currentImage.LockBits(new Rectangle(0,0,width,height),ImageLockMode.ReadWrite,currentImage.PixelFormat); System.Runtime。InteropServices.Marshal.Copy(pixels,0,bitmapData.Scan0,width * height * 2); currentImage.UnlockBits(bitmapData); – 2010-03-29 20:41:14

回答

1

我終於想通了,所以如果別人絆倒這個問題,我能夠顯示和使用代碼保存16位灰度圖像從CodeProject文章。這裏有一個鏈接到文章:

http://www.codeproject.com/KB/graphics/Image16bit.aspx?msg=3210733

+0

嗨羅斯, 我堅持在同一個問題。 我無法解決,你的方法適合初始窗口級別值的工作。 你將如何做到這一點。 謝謝 – prashant 2010-04-21 07:02:05

0

我爲Atalasoft工作,我們有一個成像組件會爲你做到這一點:

DicomDecoder decoder = new DicomDecoder(); 
using (AtalaImage image = decoder.Read(stream, frameIndex, null)) { 
    AtalaImage imageToSave = null; 
    if (image.PixelFormat == PixelFormat.Pixel16bppGrayscale) { 
     imageToSave = image.GetChangedPixelFormat(PixelFormat.Pixel8bppGrayscale); 
    } 
    else if (image.PixelFormat == PixelFormat.Pixel48bppBgr) { 
     imageToSave = image.GetChangedPixelFormat(PixelFormat.Pixel24bppBgr); 
    } 
    else { 
     imageToSave = image; 
    } 
    imageToSave.Save(outputStream, new JpegEncoder(), null); 
    if (imageToSave != image) 
     imageToSave.Dispose(); 
} 

這是一個小wordier比它嚴格需要是 - 我會列出一個例程,以適合您要使用的編碼器的格式返回圖像。一般來說,Jpeg是一個相當有限的像素格式,它可以處理,而DICOM可以封裝一些相當寬的格式。對於輸出文件格式來說,TIFF可能是更好的選擇。

因爲DicomDecoder對象會自動處理dicom的更深奧的像素格式,所以您可以在圖像的原始空間中進行窗口和水平調整(即,亮度和對比度),並獲取標籤,我們給你贏/網頁表單控件來顯示AtalaImages,這可能會減少你的應用程序的大小。

當然,有一些合理的原因可能會限制您使用mDCM,但我認爲它可以幫助您(或其他人)看到另一種選擇。