从我所读出的网络中的Y值上是亮度值,并且可以被用来创建一个灰度级图像。下面的链接: http://www.bobpowell.net/grayscale.htm ,有一些上工作了的位图图像的亮度C#代码:

{
  Bitmap bm = new Bitmap(source.Width,source.Height);
  for(int y=0;y<bm.Height;y++)        public Bitmap ConvertToGrayscale(Bitmap source)
  {
    for(int x=0;x<bm.Width;x++)
    {
      Color c=source.GetPixel(x,y);
      int luma = (int)(c.R*0.3 + c.G*0.59+ c.B*0.11);
      bm.SetPixel(x,y,Color.FromArgb(luma,luma,luma));
    }
  }
  return bm;
}

我有一个方法,该方法返回YUV值,并有一个字节数组的Y数据。我的当前片的代码,它是没有按规定Marshal.Copy - 尝试读取或写入受保护的存储器

public Bitmap ConvertToGrayscale2(byte[] yuvData, int width, int height)
        {
            Bitmap bmp;
            IntPtr blue = IntPtr.Zero;

            int inputOffSet = 0;
            long[] pixels = new long[width * height];

            try
            {
                for (int y = 0; y < height; y++)
                {
                    int outputOffSet = y * width;
                    for (int x = 0; x < width; x++)
                    {
                        int grey = yuvData[inputOffSet + x] & 0xff;
                        unchecked
                        {
                            pixels[outputOffSet + x] = UINT_Constant | (grey * INT_Constant);
                        }
                    }

                    inputOffSet += width;
                }

                blue = Marshal.AllocCoTaskMem(pixels.Length);
                Marshal.Copy(pixels, 0, blue, pixels.Length); // fails here : Attempted to read or write protected memory


                bmp = new Bitmap(width, height, width, PixelFormat.Format24bppRgb, blue);

            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                if (blue != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(blue);
                    blue = IntPtr.Zero;
                }

            }

            return bmp;
        }

任何帮助,将理解的?

有帮助吗?

解决方案 3

我得到一个黑色图像在,如果我使用此代码左上角顶部的几个像素,这是稳定运行时:

 public static Bitmap ToGrayscale(byte[] yData, int width, int height)
    {
        Bitmap bm = new Bitmap(width, height, PixelFormat.Format32bppRgb);
        Rectangle dimension = new Rectangle(0, 0, bm.Width, bm.Height);
        BitmapData picData = bm.LockBits(dimension, ImageLockMode.ReadWrite, bm.PixelFormat);
        IntPtr pixelStateAddress = picData.Scan0;

        int stride = 4 * (int)Math.Ceiling(3 * width / 4.0);
        byte[] pixels = new byte[stride * height];

        try
        {
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    byte grey = yData[y * width + x];
                    pixels[y * stride + 3 * x] = grey;
                    pixels[y * stride + 3 * x + 1] = grey;
                    pixels[y * stride + 3 * x + 2] = grey;

                }
            }

            Marshal.Copy(pixels, 0, pixelStateAddress, pixels.Length);
            bm.UnlockBits(picData);
        }
        catch (Exception)
        {
            throw;
        }

        return bm;
    }

其他提示

我想你已经分配pixels.Length 字节但复制pixels.Length 多头,这是8倍的存储器(长为64位或8字节大小)。

您可以尝试:

blue = Marshal.AllocCoTaskMem(Marshal.SizeOf(pixels[0]) * pixels.Length); 

您可能还需要使用int []为位图构造器像素和PixelFormat.Format32bppRgb(因为它们都是32位)。使用长[]给你64个比特每像素未什么24比特的像素格式期待。

您可以用蓝色的而不是灰色阴影,虽然结束了 - 取决于你UINT_Constant和INT_Constant的值

有没有必要做 “&0xff的”,如yuvData []已经包含一个字节。

下面是另一对夫妇的方法,你可以试试。

public Bitmap ConvertToGrayScale(byte[] yData, int width, int height)
{
    // 3 * width bytes per scanline, rounded up to a multiple of 4 bytes
    int stride = 4 * (int)Math.Ceiling(3 * width / 4.0);

    byte[] pixels = new byte[stride * height];
    for (int y = 0; y < height; y++)
    {
        for (int x = 0; x < width; x++)
        {
            byte grey = yData[y * width + x];
            pixels[y * stride + 3 * x] = grey;
            pixels[y * stride + 3 * x + 1] = grey;
            pixels[y * stride + 3 * x + 2] = grey;
        }
    }

    IntPtr pixelsPtr = Marshal.AllocCoTaskMem(pixels.Length);
    try
    {
        Marshal.Copy(pixels, 0, pixelsPtr, pixels.Length);

        Bitmap bitmap = new Bitmap(
            width, 
            height, 
            stride, 
            PixelFormat.Format24bppRgb, 
            pixelsPtr);
        return bitmap;
    }
    finally
    {
        Marshal.FreeHGlobal(pixelsPtr);
    }
}

public Bitmap ConvertToGrayScale(byte[] yData, int width, int height)
{
    // 3 * width bytes per scanline, rounded up to a multiple of 4 bytes
    int stride = 4 * (int)Math.Ceiling(3 * width / 4.0);

    IntPtr pixelsPtr = Marshal.AllocCoTaskMem(stride * height);
    try
    {
        for (int y = 0; y < height; y++)
        {
            for (int x = 0; x < width; x++)
            {
                byte grey = yData[y * width + x];
                Marshal.WriteByte(pixelsPtr, y * stride + 3 * x, grey);
                Marshal.WriteByte(pixelsPtr, y * stride + 3 * x + 1, grey);
                Marshal.WriteByte(pixelsPtr, y * stride + 3 * x + 2, grey);
            }
        }

        Bitmap bitmap = new Bitmap(
            width,
            height,
            stride,
            PixelFormat.Format24bppRgb,
            pixelsPtr);
        return bitmap;
    }
    finally
    {
        Marshal.FreeHGlobal(pixelsPtr);
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top