tessnet c#给出了错误的结果

时间:2014-09-05 14:23:42

标签: c# ocr

我正在尝试运行tessnet,图片只包含Numbers,但它总是给我“〜”因此,我不明白为什么...... 这是代码:

private void button1_Click(object sender, EventArgs e)
        {
            tessnet2.Tesseract ocr = new tessnet2.Tesseract();
            ocr.SetVariable("tessedit_char_whitelist", "0123456789");
            ocr.Init(@"C:\Users\Poox\Documents\Visual Studio 2013\Projects\testCaptcha\tessdata", "fra", false);
            Bitmap imgBit = new Bitmap(getImage());
           //Changing the colors of the picutre, making it easier to read, number in black and a white background:
            Color good = new Color();
            good = imgBit.GetPixel(44, 19);//the color of the numbers
            int x = 0, y = 0, mx = 100, my = 42;
            for (x = 0; x < mx; x++)
            {
                for (y = 0; y < my; y++)
                {
                    if (imgBit.GetPixel(x, y).Equals(good))
                    {
                        //a number
                        imgBit.SetPixel(x, y, Color.Black);
                    }
                    else { imgBit.SetPixel(x, y, Color.White); }//the background
                }
            }
            imgBit.Save("image2.bmp");
            //OCR; 
            String captcha = "";
            List<tessnet2.Word> result = ocr.DoOCR(imgBit, Rectangle.Empty);
            imgBit.Dispose();
            foreach (tessnet2.Word word in result)
            {
                captcha = captcha + "" + word.Text;
            } MessageBox.Show(captcha);
       }

        public Image getImage()
        {
            Image img = null;
            WebClient client = new WebClient();
            try
            {
                img = Image.FromStream(client.OpenRead("- Image Link - "));
            }catch { }
            client.Dispose();
            client = null;
            return img;
        }

我从链接中获取图片,我使其更加“清晰”,而不是将其传递给OCR,但就像我说的那样,它没有正确读取它,它给了我〜结果..永远! 这是我改变颜色后的图片,使其更加明显: Image

代码有什么问题?我该如何解决?

1 个答案:

答案 0 :(得分:0)

所以这个问题与查理在评论中所说的有关。图像太小。我拍摄了你的图像并将其调整为300x300 dpi并使用插值(重新采样)。然后我将它转换为1BPP,因为你的原始图像是32BPP使用AutoBinarize并将其保存。

您可以在此处查看最终图片:

Processed image