Question

D'après ce que j'ai lu sur Internet la valeur Y est la valeur de luminance et peut être utilisé pour créer une image en échelle de gris. Le lien suivant: http://www.bobpowell.net/grayscale.htm , a quelques-uns C # code à travailler la luminance d'une image bitmap:

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

I ont une méthode qui renvoie les valeurs YUV et que les données Y dans un tableau d'octets. J'ai la pièce en cours de code et il échoue sur Marshal.Copy - Tentative de lecture ou d'écriture de mémoire protégée

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

Toute aide serait appréciée?

Était-ce utile?

La solution 3

Je reçois une image en noir avec quelques pixels dans le coin en haut à gauche si j'utilise ce code et c'est stable lors de l'exécution:

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

Autres conseils

Je pense que vous avez alloué pixels.Length octets , mais copient pixels.Length désire ardemment , qui est 8 fois plus de mémoire (longue est de 64 bits ou 8 octets).

Vous pouvez essayer:

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

Vous pourriez aussi avoir besoin d'utiliser int [] pour les pixels et PixelFormat.Format32bppRgb dans le constructeur Bitmap (car ils sont tous les deux 32 bits). L'utilisation à long [] vous donne 64 bits par pixel qui est pas ce qu'est un format de pixel 24 bits attend.

Vous pourriez finir avec des nuances de bleu au lieu de gris si - dépend de ce que vos valeurs de UINT_Constant et INT_Constant sont

.

Il n'y a pas besoin de faire "& 0xff", comme yuvData [] contient déjà un octet.

Voici un autre couple d'approches que vous pourriez essayer.

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);
    }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top