【问题标题】:How to convert mobile image into Black and white C#如何将移动图像转换为黑白C#
【发布时间】: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


【解决方案1】:

这里将阈值降低到 90 avg = avg &lt; 90 ? 0 : 255;

或者你可以使用 EmguCV,它更快更容易,而且它们都给出了相同的结果。

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

【讨论】:

    猜你喜欢
    • 2011-09-03
    • 1970-01-01
    • 2011-11-29
    • 1970-01-01
    • 1970-01-01
    • 2011-06-07
    • 2013-01-08
    • 1970-01-01
    • 2013-04-21
    相关资源
    最近更新 更多