簡體   English   中英

在C#中將灰度圖像轉換為黑白圖像

[英]converting a grayscale image to black and white image in 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; 

在任何情況下,您都應停止使用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)
//=======================================================

我對您的代碼做了一些更改。 因此,淺灰色像素變為純白色,深灰色像素變為純黑色。

 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; 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM