【发布时间】:2014-09-05 14:23:41
【问题描述】:
我正在尝试运行 tessnet,图像仅包含数字,但结果总是给我“~”,我不明白为什么...... 代码如下:
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
代码可能有什么问题?我该如何解决?
【问题讨论】:
-
可能是您的图像太小 - OCR 通常需要适当的分辨率(大多数 OCRd 文档往往是扫描的,因此需要 300DPI 或更高)。您可能想尝试将图像重新缩放到原来的几倍(先尝试放大 10 倍),看看是否有任何结果。
-
谢谢解答,试过了,还是不行
-
你用你的字体试过trainingtesseract吗?