4

WE are implementing OCR using opencv for android, everything went fine till the part of findning contours using Imgproc.findcontours() it gives back the contours not in the same order as they were in the input image i.e : input image M N O P the first contour gets from findcontours() is P input image E F G H the first contour gets from findcontours() is E (here is right) input image I J K L the first contour gets from findcontours() is J so it seems that it extracts the contours randomly how can we fix this? because we want to give back the word as it was written exactly in the image

Hend Mohamad
  • 51
  • 1
  • 3

1 Answers1

10

OpenCV doesn't seems to have any order while finding contours. If we need it sorted we should do it manually, which is an extra task.

But for OCR purposes, I too had this problem. So what i did was to find the centroids of the detected contours. It can be found out using moments.

Finding centroid using Moments

Or you can find the bounding box of the contour, then find the center of that bounding box. Later after ocr put the data on the same centroid.

This was the method I used when I prepared OCR myself. You can find the complete details in this SOF question : Simple Digit Recognition OCR in OpenCV-Python

EDIT : Order of FindContours, (after comment from vasile)

Original image :

enter image description here

Order in which contours where found :

enter image description here

Community
  • 1
  • 1
Abid Rahman K
  • 51,886
  • 31
  • 146
  • 157
  • OpenCV findContours implements a very specific algorithm order when retrieving contours, it's just that the order is different than what you expect :) It scans the image from top to bottom and from left to right. And so, a J which is a pixel higher than the rest will be extracted before an A, by example – Sam Jun 21 '12 at 19:05
  • But i felt it opposite way, from bottom right, because that is the way i got in all cases. May be i will just upload it now. – Abid Rahman K Jun 21 '12 at 19:08
  • @vasile: Oops,,, fonts are little small, if enlarged, get mixed up. But it is readable, and marked some out of order ones. – Abid Rahman K Jun 21 '12 at 19:12
  • Thank you all you really helped me, it's done in my code now i sorted the contours based on its centroid ..thanksss – Hend Mohamad Jun 22 '12 at 10:36
  • 2
    "Best way of expressing gratitude in SOF is upvoting or accepting the answers" - Someone said so. May be, you can do that. – Abid Rahman K Jun 22 '12 at 14:02
  • Hey I think this a good approach, but it's not really suitable for me. If I understand correctly, you would use the x and y value of the centroids to compare to each other, and the ones that on a same line would have the same y values. In my case, I can't guarantee that all the characters will be at the same size and font, so their center of mass would be different in both x and y axis, even when they are in a same line. To add more complexity, the lines maybe slightly tilted compared to the horizon because there was a shaking hands holding the camera. Did you know to solve that too? – FlySoFast Jul 16 '15 at 03:22