1

我有一个数组,它包含从 Dicom 图像中提取的 PixelData。

这是代码:

        byte[] bytes = img.PixelData.GetFrame(0).Data; // img is the Dicom Image
        int count = bytes.Length / 2;
        ushort[] words = new ushort[count];
        for (int i = 0, p = 0; i < count; i++, p += 2)
        {
            words[i] = BitConverter.ToUInt16(bytes, p);
        }
        pixels16 = words.ToList(); //pixels16 contains now the PixelData for the Grayscale image

现在,这是我的问题,我如何将它渲染到 Picturebox 中?

4

4 回答 4

1

我将位图从 Format16bppGrayScale 转换为 Format8bppIndexed 格式的代码。PictureBox 可以轻松展示这种格式。(如果你愿意,你可以使用不同的调色板)。

public Bitmap Gray16To8bppIndexed(Bitmap BmpIn)
{
    if (BmpIn.PixelFormat != PixelFormat.Format16bppGrayScale)
        throw new BadImageFormatException();

    byte[] ImageData = new byte[BmpIn.Width * BmpIn.Height * 2];
    Rectangle Re = new Rectangle(0, 0, BmpIn.Width, BmpIn.Height);

    BitmapData BmpData = BmpIn.LockBits(Re, ImageLockMode.ReadOnly, BmpIn.PixelFormat);
    Marshal.Copy(BmpData.Scan0, ImageData, 0, ImageData.Length);
    BmpIn.UnlockBits(BmpData);

    byte[] ImageData2 = new byte[BmpIn.Width * BmpIn.Height];
    for (long i = 0; i < ImageData2.LongLength; i++)
        ImageData2[i] = ImageData[i * 2 + 1];
    ImageData = null;

    Bitmap BmpOut = new Bitmap(BmpIn.Width, BmpIn.Height, PixelFormat.Format8bppIndexed);
    BmpData = BmpOut.LockBits(Re, ImageLockMode.WriteOnly, BmpOut.PixelFormat);
    Marshal.Copy(ImageData2, 0, BmpData.Scan0, ImageData2.Length);
    BmpOut.UnlockBits(BmpData);
    ImageData2 = null;
    BmpData = null;

    ColorPalette GrayPalette = BmpOut.Palette;
    Color[] GrayColors = GrayPalette.Entries;
    for (int i = 0; i < GrayColors.Length; i++)
        GrayColors[GrayColors.Length - 1 - i] = Color.FromArgb(i, i, i);
    BmpOut.Palette = GrayPalette;

    return BmpOut;
}
于 2017-02-08T21:16:22.150 回答
0

您可以利用 AForge .NET Framework,它是用于图像处理的出色 .NET 库。内置的 .NET Picturebox 无法使用 System.Drawing.Imaging.PixelFormat.Format16bppGrayScale 显示图像,但 AForge 库有自己的Picturebox控件,检查一下。它需要一个 .NET 映像。

您可以使用 NuGet 轻松地将 AForge 包含到您的项目中:

Install-Package AForge.Controls
Install-Package AForge.Imaging

要不就

Install-Package AForge

下面的示例代码:

//SOME BYTES
//Load here the DICOM image
int width=640, height=480;
int numberOfPixels = width*height;
byte[] source = new byte[2*numberOfPixels];

//With AFORGE
var image = AForge.Imaging.UnmanagedImage.Create(width, height, System.Drawing.Imaging.PixelFormat.Format16bppGrayScale);
IntPtr ptrToImage = image.ImageData;
//Copies the bytes from source to the image
//System.Runtime.InteropServices
Marshal.Copy(source, 0, ptrToImage,numberOfPixels);

//WITH .NET
System.Drawing.Bitmap bitmapImage = new System.Drawing.Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format16bppGrayScale);
var imageData = bitmapImage.LockBits(new System.Drawing.Rectangle(0, 0, width, height), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format16bppGrayScale);
Marshal.Copy(source, 0, imageData.Scan0, numberOfPixels);
bitmapImage.UnlockBits(imageData);   
于 2013-08-13T20:10:48.797 回答
0

好吧,我不知道具体细节,因为这取决于您真正想要如何去做(如果性能很重要,您需要创建自己的 Bitmap 子类,否则,Bitmap.SetPixel 可以正常工作)。

但本质上,您需要将这些像素推入位图,然后将图片框的图像设置为该位图,例如:

Bitmap bitmap = new Bitmap(width, height);

for(int y = 0;y < height;y++)
   for(int x = 0;x < width;x++)
       bitmap.SetPixel(x,y, Color.fromRGB(/* unpack your R,G,B channel of your pixel here */);

pictureBox.Image = bitmap;
于 2013-08-13T19:37:40.273 回答
0

从朋友那里得到这个想法。inputImage.ImageSource属性是具有灰度像素值的二维数组。

Bitmap grayscaleImage = new Bitmap(inputImage.ImageSource);
            for (int x = 0; x < grayscaleImage.Width; x++)
            {
                for (int y = 0; y < grayscaleImage.Height; y++)
                {
                    byte[,] tempMatrix = inputImage.ImageGrayscale;
                    byte temp = tempMatrix[x, y];
                    Color tempColor = Color.FromArgb(255, temp, temp, temp);
                    grayscaleImage.SetPixel(x, y, tempColor);
                }
            }
            picboxDisplay.Image = grayscaleImage;
于 2017-09-30T08:06:19.067 回答