Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用mDCM从Dicom文件导出JPG_C#_Dicom - Fatal编程技术网

C# 使用mDCM从Dicom文件导出JPG

C# 使用mDCM从Dicom文件导出JPG,c#,dicom,C#,Dicom,我使用mDCM和C#来查看dicom标记,但我尝试将像素数据转换为位图,并最终转换为JPG文件。我已经阅读了mDCM谷歌小组关于这个主题的所有帖子,所有的代码示例要么不起作用,要么缺少重要的代码行。我正在处理的图像是16位单色1(这是前面提到的格式,但实际上是16位灰度)。我曾尝试使用LockBits、SetPixel和不安全代码将像素数据转换为位图,但所有尝试均失败。有没有人有任何代码可以使这项工作 以下是我使用SetPixel的最新尝试: DcmElement pixelData = thi

我使用mDCM和C#来查看dicom标记,但我尝试将像素数据转换为位图,并最终转换为JPG文件。我已经阅读了mDCM谷歌小组关于这个主题的所有帖子,所有的代码示例要么不起作用,要么缺少重要的代码行。我正在处理的图像是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++;
    }
}
提前谢谢


另外,在任何人建议尝试ClearCanvas之类的其他功能之前,要知道mDCM是唯一适合我需要的库,而ClearCanvas对于我需要做的事情来说太臃肿了。

太糟糕了,你没有将GDCM与C一起使用。这将是一段5行代码:


我最终找到了答案,因此,如果其他人偶然发现这个问题,我就能够使用CodeProject文章中的代码显示和保存16位灰度图像。以下是该文章的链接:

我为之工作,我们有一个成像组件可以为您完成这项工作:

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可能是更好的选择

我之所以提出这个问题,是因为dicom Decoder对象自动处理dicom更为深奥的像素格式,允许您在图像的原始空间中进行窗口和水平调整(也称为亮度和对比度),以及获取标记,我们为您提供win/web表单控件,用于显示图像,这可能会大大减少你的应用程序大小


当然,有一系列合理的理由可以解释为什么您可能会被迫使用mDCM,但我认为这可能会帮助您(或其他人)看到另一种选择。

添加了关于灰度的部分。对购买LeadTools不感兴趣。需要代码。我已经能够使用锁位和封送进行进一步的处理。使用以下代码进行复制,但它以彩虹色而不是灰度显示:byte[]pixels=currentPixelData.GetFrameDataU8(frameIndex);currentImage=新位图(宽度、高度、像素格式.Format16bppRgb555);BitmapData BitmapData=currentImage.LockBits(新矩形(0,0,宽度,高度),ImageLockMode.ReadWrite,currentImage.PixelFormat);System.Runtime.InteropServices.Marshal.Copy(像素,0,bitmapData.Scan0,宽度*高度*2);currentImage.UnlockBits(位图数据);嗨,罗斯,我一直在讨论同一个问题。我无法解决,您的方法是否适用于初始窗口级别值。你将如何做到这一点。谢谢
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();
}