1

我在 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{

        }
    }}

是否可以检索坐标?

谢谢。

4

2 回答 2

1

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

于 2016-09-10T00:48:06.740 回答
1

Tess4j 在这里对你很有帮助。这是我在测试用例中找到的示例

/**
 * 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 选项

    /**
     * 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;
于 2018-10-29T14:56:40.427 回答