Frage

Von dem, was ich im Internet des Y-Wert gelesen habe, ist der Wert Luminanz und verwendet werden kann, ein Graustufenbild zu erstellen. Über den folgenden Link: http://www.bobpowell.net/grayscale.htm , hat einige C # -Code auf die Arbeit der Luminanz eines Bitmap-Bildes aus:

{
  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;
}

Ich habe ein Verfahren, welches Returns die YUV-Werte und die Y-Daten in einem Byte-Array. Ich habe das aktuelle Stück Code, und es wird auf Marshal.Copy versagt - versucht, zu lesen oder zu schreiben geschützte Speicher

.
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;
        }

Jede mögliche Hilfe würde geschätzt werden?

War es hilfreich?

Lösung 3

bekomme ich ein schwarzes Bild mit ein paar Pixel in der oberen Ecke links, wenn ich diesen Code verwenden, und dies ist stabil, wenn sie ausgeführt wird:

 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;
    }

Andere Tipps

Ich glaube, Sie pixels.Length zugeteilt Bytes , sondern kopieren pixels.Length longs , die so viel Speicher 8mal ist (eine lange 64 Bit oder 8 Bytes groß).

Sie könnten versuchen:

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

Sie können auch auf die Verwendung int müssen [] für Pixel und PixelFormat.Format32bppRgb im Bitmap-Konstruktor (wie sie beide 32 Bits). Mit long [] gibt Ihnen 64 Bits pro Pixel, das, was nicht ein 24-Bit-Pixel-Format erwartet.

Sie könnten mit den Farben blau enden statt grau obwohl -. Hängt davon ab, was Ihre Werte von UINT_Constant und INT_Constant sind

Es gibt keine Notwendigkeit zu tun "& 0xff", wie yuvData [] bereits ein Byte enthält.

Hier sind noch ein paar Ansätze könnten Sie versuchen.

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);
    }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top