Tesseract: C# Tesseract 3.02 如何访问图像中单词的每个字符

创建于 2014-01-12  ·  3评论  ·  资料来源: charlesw/tesseract

你好,我是这里的新手。
首先,我需要在图像中的单词的每个字符上绘制矩形。
在旧版本的tesseract中,我发现我们可以通过以下方式访问每个字符

foreach (tessnet2.Character c in word.CharList)
e.Graphics.DrawRectangle.........

demo

但是,现在我正在使用 Tesseract 3.02 处理 C# winform

TesseractEngine a = new TesseractEngine(@"./tessdata", "eng", EngineMode.TesseractAndCube);
Tesseract.Page page1 = a.Process(image);
foreach(....... 在第 1 页)
{
// 从(每个字符的边界框)绘制矩形
}

问题 1:我如何访问 page1 的每个字符。

我尝试了许多方法,如 PageIteratorLevel 并获取页面的某些部分,如第一行、第一个单词或第一个块,但我无法获取它们的第一个字符。
好吧,我注意到在 page1 的 HOCRtext 的结果文本中,每个元素(如 word、line 和 block)都有 Bounding box 的值。

问题 2:我如何获得每个元素的边界框的值。 (我发现只有一种方法“TryGetBoundingBox”只返回布尔值。

谢谢你。

question

最有用的评论

回答 Q1:

查看提供的控制台示例,因为它给出了如何迭代结果的示例,但是应该可以使用以下内容:

using (var iter = page.GetIterator()) {
    do {
        do {
            do {
                if (iter.IsAtBeginningOf(PageIteratorLevel.Block)) {
                    // do whatever you need to do when a block (top most level result) is encountered.
                }
                if (iter.IsAtBeginningOf(PageIteratorLevel.Para)) {
                    // do whatever you need to do when a paragraph is encountered.
                }
                if (iter.IsAtBeginningOf(PageIteratorLevel.TextLine)) {
                    // do whatever you need to do when a line of text is encountered is encountered.
                }                                               
                if (iter.IsAtBeginningOf(PageIteratorLevel.Word)) {
                    // do whatever you need to do when a word is encountered is encountered.
                }

                // get bounding box for symbol
                Rect symbolBounds;
                if(iter.TryGetBoundingBox(PageIteratorLevel.Symbol, out symbolBounds)) {
                    // do whatever you want with bounding box for the symbol
                }
            } while(iter.Next(PageIteratorLevel.Word, PageIteratorLevel.Block));
        } while (iter.Next(PageIteratorLevel.TextLine, PageIteratorLevel.Word));
    } while (iter.Next(PageIteratorLevel.Para, PageIteratorLevel.TextLine));
}

请注意,一般结果层次结构如下:

块 -> Para -> TextLine -> Word -> Symbol

即结果集可以包含许多块,这些块又可以包含许多段落等等。

问题 2 的答案:

如上所述, TryGetBoundingBox方法在输出参数中返回边界。 就像Dictionary.TryGetValue所做的那样。

所有3条评论

回答 Q1:

查看提供的控制台示例,因为它给出了如何迭代结果的示例,但是应该可以使用以下内容:

using (var iter = page.GetIterator()) {
    do {
        do {
            do {
                if (iter.IsAtBeginningOf(PageIteratorLevel.Block)) {
                    // do whatever you need to do when a block (top most level result) is encountered.
                }
                if (iter.IsAtBeginningOf(PageIteratorLevel.Para)) {
                    // do whatever you need to do when a paragraph is encountered.
                }
                if (iter.IsAtBeginningOf(PageIteratorLevel.TextLine)) {
                    // do whatever you need to do when a line of text is encountered is encountered.
                }                                               
                if (iter.IsAtBeginningOf(PageIteratorLevel.Word)) {
                    // do whatever you need to do when a word is encountered is encountered.
                }

                // get bounding box for symbol
                Rect symbolBounds;
                if(iter.TryGetBoundingBox(PageIteratorLevel.Symbol, out symbolBounds)) {
                    // do whatever you want with bounding box for the symbol
                }
            } while(iter.Next(PageIteratorLevel.Word, PageIteratorLevel.Block));
        } while (iter.Next(PageIteratorLevel.TextLine, PageIteratorLevel.Word));
    } while (iter.Next(PageIteratorLevel.Para, PageIteratorLevel.TextLine));
}

请注意,一般结果层次结构如下:

块 -> Para -> TextLine -> Word -> Symbol

即结果集可以包含许多块,这些块又可以包含许多段落等等。

问题 2 的答案:

如上所述, TryGetBoundingBox方法在输出参数中返回边界。 就像Dictionary.TryGetValue所做的那样。

嗨,查尔斯,

希望你做得很好。

我是这个东西的新手,我可以从小图片或测试图片中获取所需的文本,但不能从实际图片中获取:

  1. 如何从照片中提取 BIB#。
    NotWorking

  2. 如何从整张照片中识别 BIB# 区域。
    H1764

谢谢。

使用 opencv 查找和裁剪区域。 有一个人用 Python 编写的演示并不太难转换为 .net。

此页面是否有帮助?
0 / 5 - 0 等级