簡體   English   中英

如何將移動圖像轉換為黑白C#

[英]How to convert mobile image into Black and white C#

我必須將圖像轉換為黑白圖像,這是使用移動相機捕獲的。
我已經閱讀了與將圖像轉換為黑白有關的問題和答案,但是提供的解決方案對我沒有幫助。 以下是我拍攝的圖像。

在此處輸入圖片說明

因此,我必須根據需要將上面的圖像轉換為黑白圖像,並將其保存在應用程序文件夾中。
我曾嘗試使用下面的C#代碼,但它給我的圖像不完整。

代碼1

Bitmap bmp = new Bitmap(@"c:\test.jpg");

            Bitmap bw = bmp.Clone(new Rectangle(0, 0, bmp.Width, bmp.Height),
      PixelFormat.Format8bppIndexed);

代碼2

  Bitmap bmp = new Bitmap(@"c:\test.jpg");
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));
                }
            }  

但是這兩個代碼都在轉換原始圖像,如下所示。 在此處輸入圖片說明

這可能是由於使用移動設備捕獲圖像時出現陰影。
請讓我知道如何在不丟失圖像的情況下將該圖像轉換為黑白圖像。
有沒有圖書館對我有幫助。

將閾值降低到90,這里avg = avg < 90 ? 0 : 255; avg = avg < 90 ? 0 : 255;

或者您可以使用EmguCV,它變得更快,更容易,而且它們都給出相同的結果。

Image<Gray, byte> img = new Image<Gray, byte>("1.jpg");
img._ThresholdBinary(new Gray(90), new Gray(255));

在此處輸入圖片說明

暫無
暫無

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

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