Java tesseract return 文本位置坐标

Java tesseract return co-ordinates of text location

我在 eclipse 中使用 Java 并且想要 return 找到的所有已识别文本的坐标。我通过 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{

        }
    }}

是否可以检索坐标?

谢谢。

您可以通过低级TessBaseAPI API中可用的ResultIterator对象获取坐标。代码示例可以在项目 repo.

的单元测试中找到

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;