Findcontours retrieve contours unsorted

2019-03-31 11:12发布

问题:

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

回答1:

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 :

Order in which contours where found :



标签: opencv ocr