[英]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));
}
}
這可能是由於使用移動設備捕獲圖像時出現陰影。
請讓我知道如何在不丟失圖像的情況下將該圖像轉換為黑白圖像。
有沒有圖書館對我有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.