Java tesseract返回文本位置的坐标

时间:2016-09-09 23:26:34

标签: java eclipse tesseract tess4j

我在eclipse中使用Java并希望返回找到的所有已识别文本的坐标。我通过tess4j获得的代码目前输出了所有找到的文本,代码如下:

import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import net.sourceforge.tess4j.*;



public class TesseractExample {

    public static void main(String[] args) throws IOException 
    {
        try 
        {           
               String x = System.getProperty("user.dir");
                File b = new File(x+"/inDCM");
            File imageFile = new File(b+"/surrey.png");
            BufferedImage img =  ImageIO.read(imageFile);
            Tesseract instance = Tesseract.getInstance(); 
            ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);  
            ColorConvertOp op = new ColorConvertOp(cs, null);
            op.filter(img, img); 
            try 
            {   
                String result = instance.doOCR(img);

                System.out.println("The result is: " + result);

            }
            catch (TesseractException e) 
            {
                System.out.println("error:" + e);
            }
        }finally{

        }
    }}

是否可以检索坐标?

感谢。

2 个答案:

答案 0 :(得分:0)

您可以通过低级ResultIterator API中的TessBaseAPI对象获取坐标。代码示例可以在项目repo中的单元测试中找到。

答案 1 :(得分:0)

Tess4j在这里对您真的很有帮助。 This is an example I found on the test cases

/**
 * Test of getSegmentedRegions method, of class Tesseract.
 *
 * @throws java.lang.Exception
 */
@Test
public void testGetSegmentedRegions() throws Exception {
    logger.info("getSegmentedRegions at given TessPageIteratorLevel");
    File imageFile = new File(testResourcesDataPath, "eurotext.png");
    BufferedImage bi = ImageIO.read(imageFile);
    int level = TessPageIteratorLevel.RIL_SYMBOL;
    logger.info("PageIteratorLevel: " + Utils.getConstantName(level, TessPageIteratorLevel.class));
    List<Rectangle> result = instance.getSegmentedRegions(bi, level);
    for (int i = 0; i < result.size(); i++) {
        Rectangle rect = result.get(i);
        logger.info(String.format("Box[%d]: x=%d, y=%d, w=%d, h=%d", i, rect.x, rect.y, rect.width, rect.height));
    }

    assertTrue(result.size() > 0);
}

TessPageIteratorLevel将定义如何分割图像内容(即字符或单词)

TessPageIteratorLevel Options

    /**
     * Block of text/image/separator line.
     */
    public static final int RIL_BLOCK = 0;
    /**
     * Paragraph within a block.
     */
    public static final int RIL_PARA = 1;
    /**
     * Line within a paragraph.
     */
    public static final int RIL_TEXTLINE = 2;
    /**
     * Word within a textline.
     */
    public static final int RIL_WORD = 3;
    /**
     * Symbol/character within a word.
     */
    public static final int RIL_SYMBOL = 4;