Koushik Ravikumar
Koushik Ravikumar

Reputation: 706

How to extract the text coordinates in an image using tess4j

I'm trying to figure out how to get the coordinates and word rect in a text image after tess4j performs the OCR. i'm a beginner so can somebody please break it down for me? Much appreciated.

Upvotes: 4

Views: 8148

Answers (2)

kane
kane

Reputation: 6027

I'm rather new to tess4j myself and I'd hate to disagree with @nguyenq, but here's how I did it

String imageUrl = "...";
File imageFile = new File(imageUrl);
Image image = ImageIO.read(imageFile);
BufferedImage bi = toBufferedImage(image);
ITesseract instance = new Tesseract();

for(Word word : instance.getWords(bi, ITessAPI.TessPageIteratorLevel.RIL_TEXTLINE)) {
  Rectangle rect = word.getBoundingBox();

  System.out.println(rect.getMinX()+","+rect.getMaxX()+","+rect.getMinY()+","+rect.getMaxY()
                    +": "+word.getText());
}

And here's my toBufferedImage method

public static BufferedImage toBufferedImage(Image img)
{
  if (img instanceof BufferedImage)
  {
      return (BufferedImage) img;
  }

  // Create a buffered image with transparency
  BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);

  // Draw the image on to the buffered image
  Graphics2D bGr = bimage.createGraphics();
  bGr.drawImage(img, 0, 0, null);
  bGr.dispose();

  // Return the buffered image
  return bimage;
}

SO credit

Edit I should note that this is using tess4j v3.3.1. This new convenience API must have been added by @nguyenq after the initial question was posted

Upvotes: 1

nguyenq
nguyenq

Reputation: 8345

Tess4J's unit tests include examples for obtaining bounding boxes for recognized words. The code is similar to Tess4J: How to use ResultIterator?.

Upvotes: 0

Related Questions