如何转换像素格式?从32bppRGB到16bpp灰度c#。

时间:2022-10-01 15:59:41

I need to do some thresholding for my image. The threshold filter function just accepts 8-16bpp grayscale. My bitmap picture has the 32bppRGB pixelformat. Please suggest some code for the same. (I also want to know if it is possible without pixel by pixel operations)

我需要对图像做一些阈值化。阈值滤波函数只接受8-16bpp灰度。我的位图图片有32bppRGB的像素格式。请提出一些相同的代码。(我也想知道如果没有像素操作,它是否可行)

p.s. I am using the Aforge.NET for thresholding.

我正在使用Aforge。净的阈值。

Thanks

谢谢

-Sagar

sagar

2 个解决方案

#1


4  

Easiest way:

简单的方法:

public static Bitmap MakeGrayscale(Bitmap original)
 {
    //make an empty bitmap the same size as original
    Bitmap newBitmap = new Bitmap(original.Width, original.Height);

   for (int i = 0; i < original.Width; i++)
    {
       for (int j = 0; j < original.Height; j++)
       {
          //get the pixel from the original image
          Color originalColor = original.GetPixel(i, j);

         //create the grayscale version of the pixel
          int grayScale = (int)((originalColor.R * .3) + (originalColor.G * .59)
              + (originalColor.B * .11));

         //create the color object
          Color newColor =  Color.FromArgb(grayScale, grayScale, grayScale);

          //set the new image's pixel to the grayscale version
          newBitmap.SetPixel(i, j, newColor);
         }
     }

    return newBitmap;
 }

Faster way:

更快的方式:

public static Bitmap MakeGrayscale2(Bitmap original)
 {
    unsafe
    {
       //create an empty bitmap the same size as original
       Bitmap newBitmap = new Bitmap(original.Width, original.Height);

      //lock the original bitmap in memory
       BitmapData originalData = original.LockBits(
          new Rectangle(0, 0, original.Width, original.Height),
          ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);

      //lock the new bitmap in memory
       BitmapData newData = newBitmap.LockBits(
          new Rectangle(0, 0, original.Width, original.Height), 
         ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);

       //set the number of bytes per pixel
       int pixelSize = 3;

      for (int y = 0; y < original.Height; y++)
       {
          //get the data from the original image
          byte* oRow = (byte*)originalData.Scan0 + (y * originalData.Stride);

         //get the data from the new image
          byte* nRow = (byte*)newData.Scan0 + (y * newData.Stride);

         for (int x = 0; x < original.Width; x++)
          {
             //create the grayscale version
             byte grayScale = 
               (byte)((oRow[x * pixelSize] * .11) + //B
                (oRow[x * pixelSize + 1] * .59) +  //G
                (oRow[x * pixelSize + 2] * .3)); //R

            //set the new image's pixel to the grayscale version
             nRow[x * pixelSize] = grayScale; //B
             nRow[x * pixelSize + 1] = grayScale; //G
             nRow[x * pixelSize + 2] = grayScale; //R
          }
       }

      //unlock the bitmaps
       newBitmap.UnlockBits(newData);
       original.UnlockBits(originalData);

      return newBitmap;
    }
 }

Fastest way:

最快的方式:

public static Bitmap MakeGrayscale3(Bitmap original)
 {
    //create a blank bitmap the same size as original
    Bitmap newBitmap = new Bitmap(original.Width, original.Height);

    //get a graphics object from the new image
    Graphics g = Graphics.FromImage(newBitmap);

   //create the grayscale ColorMatrix
    ColorMatrix colorMatrix = new ColorMatrix(
       new float[][] 
      {
          new float[] {.3f, .3f, .3f, 0, 0},
          new float[] {.59f, .59f, .59f, 0, 0},
          new float[] {.11f, .11f, .11f, 0, 0},
          new float[] {0, 0, 0, 1, 0},
          new float[] {0, 0, 0, 0, 1}
       });

   //create some image attributes
    ImageAttributes attributes = new ImageAttributes();

   //set the color matrix attribute
    attributes.SetColorMatrix(colorMatrix);

   //draw the original image on the new image
    //using the grayscale color matrix
    g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height),
       0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);

   //dispose the Graphics object
    g.Dispose();
    return newBitmap;
 }

#2


8  

Use AForge.NET framework Grayscale filter

使用AForge。净框架灰度级过滤器

The filter accepts 24, 32, 48 and 64 bpp color images and produces 8 (if source is 24 or 32 bpp image) or 16 (if source is 48 or 64 bpp image) bpp grayscale image.

该过滤器接受24、32、48和64 bpp彩色图像,并产生8(如果源是24或32 bpp图像)或16(如果源是48或64 bpp图像)bpp灰度图像。

Then apply threshold filter.

然后应用阈值过滤。

#1


4  

Easiest way:

简单的方法:

public static Bitmap MakeGrayscale(Bitmap original)
 {
    //make an empty bitmap the same size as original
    Bitmap newBitmap = new Bitmap(original.Width, original.Height);

   for (int i = 0; i < original.Width; i++)
    {
       for (int j = 0; j < original.Height; j++)
       {
          //get the pixel from the original image
          Color originalColor = original.GetPixel(i, j);

         //create the grayscale version of the pixel
          int grayScale = (int)((originalColor.R * .3) + (originalColor.G * .59)
              + (originalColor.B * .11));

         //create the color object
          Color newColor =  Color.FromArgb(grayScale, grayScale, grayScale);

          //set the new image's pixel to the grayscale version
          newBitmap.SetPixel(i, j, newColor);
         }
     }

    return newBitmap;
 }

Faster way:

更快的方式:

public static Bitmap MakeGrayscale2(Bitmap original)
 {
    unsafe
    {
       //create an empty bitmap the same size as original
       Bitmap newBitmap = new Bitmap(original.Width, original.Height);

      //lock the original bitmap in memory
       BitmapData originalData = original.LockBits(
          new Rectangle(0, 0, original.Width, original.Height),
          ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);

      //lock the new bitmap in memory
       BitmapData newData = newBitmap.LockBits(
          new Rectangle(0, 0, original.Width, original.Height), 
         ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);

       //set the number of bytes per pixel
       int pixelSize = 3;

      for (int y = 0; y < original.Height; y++)
       {
          //get the data from the original image
          byte* oRow = (byte*)originalData.Scan0 + (y * originalData.Stride);

         //get the data from the new image
          byte* nRow = (byte*)newData.Scan0 + (y * newData.Stride);

         for (int x = 0; x < original.Width; x++)
          {
             //create the grayscale version
             byte grayScale = 
               (byte)((oRow[x * pixelSize] * .11) + //B
                (oRow[x * pixelSize + 1] * .59) +  //G
                (oRow[x * pixelSize + 2] * .3)); //R

            //set the new image's pixel to the grayscale version
             nRow[x * pixelSize] = grayScale; //B
             nRow[x * pixelSize + 1] = grayScale; //G
             nRow[x * pixelSize + 2] = grayScale; //R
          }
       }

      //unlock the bitmaps
       newBitmap.UnlockBits(newData);
       original.UnlockBits(originalData);

      return newBitmap;
    }
 }

Fastest way:

最快的方式:

public static Bitmap MakeGrayscale3(Bitmap original)
 {
    //create a blank bitmap the same size as original
    Bitmap newBitmap = new Bitmap(original.Width, original.Height);

    //get a graphics object from the new image
    Graphics g = Graphics.FromImage(newBitmap);

   //create the grayscale ColorMatrix
    ColorMatrix colorMatrix = new ColorMatrix(
       new float[][] 
      {
          new float[] {.3f, .3f, .3f, 0, 0},
          new float[] {.59f, .59f, .59f, 0, 0},
          new float[] {.11f, .11f, .11f, 0, 0},
          new float[] {0, 0, 0, 1, 0},
          new float[] {0, 0, 0, 0, 1}
       });

   //create some image attributes
    ImageAttributes attributes = new ImageAttributes();

   //set the color matrix attribute
    attributes.SetColorMatrix(colorMatrix);

   //draw the original image on the new image
    //using the grayscale color matrix
    g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height),
       0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);

   //dispose the Graphics object
    g.Dispose();
    return newBitmap;
 }

#2


8  

Use AForge.NET framework Grayscale filter

使用AForge。净框架灰度级过滤器

The filter accepts 24, 32, 48 and 64 bpp color images and produces 8 (if source is 24 or 32 bpp image) or 16 (if source is 48 or 64 bpp image) bpp grayscale image.

该过滤器接受24、32、48和64 bpp彩色图像,并产生8(如果源是24或32 bpp图像)或16(如果源是48或64 bpp图像)bpp灰度图像。

Then apply threshold filter.

然后应用阈值过滤。