【问题标题】:Export JPG out of Dicom File With mDCM使用 mDCM 从 Dicom 文件中导出 JPG
【发布时间】:2010-03-29 19:30:41
【问题描述】:

我正在使用带有 C# 的 mDCM 来查看 dicom 标签,但我正在尝试将像素数据转换为位图并最终转换为 JPG 文件。我已阅读 mDCM Google Group 上有关该主题的所有帖子,并且所有代码示例都不起作用或缺少重要的代码行。我正在使用的图像是 16 位单色 1(即提到的格式,但它实际上是 16 位灰度)。我尝试使用 LockBits、SetPixel 和不安全代码将像素数据转换为位图,但所有尝试都失败了。有没有人有任何代码可以使这项工作。

这是我使用 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 对于我需要做的事情来说太臃肿了。

【问题讨论】:

  • 新增灰度部分。对购买 LeadTools 不感兴趣。需要代码。
  • 我已经能够使用以下代码进一步使用 LockBits 和 Marshal.Copy,但它以彩虹色而不是灰度显示:byte[] pixel = currentPixelData.GetFrameDataU8(frameIndex) ; currentImage = new Bitmap(width, height, 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);

标签: c# dicom


【解决方案1】:

我最终想通了,所以如果其他人偶然发现了这个问题,我可以使用 CodeProject 文章中的代码显示和保存 16 位灰度图像。这是该文章的链接:

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

【讨论】:

  • 嗨,罗斯,我遇到了同样的问题。我无法解决,您是否对初始窗口级别值进行了适当的工作。你将如何做到这一点。谢谢
【解决方案2】:

很遗憾,您没有将 GDCM 与 C# 一起使用。这将是一段 5 行代码:

http://gdcm.sourceforge.net/html/ExtractEncapsulatedFile.cs-example.html

【讨论】:

    【解决方案3】:

    我为 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();
    }
    

    这比它严格需要的要冗长一些 - 我会分解出一个例程,以适合您要使用的编码器的格式返回图像。一般来说,JPEG 是它可以处理的相当有限的像素格式,而 dicom 可以封装一些相当宽的格式。 TIFF 可能是输出文件格式的更好选择。

    我提出这个是因为 DicomDecoder 对象自动处理 dicom 更深奥的像素格式,允许您在图像的原始空间中进行窗口和调平(也就是亮度和对比度),以及获取标签,我们给出你赢得/网络表单控件来显示 AtalaImages,这可能会减少你的应用程序大小。

    当然,有一些合理的原因可能会限制您使用 mDCM,但我认为它可能会帮助您(或其他人)看到另一个选项。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-10
      相关资源
      最近更新 更多