如何显示从Tessnet(Tesseract)获得的tif坐标的图像

时间:2012-06-05 18:00:26

标签: c# ocr tiff tesseract

我正在使用Tessnet作为C#包装器在tesseract引擎上运行OCR。我已经获得了识别单词的图像坐标,我想使用这些坐标来仅显示页面的那一部分。我不在乎这个页面部分是保存为单独的图像,还是只是以某种方式突出显示tif图像的部分。

这是我目前的代码:

TextWriter tw = new StreamWriter(@"U:\user files\bwalker\ocrTesting.txt");
            Bitmap image = new Bitmap(@"u:\user files\bwalker\2849257.tif");
            tessnet2.Tesseract ocr = new tessnet2.Tesseract();
            ocr.SetVariable("tessedit_char_whitelist", "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.,$-/#&=()\"':?"); // If digit only
            ocr.Init(@"C:\Users\bwalker\Documents\Visual Studio 2010\Projects\tessnetWinForms\tessnetWinForms\bin\Release\", "eng", false); // To use correct tessdata
            List<tessnet2.Word> result = ocr.DoOCR(image, System.Drawing.Rectangle.Empty);
            string Results = "";
            foreach (tessnet2.Word word in result)
            {
                Results += word.Confidence + ", " + word.Text + ", " +word.Top+", "+word.Bottom+", "+word.Left+", "+word.Right+"\n";
            } 
            using (StreamWriter writer = new StreamWriter(@"U:\user files\bwalker\ocrTesting2.txt", true))
            {
                writer.WriteLine(Results);
                writer.Close();
            }
            MessageBox.Show("Completed");

这是生成的.txt文件的一部分:

14, Due, 105, 136, 1886, 1962
89, Date, 105, 136, 1978, 2064
50, 06/16/2009, 105, 136, 2298, 2504

1 个答案:

答案 0 :(得分:2)

我已经解决了这个问题。获取单词的坐标后,我这样做是为了显示相关的图像:

System.Drawing.Rectangle dueDateRectangle = new System.Drawing.Rectangle(dueDateRect1, dueDateRect2, dueDateRect4 - dueDateRect1, dueDateRect3 - dueDateRect2);
System.Drawing.Imaging.PixelFormat format = image.PixelFormat;
Bitmap cloneBitmap = image.Clone(dueDateRectangle, format);
MemoryStream ms = new MemoryStream();
cloneBitmap.Save(ms, ImageFormat.Png);
ms.Position = 0;
BitmapImage dueDateImage = new BitmapImage();
dueDateImage.BeginInit();
dueDateImage.StreamSource = ms;
dueDateImage.EndInit();
dueDateImageBox.Source = dueDateImage;