【发布时间】:2019-04-11 00:35:30
【问题描述】:
我必须将图像转换为黑白,这是使用移动相机拍摄的。
我已经阅读了与将图像转换为黑白相关的问题和答案,但提供的解决方案对我没有帮助。
下面是我拍摄的照片。
所以我必须将上面的图像保存在我的应用程序文件夹中,根据要求将其转换为黑白。
我曾尝试使用以下 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));
}
}
这可能是由于使用移动设备拍摄图像时出现阴影造成的。
请让我知道如何将此图像转换为黑白而不会丢失图像。
有什么图书馆可以帮助我吗?
【问题讨论】:
-
您正在寻找阈值设置,实际上您需要做的就是调整 "avg = avg
-
另外,令我惊讶的是,您必须在您自己的源代码中“逐个像素地”处理位图数据。 (在 StackOverflow 上搜索 “C# 图像处理” ...)
-
有趣的...找到this post - 请看看第5号。)。正如上面 Trey 所建议的,您需要摆弄您的
avg参数。
标签: c# image-processing jpeg