在c#中将灰度图像转换为黑白图像

时间:2015-04-11 11:42:03

标签: c#

我已经编写了一些c#代码将我的rgb图像转换为灰度图像,我想将灰度转换为仅2种颜色,黑色和黑色。 你能帮忙吗?

代码: -

 Bitmap bmp = new Bitmap(file);
 int width = bmp.Width;
 int height = bmp.Height;
 int[] arr = new int[225];
 int i = 0;
 Color p;

 //Grayscale
 for (int y = 0; y < height; y++)
 {
     for (int x = 0; x < width; x++)
     {
         p = bmp.GetPixel(x,y);
         int a = p.A;
         int r = p.R;
         int g = p.G;
         int b = p.B;
         int avg = (r+g+b)/3;
         bmp.SetPixel(x, y, Color.FromArgb(a, avg ,avg, avg));
     }
 }
 pictureBox2.Image = bmp; 

2 个答案:

答案 0 :(得分:4)

您应该在任何情况下停止使用GetPixel方法。

我是VB.Net的人,我已经写过这种方法,你可以将图像转换为黑白图像。

首先我使用灰色矩阵然后使用SetThreshold方法设置将黑色与白色分开的阈值。

''' <summary>
''' Transforms an image to black and white.
''' </summary>
''' <param name="img">The image.</param>
''' <returns>The black and white image.</returns>
Public Shared Function GetBlackAndWhiteImage(ByVal img As Image) As Image

    Dim bmp As Bitmap = New Bitmap(img.Width, img.Height)

    Dim grayMatrix As New System.Drawing.Imaging.ColorMatrix(
        {
            New Single() {0.299F, 0.299F, 0.299F, 0, 0},
            New Single() {0.587F, 0.587F, 0.587F, 0, 0},
            New Single() {0.114F, 0.114F, 0.114F, 0, 0},
            New Single() {0, 0, 0, 1, 0},
            New Single() {0, 0, 0, 0, 1}
        })

    Using g As Graphics = Graphics.FromImage(bmp)

        Using ia As System.Drawing.Imaging.ImageAttributes = New System.Drawing.Imaging.ImageAttributes()

            ia.SetColorMatrix(grayMatrix)
            ia.SetThreshold(0.5)

            g.DrawImage(img, New Rectangle(0, 0, img.Width, img.Height), 0, 0, img.Width, img.Height,
                                             GraphicsUnit.Pixel, ia)

        End Using

    End Using

    Return bmp

End Function

在线翻译为C#:

/// <summary>
/// Transforms an image to black and white.
/// </summary>
/// <param name="img">The image.</param>
/// <returns>The black and white image.</returns>
public static Image GetBlackAndWhiteImage(Image img)
{

Bitmap bmp = new Bitmap(img.Width, img.Height);

System.Drawing.Imaging.ColorMatrix grayMatrix  = new ColorMatrix(
        new float[][] {
            new float[] { 0.299f, 0.299f, 0.299f, 0, 0  },
            new float[] { 0.587f, 0.587f, 0.587f, 0, 0  },
            new float[] { 0.114f, 0.114f, 0.114f, 0, 0 },
            new float[] { 0, 0, 0, 1, 0 },
            new float[] { 0, 0, 0, 0, 1}
        }););

using (Graphics g = Graphics.FromImage(bmp)) {

    using (System.Drawing.Imaging.ImageAttributes ia = new System.Drawing.Imaging.ImageAttributes()) {

        ia.SetColorMatrix(grayMatrix);
        ia.SetThreshold(0.5);

        g.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, ia);

    }

}

return bmp;

}

//=======================================================
//Service provided by Telerik (www.telerik.com)
//=======================================================

答案 1 :(得分:3)

我已经改变了你的代码。因此浅灰色像素变为纯白色,深灰色像素变为纯黑色。

 Bitmap bmp = new Bitmap(file);
 int width = bmp.Width;
 int height = bmp.Height;
 int[] arr = new int[225];
 int i = 0;
 Color p;

 //Grayscale
 for (int y = 0; y < height; y++)
 {
     for (int x = 0; x < width; x++)
     {
         p = bmp.GetPixel(x,y);
         int a = p.A;
         int r = p.R;
         int g = p.G;
         int b = p.B;
         int avg = (r+g+b)/3;
         avg = avg < 128 ? 0 : 255;     // Converting gray pixels to either pure black or pure white
         bmp.SetPixel(x, y, Color.FromArgb(a, avg ,avg, avg));
     }
 }
 pictureBox2.Image = bmp;