将图像转换为灰度 [英] Convert an image to grayscale

查看:163
本文介绍了将图像转换为灰度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法将图像灰度每比特的格式16像素,而不是各R,G和B分量的设置亮度转换。我现在有从文件BMP。

 位图C =新位图(文件名);

我要一个位图D,这是C的灰度版本。我看到一个构造函数,包括System.Drawing.Imaging.PixelFormat,但我不知道如何使用它。我是新的图像处理和相关的C#库,但有一个适度的经验C#本身。

任何帮助,参考在线来源,提示或建议将AP preciated。

感谢您。

编辑:d为C的灰度版本


解决方案

  

我希望有一个位图D,这是灰度。
  我确实看到了consructor包括
  System.Drawing.Imaging.PixelFormat,
  但我不知道如何使用
  该


下面是如何做到这一点。

 位图grayScaleBP =新
         System.Drawing.Bitmap(2,2,System.Drawing.Imaging.PixelFormat.Format16bppGrayScale);

编辑::要转换为灰度

 位图C =新位图(FROMFILE);
             位图D组;
             INT X,Y;             //循环遍历图像的像素重置颜色。
             为(X = 0; X&下; c.Width; X ++)
             {
                 对于(Y = 0; Y< c.Height; Y ++)
                 {
                     色pixelColor = c.GetPixel(X,Y);
                     色newColor = Color.FromArgb(pixelColor.R,0,0);
                     c.SetPixel(X,Y,newColor); //现在灰阶
                 }
             }
            D = C: // d为的C灰度版本

从<更快版本href=\"http://www.switchonthe$c$c.com/tutorials/csharp-tutorial-convert-a-color-image-to-grayscale\">switchonthe$c$c遵循链接,全面分析:

 公共静态位图MakeGrayscale3(位图原)
{
   //创建一个空白的位图的大小相同的原
   位图newBitmap =新位图(original.Width,original.Height);   //获取图形从新Image对象
   图形G = Graphics.FromImage(newBitmap);   //创建灰度嘉洛斯
   嘉洛斯嘉洛斯=新嘉洛斯(
      新的浮动[] []
      {
         新的浮动[] {.3f,.3f,.3f,0,0},
         新的浮动[] {.59f,.59f,.59f,0,0},
         新的浮动[] {.11f,.11f,.11f,0,0},
         新浮[] {0,0,0,1,0},
         新浮[] {0,0,0,0,1}
      });   //创建一些图像属性
   ImageAttributes属性=新ImageAttributes();   //设置色彩矩阵属性
   attributes.SetColorMatrix(嘉洛斯);   //绘制新图像的原始图像
   //使用灰度色彩矩阵
   g.DrawImage(原件,新的Rectangle(0,0,original.Width,original.Height)
      0,0,original.Width,original.Height,GraphicsUnit.Pixel,属性);   //处置Graphics对象
   g.Dispose();
   返回newBitmap;
}

Is there a way to convert an image to grayscale 16 pixels per bit format, rather than setting each of the r,g and b components to luminance. I currently have a bmp from file.

Bitmap c = new Bitmap("filename");

I want a Bitmap d, that is grayscale version of c. I do see a constructor that includes System.Drawing.Imaging.PixelFormat, but I don't understand how to use that. I'm new to Image Processing and the relevant C# libraries, but have a moderate experience with C# itself.

Any help, reference to an online source, hint or suggestion will be appreciated.

Thank you.

EDIT: d is the grayscale version of c.

解决方案

"I want a Bitmap d, that is grayscale. I do see a consructor that includes System.Drawing.Imaging.PixelFormat, but I don't understand how to use that."

Here is how to do this

Bitmap grayScaleBP = new 
         System.Drawing.Bitmap(2, 2, System.Drawing.Imaging.PixelFormat.Format16bppGrayScale);

EDIT: To convert to grayscale

             Bitmap c = new Bitmap("fromFile");
             Bitmap d;
             int x, y;

             // Loop through the images pixels to reset color.
             for (x = 0; x < c.Width; x++)
             {
                 for (y = 0; y < c.Height; y++)
                 {
                     Color pixelColor = c.GetPixel(x, y);
                     Color newColor = Color.FromArgb(pixelColor.R, 0, 0);
                     c.SetPixel(x, y, newColor); // Now greyscale
                 }
             }
            d = c;   // d is grayscale version of c  

Faster Version from switchonthecode follow link for full analysis:

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

这篇关于将图像转换为灰度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆