0

I succed to extract text from an image with Tesseract library. Now I want to achieve is the segmentation of each character from picture with the bounding box of each character. I've write my code just like some tutorial, here's my source code :

String SourceImage = cursor.getString(URIImage);
Bitmap OutputImagePicker = BitmapFactory.decodeFile(SourceImage); 

TessBaseAPI baseApi = new TessBaseAPI();
baseApi.setDebug(true);
baseApi.init(Path, Language);
baseApi.setImage(OutputImagePicker);
String Text = baseApi.getUTF8Text();

final ResultIterator iterator = baseApi.getResultIterator();
String lastUTF8Text;
     float lastConfidence;
     int[] lastBoundingBox;
     int count = 0;
     iterator.begin();
     do {
         lastUTF8Text = iterator.getUTF8Text(PageIteratorLevel.RIL_WORD);
         lastConfidence = iterator.confidence(PageIteratorLevel.RIL_WORD);
         lastBoundingBox = iterator.getBoundingBox(PageIteratorLevel.RIL_WORD);
         count++;
         } 
     while (iterator.next(PageIteratorLevel.RIL_WORD));

 baseApi.end(); 

 RecognizedText.setText(Text);
 ImageOutput.setImageBitmap(OutputImagePicker);

But, the result is picture bitmap no line bounding box ??? What's wrong with my source ??? i confuse, i've search some tutorials about this, but there's no one explain about it. Is anyone know how to achieve it ??? Please ???

EDITED

I got advice from Nguyenq that i need to draw rect using coordinat in lastBoundingBox, here's my code to draw it :

      Canvas canvas = new Canvas(OutputImagePicker);

      // Draw bounding boxes around each word         
      for (int i = 0; i < lastBoundingBox.length; i++) {
        paint.setAlpha(0xFF);
        paint.setColor(Color.RED);
        paint.setStyle(Style.STROKE);
        paint.setStrokeWidth(2);
        int Rect = lastBoundingBox.length;
        canvas.drawRect(Rect, paint);
      } 

But, it still give an error, it said "The method drawRect(Rect Paint) in the type Canvas is not applicable for arguments int paint", as shown below ,.... what should i do ???

Community
  • 1
  • 1
Papin Nasri
  • 51
  • 1
  • 7
  • You'd need to draw those bounding boxes. – nguyenq May 08 '14 at 12:22
  • how is it ??? can you give me some example / tutorial ??? thanks – Papin Nasri May 09 '14 at 03:29
  • You already got the coordinates in `lastBoundingBox`; now `Canvas.drawRect` it. – nguyenq May 09 '14 at 13:46
  • thanks, do you mean like this ??? Canvas canvas = new Canvas(OutputImagePicker); for (int i = 0; i < lastBoundingBox.size(); i++) { paint.setAlpha(0xFF); paint.setColor(0xFF00CCFF); paint.setStyle(Style.STROKE); paint.setStrokeWidth(2); Rect r = lastBoundingBox.get(i); canvas.drawRect(r, paint); } – Papin Nasri May 10 '14 at 03:49
  • I've tried that way but, it give error, it said "The method drawRect(Rect Paint) in the type Canvas is not applicable for arguments int paint",.... I'm very confuse now, there's no clear & detailed example to draw rect using coordinates in lastBoundingBox – Papin Nasri May 10 '14 at 12:00
  • Doesn't a rectangle have four corners? ([hint](https://github.com/rmtheis/tess-two/blob/master/tess-two/src/com/googlecode/tesseract/android/PageIterator.java)) – nguyenq May 10 '14 at 14:45
  • yes, a rectangle have four corners,.... i'm just confuse how to draw it using Canvas.drawRect ??? – Papin Nasri May 10 '14 at 14:59
  • http://stackoverflow.com/questions/7344497/android-canvas-draw-rectangle http://developer.android.com/reference/android/graphics/Rect.html – nguyenq May 10 '14 at 15:12
  • thank you, I've read that article,.... and I've tried this code, What I'm confused is how to retrieve the coordinat value lastBoundingBox, then put it into this canvas.drawRect(???????, paint); I need to fill with what ??? – Papin Nasri May 10 '14 at 15:19
  • lastBoundingBox is an array of `int`, probably consisting of 4 elements. So `Rect rect = new Rect(lastBoundingBox[0], lastBoundingBox[1], lastBoundingBox[2], lastBoundingBox[3]);` – nguyenq May 10 '14 at 15:37
  • thank you,... thank you,... thank you,... for your help, it works, although who got the bounding box is the word, not the character,.... but it's very helpfull, thank youvery much Mr.Nguyenq,.... Now, i've to find how to get bounding box for each character, not for each word,...... – Papin Nasri May 10 '14 at 15:57
  • Use `PageIteratorLevel.RIL_SYMBOL`. – nguyenq May 10 '14 at 16:15
  • yeah,.... it works pretty well so far, but now it's now only the last of characters get bounding box, for example word is "DOG",... so in that picture the only character get bounding box is "G",.... the others got no line bounding box,... – Papin Nasri May 10 '14 at 16:31
  • You should create separate StackOverflow posts for your follow-on questions. One question per post. – rmtheis May 11 '14 at 16:24

0 Answers0