如何在C#中使用EvilDICOM显示DICOM图像? [英] How to Display DICOM images using EvilDICOM in c#?

查看:117
本文介绍了如何在C#中使用EvilDICOM显示DICOM图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用EvilDICOM库处理一些dicom图像.现在,我试图在C#中的PictureBox中显示图像.我该怎么办?

I want to use EvilDICOM library to work on some dicom images. Now I am trying to show an image in a PictureBox in C#. How can I do that?

推荐答案

这将在不压缩的情况下处理灰色图像.我从此问题

This will work with gray images without compression. I used the function ImageFromRawBgraArray from this question

   public void loadImage(string fileName)
    {

        var dcm = EvilDICOM.Core.DICOMObject.Read(fileName);
        string photo = dcm.FindFirst(TagHelper.PHOTOMETRIC_INTERPRETATION).DData.ToString();
        ushort bitsAllocated = (ushort)dcm.FindFirst(TagHelper.BITS_ALLOCATED).DData;
        ushort highBit = (ushort)dcm.FindFirst(TagHelper.HIGH_BIT).DData;
        ushort bitsStored = (ushort)dcm.FindFirst(TagHelper.BITS_STORED).DData;
        double intercept =(double) dcm.FindFirst(TagHelper.RESCALE_INTERCEPT).DData;
        double slope = (double)dcm.FindFirst(TagHelper.RESCALE_SLOPE).DData;
        ushort rows = (ushort)dcm.FindFirst(TagHelper.ROWS).DData;
        ushort colums = (ushort)dcm.FindFirst(TagHelper.COLUMNS).DData;
        ushort pixelRepresentation = (ushort)dcm.FindFirst(TagHelper.PIXEL_REPRESENTATION).DData;
        List<byte> pixelData = (List<byte>)dcm.FindFirst(TagHelper.PIXEL_DATA).DData_;
        double window = (double)dcm.FindFirst(TagHelper.WINDOW_WIDTH).DData;
        double level = (double)dcm.FindFirst(TagHelper.WINDOW_CENTER).DData;
        int minVal = 0;
        int maxVal = 255;

        if (!photo.Contains("MONOCHROME"))//just works for gray images
            return;

        int index = 0;
        byte[] outPixelData = new byte[rows * colums * 4];//rgba
        ushort mask = (ushort)(ushort.MaxValue >> (bitsAllocated - bitsStored));
        double maxval = Math.Pow(2, bitsStored);

        for (int i = 0; i < pixelData.Count; i+=2)
        {
            ushort gray = (ushort)((ushort)(pixelData[i]) + (ushort)(pixelData[i + 1] << 8));
            double valgray = gray & mask;//remove not used bits

            if (pixelRepresentation == 1)// the last bit is the sign, apply a2 complement
            {
                if (valgray > (maxval / 2))
                    valgray = (valgray - maxval);

            }

            valgray = slope * valgray + intercept;//modality lut


            //This is  the window level algorithm
            double half = ((window - 1) / 2.0) - 0.5;

            if (valgray <= level - half)
                valgray = 0;
            else if (valgray >= level + half)
                valgray = 255;
            else
                valgray = ((valgray - (level - 0.5)) / (window - 1) + 0.5) * 255;

            outPixelData[index] =(byte)valgray;
            outPixelData[index + 1] = (byte)valgray;
            outPixelData[index + 2] = (byte)valgray;
            outPixelData[index + 3] = 255;

            index += 4;
        }


        Image newimage = this.ImageFromRawBgraArray(outPixelData, colums, rows);
        pictureBox1.Image = newimage;
    }


    public Image ImageFromRawBgraArray(
       byte[] arr, int width, int height)
    {
        var output = new Bitmap(width, height);
        var rect = new Rectangle(0, 0, width, height);
        var bmpData = output.LockBits(rect,
            ImageLockMode.ReadWrite, output.PixelFormat);
        var ptr = bmpData.Scan0;
        Marshal.Copy(arr, 0, ptr, arr.Length);
        output.UnlockBits(bmpData);
        return output;
    }

这篇关于如何在C#中使用EvilDICOM显示DICOM图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆