C# 用c语言将灰度图像转换为黑白图像#

C# 用c语言将灰度图像转换为黑白图像#,c#,C#,我已经写了一些c#代码将我的rgb图像转换为灰度,我想将灰度转换为只有两种颜色,黑色和白色。 你能帮忙吗 代码:- 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++) { fo

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

代码:-

 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; 
Bitmap bmp=新位图(文件);
int width=bmp.width;
int-height=bmp.height;
int[]arr=新int[225];
int i=0;
颜色p;
//灰度
对于(int y=0;y
我对您的代码做了一些更改。因此浅灰色像素变成纯白色,深灰色像素变成纯黑色

 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; 
Bitmap bmp=新位图(文件);
int width=bmp.width;
int-height=bmp.height;
int[]arr=新int[225];
int i=0;
颜色p;
//灰度
对于(int y=0;y
您应该停止在任何情况下使用
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
“”
''将图像转换为黑白。
''' 
“这是我的形象。
“黑白图像”。
公共共享函数GetBlackAndWhiteImage(ByVal img作为图像)作为图像
将bmp变暗为位图=新位图(img.Width、img.Height)
Dim grayMatrix作为新的System.Drawing.Imaging.ColorMatrix(
{
新的Single(){0.299F,0.299F,0.299F,0,0},
新的Single(){0.587F,0.587F,0.587F,0,0},
新的Single(){0.114F,0.114F,0.114F,0,0},
新的Single(){0,0,0,1,0},
新的Single(){0,0,0,0,1}
})
使用g作为Graphics=Graphics.FromImage(bmp)
将ia用作System.Drawing.Imaging.ImageAttributes=新的System.Drawing.Imaging.ImageAttributes()
ia.SetColorMatrix(灰度矩阵)
ia.设定阈值(0.5)
g、 DrawImage(img,新矩形(0,0,img.Width,img.Height),0,0,img.Width,img.Height,
GraphicsUnit.Pixel,ia)
终端使用
终端使用
返回bmp
端函数
在线翻译成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)
//=======================================================
//
///将图像转换为黑白。
/// 
///形象。
///黑白图像。
公共静态图像GetBlackAndWhiteImage(图像img)
{
位图bmp=新位图(img.Width,img.Height);
System.Drawing.Imaging.ColorMatrix grayMatrix=新的ColorMatrix(
新浮动[][]{
新浮点[]{0.299f,0.299f,0.299f,0,0},
新浮点[]{0.587f,0.587f,0.587f,0,0},
新浮点[]{0.114f,0.114f,0.114f,0,0},
新浮点[]{0,0,0,1,0},
新浮点[]{0,0,0,0,1}
}););
使用(Graphics g=Graphics.FromImage(bmp)){
使用(System.Drawing.Imaging.ImageAttributes ia=new System.Drawing.Imaging.ImageAttributes()){
ia.SetColorMatrix(灰度矩阵);
ia.设定阈值(0.5);
g、 DrawImage(img,新矩形(0,0,img.Width,img.Height),0,0,img.Width,img.Height,GraphicsUnit.Pixel,ia);
}
}
返回bmp;
}
//=======================================================
//Telerik提供的服务(www.Telerik.com)
//=======================================================

基于什么标准??如果像素超过某个限制,则使用灰色获取并将其设置为白色,否则设置为黑色。。。但它很可能看起来像s***;)你的灰度转换码是borken,人眼对绿色很敏感,对红色不太敏感,对蓝色很敏感。这和你能吃的东西的颜色有很大关系。使用0.21*r+0.71*g+0.07*b。剪贴成黑色或白色只需比0.5谢谢Farzan Hajain。这很有效。感谢您的建议和支持help@jeanthierry如果其中一个答案解决了您的问题,请确保将其中一个答案标记为已接受,欢迎使用,但请阅读规则,谢谢。请使用
int avg=(int)(0.2989*r+0.5870*g+0.1140*b)/3作为
intavg=(r+g+b)/3是非常错误的@jeanthierry很乐意帮助您,但您需要了解逐像素操作(
For…Bitmap.GetPixel
)比我向您展示的速度慢x100倍。