【问题标题】:Convert gray image to binary image (0 1 image) in C#?在 C# 中将灰度图像转换为二进制图像(0 1 图像)?
【发布时间】:2015-12-07 01:06:59
【问题描述】:

我有一些代码行。我只想将灰度图像转换为二进制图像。它看起来很简单,但我不明白哪里错了! 你能告诉我哪里错了吗。 就是这样:

private void convert()
        {
            try
            {
                OpenFileDialog op = new OpenFileDialog();
                op.InitialDirectory = "D:/";
                op.Filter = "All Files|*.*|JPEGs|*.jpg|Bitmaps|*.bmp|GIFs|*.gif";
                op.FilterIndex = 1;

                if (op.ShowDialog() == DialogResult.OK)
                {
                    pictureBox3.Image = Image.FromFile(op.FileName);
                    pictureBox3.SizeMode = PictureBoxSizeMode.StretchImage;
                    pictureBox3.BorderStyle = BorderStyle.Fixed3D;

                    Bitmap img = new Bitmap(op.FileName);
                    int width = img.Width;
                    int height = img.Height;
                    string t = "";
                    for (int i = 0; i < width; i++)
                    {
                        for (int j = 0; j < height; j++)
                        {
                            if (img.GetPixel(j, i).A > 100 && img.GetPixel(j, i).B > 100 && img.GetPixel(j, i).G > 100 && img.GetPixel(j, i).R > 100)

                            {
                                t = t + "0";
                            }
                            else
                            {
                                t = t + "1";
                            }
                        }
                        t = t + "\r\n";
                    }
                    textBox1.Text = t;
                }
            }
            catch { };
        }

谢谢大家!

【问题讨论】:

  • 忽略 Alpha 通道。选择列出的公式之一here
  • 它在什么方面不起作用?
  • @Martheen,你为什么要忽略 Alpha 通道?
  • 除非图像是 PNG 或 GIF 格式并实际利用透明度,否则将毫无意义。
  • 投票以“不清楚你在问什么”作为结束投票,因为@Learning_English 没有解释它在什么方面不起作用。

标签: c# image binary


【解决方案1】:
  • 你把ij 误认为GetPixel(width, height)
  • 可能是 img.GetPixel(j, i).A &gt; 100 是多余的
  • catch { }; 这是 你的主要问题。你隐藏了一个异常和惊人的
  • 使用StringBuilder

所以:

private void Convert()
{        
    OpenFileDialog op = new OpenFileDialog();
    op.InitialDirectory = "D:/";
    op.Filter = "All Files|*.*|JPEGs|*.jpg|Bitmaps|*.bmp|GIFs|*.gif";
    op.FilterIndex = 1;

    if (op.ShowDialog() == DialogResult.OK)
    {
        pictureBox3.Image = Image.FromFile(op.FileName);
        pictureBox3.SizeMode = PictureBoxSizeMode.StretchImage;
        pictureBox3.BorderStyle = BorderStyle.Fixed3D;
        Bitmap img = new Bitmap(op.FileName);
        StringBuilder t = new StringBuilder();
        for (int i = 0; i < img.Width; i++)
        {
            for (int j = 0; j < img.Height; j++)
            {
                    t.Append((img.GetPixel(i, j).R > 100 && img.GetPixel(i, j).G > 100 &&
                             img.GetPixel(i, j).B > 100) ? 0 : 1);
            }
            t.AppendLine();
        }
        textBox1.Text = t.ToString();
    }
}

【讨论】:

  • 它的工作。非常感谢你!我刚开始使用c#,您的回答对我来说非常详细。再次感谢!
猜你喜欢
  • 1970-01-01
  • 2013-12-16
  • 2019-12-01
  • 2011-02-28
  • 1970-01-01
  • 2014-10-17
  • 2013-11-25
  • 1970-01-01
  • 2014-01-01
相关资源
最近更新 更多